Thread: Few arguments

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Few arguments

    Code:
    /*random exercise 
    * this program assigns a 2d array with random numbers and returns the largest 
    * element in the array: more ambitious is smallest and subscript of largest and
    * smallest
    */
    
    #include <stdio.h>
    #include <stdlib.h> //for random
    #define LEN 10
    
    void Show(int (*)[LEN], int);
    int Max(int *, int);
    int Min(int *, int);
    
    int main(void){
        int array1[LEN][LEN];
        int array2[LEN];
        int (*ptr), (*ptr2)[LEN];/* pointer to array of LEN size */
        ptr = array2;
        ptr2 =array1;
        
        //Show(array, LEN);//notice when called called with just name nothing else
        //whether single or multi
        //printf("Largest integer in array is %d", (Max(array,LEN)) );
        //printf("\n");
        //printf("Smallest integer in array is %d, (Min(array, LEN));
        
        getchar();
        return 0;
    } 
    
    void Show(int (*array)[LEN], rows){
        printf("This array contains: ");
        /* notice the second parameter isn't necessary really but...*/
        int cnt, cnt2;
        for(cnt = 0;cnt < rows; cnt+=1){
            printf("Row %d: ", cnt);
            for(cnt2=0;cnt2 < LEN; cnt2++){
                printf(" %d", *(*(array + cnt) + cnt2));
            }
          printf("\n");
      }
      return;
    }
    I keep getting that my Show arguments doesn't match my prototype. What am i missing please.


    [edit] God My brain is farting something fierce. Please erase this thread webmaster. I'v fixed. [/edit]
    *sighs annoyingly *
    Last edited by caroundw5h; 07-21-2004 at 06:50 PM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
      void Show(int (*)[LEN], int);
      void Show(int (*array)[LEN], rows)
    meh, just do this:

    Code:
      void Show(int**, int);
      void Show(int** array, int rows)
    Much simpler and easier to read. Also, you left out what type "rows" was in the actual function implementation, I don't know about your compiler, but VC++6.0 certainly doesn't like that.


    -edit after seeing the other edit-
    oh.......ok then....

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks for replying. I've finished the code, but now I want to know what the logic would be behind finding the indice of the largest number: would recursion work. that is calling Max in Max. I"m sure how to go about it.
    Code:
     /*random exercise 
    * this program assigns a 2d array with random numbers and returns the largest 
    * element in the array: more ambitious is smallest and subscript of largest and
    * smallest
    */
    
    #include <stdio.h>
    #include <stdlib.h> //for random
    //#include <time.h>//for random
    #define LEN 10
    
    void InitArray(int (*)[LEN], int);//Initializes the array 
    void Show(int (*)[LEN], int);/* shows a multidemsional array */
    int Max(int (*)[LEN], int);/* returns the Highest num in a 2d array */
    int Min(int *, int); /* returns Lowest num in given array */
    
    int main(void){
        srand(time(NULL));// SEED RANDOM GENERATOR WITH TIME ONCE
        int array[LEN][LEN]; // 10 X 10 array
        int Indice, High;
        /*int (*ptr)[LEN];
        // ptr = array;
        /* if declaring a single ptr to array would be:
            int (*ptr);
            ptr = ptr
        * notice the pattern and the nice uses of parentheses 
        */
        
        InitArray(array, LEN);
        
        Show(array, LEN);//notice when called called with just name nothing else
        //whether single or multi
        putchar('\n');
        
        High = Max(array,LEN);
        printf("Largest integer in array is %d", High );
        //printf("\n");
        //printf("Smallest integer in array is %d, (Min(array, LEN));
        
        getchar();
        return 0;
    } 
    
    void Show(int (*array)[LEN], int rows){
        printf("This array contains:\n\n");
        /* notice the second parameter isn't necessary really but...*/
        int cnt, cnt2;
        for(cnt = 0;cnt < rows; cnt+=1){
            printf("Row %d: ", cnt);
            for(cnt2=0;cnt2 < LEN; cnt2++){
                printf(" %3d", *(*(array + cnt) + cnt2));
            }
          printf("\n");
      }
      return;
    }  
    
    int Max(int (*seq)[LEN], int rows){
        /* just like int ptr decalration *seq is a pointer to a const int of LEN values. if was 
        single ptr would declare (*ptr) and now right followups, I see the pattern */
        
            int i,j, highest = seq[0][0];// highest starts off with the first number in array
            for(i = 0; i < rows; i+=1){
                for(j=0; j < LEN; j++){
                highest =  highest < seq[i][j] ? seq[i][j] : highest;
                /* is seq[0][0] < seq[0][0] if yes then highest = seq[0][0]
                if seq[0][1] < seq[0][0] then higest = seq[0][1]. */
              }
              
          } 
          return highest;
    
    }
    
    
    void InitArray(int (*array)[LEN], int rows){
        int i,j,RandNum;
        for(i = 0; i < rows; i++){
            for(j = 0; j<LEN; j++){
                RandNum = (rand() % 100) + 1;// a random number each time it goes through the loop
                *(*(array + i) + j) = RandNum;
                //array[i][j] = RandNum;
            }
    }//end of outer loop
    return;
    }
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     void Show(int (*array)[LEN], rows)
    In case you're wondering, this was your problem. You cannot just use the , operator and declare multiple of the same type as arguments, as you would normal variables:
    Code:
    int a, b, c;  /* legal */
    
    void foo( int a, b, c ); /* not legal */
    void bar( int a, int b, int c ); /* It must be done this way. Each argument must have its type listed implicitly. */
    Here is how you find the index of a given value:
    Code:
    void foo( int find )
    {
        int array[] = { 1, 2, 5, 77, 32, 14, 68, 100 };
        int x;
        for( x = 0; x < sizeof array / sizeof array[0]; x++ )
            if( array[x] == find )
                return x;
        return -1;
    }
    Simple example, but the index is where in the array you find the element. If we were to pass 77 to this function, it would return 3. I posted the example of what you're looking for on the C++ board the other day, where it returns the index of the largest element.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    void foo( int a, b, c ); /* not legal */
    if you remove the semicolon you can separate variables without declaring their type as in K&R c
    Code:
    int main(argc,argv)
    int argc;
    char **argv;
    {                                   /*legal*/

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The semicolon was to show that that was a function prototype. You would also have had to remove the keyword "int" in there also, to do what you illustrate. The point was, you cannot simply run all your arguments together as they did, as I have shown, which is why they were getting the compiler message.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Where were all of you guys yesterday??? it's like everyone took a vacation.
    Anyway thanks for your help guys. I figured out the problem in regards to the function definition. I also remember that in declaring the function you could do
    Code:
    void function(ch, num)
    char ch; int num;
    or the following
    Code:
    void funct2(arg1,arg2,arg3)
    int arg1, arg2,arg3;


    as to the function to find the index in a array. I had the logic of it figured it out, it was simply a matter of conforming it to C's requirements.
    Code:
    IndiceFinder(int (*array)[LEN],int rows, int num){
    /* logic: loop through a given 2d array and search for a num. If that num is found
    then note the indces. That is its row and clm */
    
        int i,j,HighCount =0;
        for(i = 0; i< rows; i++){
            for(j=0; j< LEN; j++){
                if ( ( array[i][j] == num) ){
                    HighCount+=1;
                    printf("the %s is in row %d clm %d\n\n", (HighCount == 1 ?"value" : "other")
                    ,i,j);
                }    
            }
        }
        
        return ;
    }

    I like your code though Quazah
    Code:
    for( x = 0; x < sizeof array / sizeof array[0]; x++ )
    Never would have thought of that. Guess C comes with experience.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Except your code doesn't note the indicies of where the high value is, nor does it do anything to even check the high value. All it does is increment "HighCount" each time "num" is located in the array. If you're really trying to keep track of the indicies, you'll need one to store the "row", and one to store the "column" that the high value is in. This assumes you're actually trying to find the highest:
    Code:
    if( array[x][y] > array[rowh][colh] )
    {
        rowh = x;
        colh = y;
    }
    This would effectively keep track of the x and y coordinates of the highest value.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by quzah
    Except your code doesn't note the indicies of where the high value is, nor does it do anything to even check the high value. All it does is increment "HighCount" each time "num" is located in the array..
    It actually does this, My code compiles fine and runs. Maybe i'm missing something your saying
    Code:
    /*randomPointers.c
    * this program assigns a 2d array with random numbers and returns the largest 
    * and smallest element in the array. It also takes use of the ternary operator.
    * This program under TrueSoftware(TM) liscence. Feel free to do whatever you want 
    * with it.
    * authour: Caroundw5h 8/21/2004
    *
    */
    /* this can be improeved with pointers somhow, i'm sure. and also having one function
    that tells if the number is High or Low. I know it. too tired right now. */
    
    #include <stdio.h>
    #include <stdlib.h> //for random
    #include <time.h>//for random
    #define LEN 10
    
    void InitArray(int (*)[LEN], int);//Initializes the array 
    void Show(int (*)[LEN], int);/* shows a multidemsional array */
    int Max(int (*)[LEN], int);/* returns the Highest num in a 2d array */
    int Min(int (*)[LEN], int); /* returns Lowest num in given array */
    int IndiceFinder(int [][LEN],int,int); 
    
    int main(void){
        srand(time(NULL));// SEED RANDOM GENERATOR WITH TIME ONCE
        int array[LEN][LEN]; // 10 X 10 array
        int Indice, High, Low;
       
        
        InitArray(array, LEN);
        
        Show(array, LEN);//notice when called called with just name nothing else
        //whether single or multi
        putchar('\n');
        
        High = Max(array,LEN);
        printf("Largest integer in array is %d\n", High );
        IndiceFinder(array,LEN,High);
        
        Low = Min(array, LEN);
        printf("Smallest integer in array is %d\n", Low);
        
        //IndiceFinder(array,LEN,High);
        
        getchar();
        return 0;
    } 
    
    void Show(int (*array)[LEN], int rows){
        
        printf("This array contains:\n\n");
        /* notice the second parameter isn't necessary really but...*/
        int cnt, cnt2;
        for(cnt = 0;cnt < rows; cnt+=1){
            printf("Row %d: ", cnt);
            for(cnt2=0;cnt2 < LEN; cnt2++){
                printf(" %3d", *(*(array + cnt) + cnt2));
            }
          printf("\n");
      }
      
      return;
    }  
    
    int Max(int (*seq)[LEN], int rows){
       
        /* just like int ptr decalration *seq is a pointer to a const int of LEN values. if was 
        single ptr would declare (*ptr) and now right followups, I see the pattern */
        
            int i,j, highest = seq[0][0];// highest starts off with the first number in array
            for(i = 0; i < rows; i+=1){
                for(j=0; j < LEN; j++){
                highest =  highest < seq[i][j] ? seq[i][j] : highest;
                /* is seq[0][0] < seq[0][0] if yes then highest = seq[0][0]
                if seq[0][1] < seq[0][0] then higest = seq[0][1]. */
              }
              
          } 
          return highest;
    
    }
    int Min(int (*seq)[LEN], int rows){
       
        /* just like int ptr decalration *seq is a pointer to a const int of LEN values. if was 
        single ptr would declare (*ptr) and now right followups, I see the pattern */
        
            int i,j, lowest = seq[0][0];// highest starts off with the first number in array
            for(i = 0; i < rows; i+=1){
                for(j=0; j < LEN; j++){
                lowest =  lowest > seq[i][j] ? seq[i][j] : lowest;
                
              }
              
          } 
          return lowest;
    
    }
    
    
    void InitArray(int (*array)[LEN], int rows){
        int i,j,RandNum;
        for(i = 0; i < rows; i++){
            for(j = 0; j<LEN; j++){
                RandNum = (rand() % 100) + 1;// a random number each time it goes through the loop
                *(*(array + i) + j) = RandNum;
                //array[i][j] = RandNum;
            }
    }
    return;
    }    
    
    
    IndiceFinder(int (*array)[LEN],int rows, int num){
    /* logic: loop through a given 2d array and search for a num. If that num is found
    then note the indces. That is its row and clm */
    
        int i,j,HighCount =0;
        for(i = 0; i< rows; i++){
            for(j=0; j< LEN; j++){
                if ( ( array[i][j] == num) ){
                    HighCount+=1;
                    printf("the %s is in row %d clm %d\n\n", (HighCount == 1 ?"value" : "other")
                    ,i,j);
                }    
            }
        }
        
        return ;
    }
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM