Jump to content

Help With C [arrays]


sablefoxx

Recommended Posts

Okay, making this for school, read in values store in an array sort, then print back out.  Pretty simple.  And im pretty sure this is a really stupid quetion, but i cant figure it out for the life of me.

Okay, while compiling i keep getting:

h:7.cpp In function `void array_sort(int, int)':

63 h:7.cpp invalid types `int[int]' for array subscript

63 h:7.cpp invalid types `int[int]' for array subscript

64 h:7.cpp invalid types `int[int]' for array subscript

64 h:7.cpp invalid types `int[int]' for array subscript

67 h:7.cpp invalid conversion from `int' to `int*'

67 h:7.cpp  initializing argument 1 of `void array_swap(int*, int, int)'

Why is this?  Why cant i take the value at array[ i ] and assign 'min' to it?

Other Tips would be nice too  :P !

This is a work in progress! (havnt even written the 'swap' function yet so srry if its kinda hard to read i commented best i could)

/***************************************
  Name: XXXXXXXXXXX
  Date: 20/03/08 1:24 a.m.
  Description: Input Values and Sort
****************************************/
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10

int array[MAXSIZE];

int main()/*================START_MAIN================*/{
   
    /*---FUNCTION_PROTOTYPES---*/
   int array_read  (int array[]);  //take in values store as array
   void array_sort (int array[], int);  //sorts array
   void array_print(int array[], int); //print out array's values to user
   int size;
   
     /*---FUNCTION_CALLS---*/
   size = array_read(array);
   printf("n-->Array is Full, Sorting...n", size);
   array_print(array, size); // print before sort
   array_sort (array, size);
   printf("n<--Array Sorting Complete!!!n");
   array_print(array, size); //print after sort
   printf("n >>End Of File<<n");
   return EXIT_SUCCESS;    
/*=======================END_MAIN=====================*/}

/*---ARRAY_READ---*/
int array_read(int array[]){
    int r,    //scanf's return value (0, 1, EOF)
        next, //usr's input
        cnt;  //array index value, starts at zero
    cnt = 0;
    printf("nInput Values: ");
    while((r=scanf("%i", &next))!=EOF && cnt < MAXSIZE)
    {
       if(r==0){
                printf("n***Input Must be a Number***n");
                while(getchar() != 'n')//flush input
               ;}
       else
                array[cnt++]=next;
       
       if(cnt == MAXSIZE)
         break; //dont want to print and extra line
       else
         printf("n  Next Value: ");
     }
     return(cnt); //return size of the array
}

/*---ARRAY_SORT---*/
void array_sort(int array, int size){
     void array_swap (int array[], int, int);//used to move values in array, called by sort
     int i,                //array index value
         min_index, //min's index value
         j;               //working index
     
     min_index = i = 0;
     
     while(i <= (size-2)){
                       min = array[i];  // <------ ***HERE IS WHERE PROBLEM STARTS***
                       for(j=(i+1);i<=(size-1);   ){
                                            if(array[j] <= min){
                                                           min = (array[j]);
                                                           min_index = j;}
                                                    }
                       array_swap(array, i, min_index);
                       }
     return /*void*/;
     }

/*---ARRAY_SWAP---*/
void array_swap(int array[], int i, int min_index){  //HAVE NOT WRITTEN THIS FUNCTION YET
     //int temp1;
     printf("ni=%inmin_index=%in", i, min_index); //making sure values are past correctly
    return /*void*/;    }

/*---ARRAY_PRINT---*/
void array_print(int array[], int size){    
     int cnt; //array index value
     cnt = 0; //index starts at zero

     while(cnt < size){
       printf("n %i:%i n", (cnt+1), array[cnt]);
       cnt++;}
     return /*void*/;
}

Thx for the help in adv!

Link to comment
Share on other sites

***SOLVED***


i forgot the damn [  ] ,  lol

here is the fixed code (although i still cant get array_sort to work  :-(

/***************************************
  Name: Joe XXXXXXXXXXX
  Date: 20/03/08 1:24 a.m.
  Description: Input Values and Sort
****************************************/
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10

int array[MAXSIZE];

int main()/*================START_MAIN================*/{
   
   /*---FUNCTION_PROTOTYPES---*/
   int array_read  (int array[]);  //take in values store as array
   void array_sort (int array[], int);  //sorts array
   void array_print(int array[], int); //print out array's values to user
   int size;
   
   /*---FUNCTION_CALLS---*/
   size = array_read(array);
   array_print(array, size);
   printf("n-->Array is Full, Sorting...n");
   array_sort (array, size);
   printf("n<--Array Sorting Complete!!!n");
   array_print(array, size);
   printf("n >>End Of Program<<n");
   return EXIT_SUCCESS;    
/*=======================END_MAIN=====================*/}

/*---ARRAY_READ---*/
int array_read(int array[]){
    int r,    //scanf's return value (0, 1, EOF)
        next, //usr's input
        cnt;  //array index value, starts at zero
    cnt = 0;
    printf("nInput Values: ");
    while((r=scanf("%i", &next))!=EOF && cnt < MAXSIZE)
    {
       if(r==0){
                printf("n***Input Must be a Number***n");
                while(getchar() != 'n')//flush input
               ;}
       else
                array[cnt++]=next;
       
       if(cnt == MAXSIZE)
         break; //dont want to print and extra line
       else
         printf("n  Next Value: ");
     }
     return(cnt);
}

/*---ARRAY_SORT---*/
void array_sort(int array[], int size){ //  <------ DONT FORGET THE [ and the damn ]
     void array_swap (int array[], int, int);//used to move values in array, called by sort
     int cur_val,     //smallest known value  
         pass,    //current pass over array
         cur_index, //current index
         wrk_index;       //working index, the target
     
     cur_index = wrk_index = 0; //start at zero
     
     while(cur_index<=(size-1)){ //make teacher angery by writing your own sort function
                       
                       cur_val = array[cur_index];
                       
                       for(pass = (cur_index + 1);pass==size;pass++){
                                            
                                            if(array[pass] <= cur_val){
                                                                       cur_val = array[pass];
                                                                       wrk_index = pass; //leave marker at old location
                                                                      } 
                                                    }     
                       array_swap(array, wrk_index, cur_index);
                       cur_index++;
                       }
     return /*void*/;
     }

/*---ARRAY_SWAP---*/
void array_swap(int array[], int wrk_index, int cur_index){
    int temp1;  //holds value temprly
    
    temp1 = array[cur_index];  //save val
    array[cur_index] = array[wrk_index];  //swap values
    array[wrk_index] = temp1;    
    return /*void*/;    }

/*---ARRAY_PRINT---*/
void array_print(int array[], int size){    
     int cnt; //array index value
     cnt = 0; //index starts at zero
     printf("nnIndex | Valuen");
     while(cnt < size){
       printf("n   %i:     %i n", (cnt+1), array[cnt]);
       cnt++;}
     return /*void*/;
}

Link to comment
Share on other sites

fixed everything...

/***************************************
  Name: Joe XXXXXXXXXXX
  Date: 20/03/08 1:24 a.m.
  Description: Input Values and Sort
****************************************/
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10

int array[MAXSIZE];
int array_read  (int array[]);  //take in values store as array
void array_sort (int array[], int);  //sorts array
void array_print(int array[], int); //print out array's values to user
void array_swap(int *, int, int);

int main()/*================START_MAIN================*/{
   
   /*---FUNCTION_PROTOTYPES---*/

   int size;
   
   /*---FUNCTION_CALLS---*/
   size = array_read(array);
   array_print(array, size);
   printf("n-->Array is Full, Sorting...n");
   array_sort (array, size);
   printf("n<--Array Sorting Complete!!!n");
   array_print(array, size);
   printf("n >>End Of Program<<n");
   return EXIT_SUCCESS;    
/*=======================END_MAIN=====================*/}

/*---ARRAY_READ---*/
int array_read(int array[]){
    int r,    //scanf's return value (0, 1, EOF)
        next, //usr's input
        cnt;  //array index value, starts at zero
    cnt = 0;
    printf("nInput Values: ");
    while((r=scanf("%i", &next))!=EOF && cnt < MAXSIZE)
    {
       if(r==0){
                printf("n***Input Must be a Number***n");
                while(getchar() != 'n')//flush input
               ;}
       else
                array[cnt++]=next;
       
       if(cnt == MAXSIZE)
         break; //dont want to print and extra line
       else
         printf("n  Next Value: ");
     }
     return(cnt);
}

/*---ARRAY_SORT---*/
/* Sorry, I don't know what you were trying to do here.. Insertion sort? */
/* I did bubble sort below for simplicity's sake...
void array_sort(int array[], int size){ //  <------ DONT FORGET THE [ and the damn ]
     void array_swap (int array[], int, int);//used to move values in array, called by sort
     int cur_val,     //smallest known value  
         pass,    //current pass over array
         cur_index, //current index
         wrk_index;       //working index, the target
     
     cur_index = wrk_index = 0; //start at zero
     
     while(cur_index<=(size-1)){ //make teacher angry by writing your own sort function
                       
                       cur_val = array[cur_index];
                       
                       for(pass = (cur_index + 1);pass==size;pass++){
                                            
                                            if(array[pass] <= cur_val){
                                                                       cur_val = array[pass];
                                                                       wrk_index = pass; //leave marker at old location
                                            } 
                       }     
                       array_swap(array, wrk_index, cur_index);
                       cur_index++;
     }
} */

void array_sort(int array[], int size) {
    int i, j;
    for (i = 0; i < size; i++)
        for (j = 0; j < i; j++)
            array_swap(array, i, j);
}

/*---ARRAY_SWAP---*/
void array_swap(int *array, int w, int c){
    array[c] ^= array[w];
    array[w] ^= array[c];
     array[c] ^= array[w]; /* cooler, faster, templess swap */
    /* the tripple-xor swap */
}

/*---ARRAY_PRINT---*/
void array_print(int array[], int size){    
     int cnt; //array index value
     cnt = 0; //index starts 
     printf("nnIndex | Valuen");
     while(cnt < size){
       printf("n%5.i :%5.in", (cnt+1), array[cnt]);
       cnt++;}
//     return /*void*/;
}

also, you don't need return statements on your void functions...

Link to comment
Share on other sites

i know but i think it makes it easier to read.  8-)

thx a million bro

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...