Thread: question about an array

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    question about an array

    I'm writing a program that prompts for ten integers, then stores these intergers in an array. Then the program computes and stores the sum and the largest and smallest of these integers. The program then outputs the ten integers but in the reverse order they were entered and on a separate line, the sum and the max and min of these integers. I'm having trouble compiling this program and I was wondering if anyone had any advice? My first question is, can you store an array within the prototype of the function? Here is my code:

    Code:
    /*
         Filename:     sum.c     
         Description:  This program computes the sum, the largest and smallest of these integers.
    */
    
    #include <stdio.h>
    
       int findmax(int number [10]);
       int findmin(int number [10]);
    
         int main()
    
      {
    
         int number[10];
         int i,sum;
         char answer [2];
    
      do
    
         {
    
          printf ("Enter 10 non-negative numbers:\n");
    
             for (i=0;i<10;i++)
    
                {
    
                  scanf ("%d",number[i]);
                  sum=sum + number[i];
    
                }
    
              for (i=9;i>0;i--)
    
                {
    
                  printf ("%d\n",number[i]);
    
                  printf ("The sum is %d\n",sum);
    
                  printf ("The largest integer entered was %d\n",findmax(number));
    
                  printf ("The smallest integer entered was %d\n",findmin(number));
    
                }
    
          }
    
             while((answer[0]=='y')||(answer[0]=='Y'));
    
    
      return 0;
    
       }
    
         int findmax (int number[10])
    
           {
    
             int max,i;
             max=number[0];
    
                for (i=0;i<10;i++)
    
                  {
    
                    if (max<number[i])
                       max=number[i];
    
                  }
    
            }
    
             return max;
    
          }
    
         int findmin (int number[10])
    
           {
    
             int min,i;
             min=number[0];
    
                for (i=0;i<10;i++)
    
                  {
    
                    if (min>number[i])
                       max=number[i];
    
                  }
    
            }
    
             return min;
    
          }

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    I didn't read through all the code but saw one thing. In the prototype of the functions it should be declared like this:
    Code:
    int findmax(int[]);
    Should be used like this:
    Code:
    findmax(num);
    And should be defined like this:
    Code:
    int findmax(int num[])
    {
        //Function code goes here
    }
    The same with the other function. The variable num, of course, should be an integer array. What sections of the code are giving you errors?
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Code:
         int findmax (int number[10])
    
           {
    
             int max,i;
             max=number[0];
    
                for (i=0;i<10;i++)
    
                  {
    
                    if (max<number[i])
                       max=number[i];
    
                  }
    
            }
    
             return max;
    
          }
    you do realize your trying to return something outside of the function, correct? same w/your other function.

    also - you didn't declare 'max' in your findmin function. but why would you be using max at all?
    Code:
    if (min>number[i])
          max=number[i];
    Last edited by willc0de4food; 10-17-2005 at 06:20 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    I changed my code and I get the following erros:

    sum.c: In function `main':
    sum.c:30: warning: format argument is not a pointer (arg 2)
    sum.c:43: warning: passing arg 1 of `findmax' makes integer from pointer without a cast
    sum.c:45: warning: passing arg 1 of `findmin' makes integer from pointer without a cast
    sum.c: At top level:
    sum.c:61: error: conflicting types for 'findmax'
    sum.c:9: error: previous declaration of 'findmax' was here
    sum.c:61: error: conflicting types for 'findmax'
    sum.c:9: error: previous declaration of 'findmax' was here
    sum.c: In function `findmax':
    sum.c:62: error: parse error before "max"
    sum.c:64: error: old-style parameter declarations in prototyped function definition
    sum.c:68: error: `max' undeclared (first use in this function)
    sum.c:68: error: (Each undeclared identifier is reported only once
    sum.c:68: error: for each function it appears in.)
    sum.c:68: error: `i' undeclared (first use in this function)
    sum.c: At top level:
    sum.c:74: error: parse error before "return"
    sum.c:81: error: conflicting types for 'findmin'
    sum.c:10: error: previous declaration of 'findmin' was here
    sum.c:81: error: conflicting types for 'findmin'
    sum.c:10: error: previous declaration of 'findmin' was here
    sum.c: In function `findmin':
    sum.c:82: error: parse error before "min"
    sum.c:84: error: old-style parameter declarations in prototyped function definition
    sum.c:88: error: `min' undeclared (first use in this function)
    sum.c:88: error: `i' undeclared (first use in this function)
    sum.c: At top level:
    sum.c:94: error: parse error before "return"

    Here is the modified code:

    Code:
    /*
         Filename:     sum.c     
         Description:  This program computes the sum, the largest and smallest of these integers.
         Author:       Scott Manley
    */
    
    #include <stdio.h>
    
       int findmax(int number);
       int findmin(int number);
    
         int main()
    
      {
    
         int number[10];
         int i,sum;
         char answer [2];
    
      do
    
         {
    
          printf ("Enter 10 non-negative numbers:\n");
    
             for (i=0;i<10;i++)
    
                {
    
                  scanf ("%d",number[i]);
                  sum=sum + number[i];
    
                }
    
              for (i=9;i>0;i--)
    
                {
    
                  printf ("%d\n",number[i]);
    
                  printf ("The sum is %d\n",sum);
    
                  printf ("The largest integer entered was %d\n",findmax(number));
    
                  printf ("The smallest integer entered was %d\n",findmin(number));
    
                }
    
          }
    
             while((answer[0]=='y')||(answer[0]=='Y'));
    
    
      return 0;
    
       }
    
         int findmax (int number[10])
    
    
             int max,i;
             max=number[0];
    
                for (i=0;i<10;i++)
    
                  {
    
                    if (max<number[i])
                       max=number[i];
    
                  }
    
    
             return max;
    
          }
    
         int findmin (int number[10])
    
    
             int min,i;
             min=number[0];
    
                for (i=0;i<10;i++)
    
                  {
    
                    if (min>number[i])
                       min=number[i];
    
                  }
    
    
             return min;
    
          }

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    its really weird you dont even know where to put braces ..what is function prototype and you are going to write programs.
    I think you should
    NOT MIX BEER AND CODING
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  6. #6
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    These:
    Code:
       int findmax(int number);
       int findmin(int number);
    Should be:
    Code:
       int findmax(int number[]);
       int findmin(int number[]);
    That cleared up quite a bit of those errors. Now we have those brackets cbastard was talking about. See this part where you are defining those functions:
    Code:
         int findmax (int number[10])
    .....
    And
    Code:
         int findmin (int number[10])
    .....
    You left off the opening brackets. Just add brackets like so:
    Code:
         int findmin (int number[10])
    {
    .....
    }
    And like:
    Code:
         int findmax (int number[10])
    {
    .....
    }
    I noticed that you already had the closing bracket so just add the opening one, and then I think it should compile, although I didn't look at the code that wasn't giving you errors.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  7. #7
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    If you aren't using a text editor that does syntax highlighting and bracket matching, you should be, it will save you a lot of problems. For instance, if I put an extra closing brace in gvim (my preferred editor), it will highlight it in red, letting me know straight away that I've done something wrong. When I type a closing brace, it will briefly highlight the matching opening brace. If I have an extra opening brace, the indentation will be off, letting me know I've done something wrong.

    If you use a decent editor, a substantial number of simple syntax errors never make it to the compiler.

    Also, these sorts of problems can often be avoided by clear and consistent indentation (something your code at present does not have).

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    OK...first thing...cbastard...i don't drink. Maybe I should start though...maybe I'll write better code. So thanks for the advice. I've come to realize us newbies are susceptible to punishment. Second, I finally was able to compile the program but now I get a runtime error that says "Segmentation fault (core dumped). Here is the new code:

    Code:
    #include <stdio.h>
    
       int findmax(int number []);
       int findmin(int number []);
    
         int main()
    
      {
    
         int number[10];
         int i,sum;
         char answer [2];
    
      do
    
         {
    
          printf ("Enter 10 non-negative numbers:\n");
    
             for (i=0;i<10;i++)
    
                {
    
                  scanf ("%d",number[i]);
                  sum=sum + number[i];
    
                }
    
              for (i=9;i>0;i--)
    
                {
    
                  printf ("%d\n",number[i]);
    
                  printf ("The sum is %d\n",sum);
    
                  printf ("The largest integer entered was %d\n",findmax(number));
    
                  printf ("The smallest integer entered was %d\n",findmin(number));
    
                }
    
          }
    
             while((answer[0]=='y')||(answer[0]=='Y'));
    
    
      return 0;
    
       }
    
         int findmax (int number[10])
    
         {
    
             int max,i;
             max=number[0];
    
                for (i=0;i<10;i++)
    
                  {
    
                    if (max>number[i])
                       max=number[i];
    
                  }
    
    
             return max;
    
          }
    
         int findmin (int number[10])
    
        {
    
             int min,i;
             min=number[0];
    
                for (i=0;i<10;i++)
    
                  {
    
                    if (min<number[i])
                       min=number[i];
    
                  }
    
    
             return min;
    
          }

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well u get the segmentation fault b'code of this
    Code:
     scanf ("%d",&number[i]);
    u should specify the &operator which basically mean address of. and there where few logical error which i have highlighted. go through the code u might get to know what mistake u have done. and more thing to be pointed u programe is that aligned properly u have to that first. well, there u go

    Code:
    #include <stdio.h>
    
       int findmax(int number []);
       int findmin(int number []);
    
         int main()
    
      {
    
         int number[10];
         int i,sum=0;
         char answer [2];  // no need of having a string a single chat is enough
    
      do
    
         {
    
          printf ("Enter 10 non-negative numbers:\n");
    
             for (i=0;i<10;i++)
    
                {
    
                  scanf ("%d",&number[i]);   // need to specilfy the address of operator
                  sum=sum + number[i];
    
                }
    
              for (i=9;i>0;i--)
    
                {  // u dont need this braces here
    
                  printf ("%d\n",number[i]);
    
                  printf ("The sum is %d\n",sum);
    
                  printf ("The largest integer entered was %d\n",findmax(number));
    
                  printf ("The smallest integer entered was %d\n",findmin(number));
    
                } // no need
                        // need to read a character here
             
          }
             
             while((answer[0]=='y')||(answer[0]=='Y'));
             
    
    
      return 0;
    
       }
    
         int findmax (int number[10])
    
         {
    
             int max,i;
             max=number[0];
    
                for (i=0;i<10;i++)
    
                  {
    
                    if (max>number[i]) /// should be <
                       max=number[i];
    
                  }
    
    
             return max;
    
          }
    
         int findmin (int number[10])
    
        {
    
             int min,i;
             min=number[0];
    
                for (i=0;i<10;i++)
    
                  {
    
                    if (min<number[i])  // should be >
                       min=number[i];
    
                  }
    
    
             return min;
    
          }
    ssharish2005
    Last edited by ssharish2005; 10-18-2005 at 07:46 PM.

  10. #10
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Quote Originally Posted by s_ny33
    OK...first thing...cbastard...i don't drink. Maybe I should start though...maybe I'll write better code. So thanks for the advice. I've come to realize us newbies are susceptible to punishment. .....
    i know exactly how you feel. your unsure about what your doing, testing the water, trying to get a feel for it...and then someone criticizes you like you should know what your doing. lol but dont worry dude, this "punishment" wont seem so much like punishment when you're more accustomed to posting code and getting help. just dont take anything personal.


    on the way of your code though, i'm curious as to why you dont put spaces anywhere.? is it that your unsure if its legal? or you just dont prefer to? personally, if i had a for() statement, this is how i would & would not code it:
    Code:
    would
    for (i = 0; i < 10; i++)
    {
        ...
    }
    
    would not.
    for(i=0;i<10;i++)
    {
    ...
    }
    but thats just me. i find the code more readable, but go w/whatever suits you.
    Registered Linux User #380033. Be counted: http://counter.li.org

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    you have actually made mistakes of putting unnecessary braces .

    first of all in the functions findmax and findmin u have put extra braces.
    secondly in function findmin u have declared variable min and using max for comparison
    In the main function while u are printing numbers in reverse order u are also repeatedly printing the sum,largest and smallest
    number statements again and again by putting inside the for loop.

    you can use either int/void main().

    your char answer was not intially set as 'y' neither are u asking the user whether he is willing to use the program again.u als need only 1 char not an array
    The variable sum was not intialized to zero
    in the scanf statement u missed & operator therefore numbers were not stored at the variable

    some xtra features can be that when user wishes to quit u can use exit(0) function when he presses 'n'
    u should also use clrscr() for wiping out previous contents
    the new program is this:

    Code:
    /*
         Filename:     sum.c
         Description:  This program computes the sum, the largest and smallest of these integers.
    */
    
    #include <stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
       int findmax(int number [10]);
       int findmin(int number [10]);
    
         void main()
    
      {
    
         int number[10];
         int i,sum=0;
         char answer='y' ;
         clrscr();
      while(answer=='y')
    
         {
    
          printf ("Enter 10 non-negative numbers:\n");
    
    	 for (i=0;i<10;i++)
    
    	    {
    
    	      scanf ("%d",&number[i]);
    	      sum=sum + number[i];
    
    	    }
    	  printf("\n\n");
    	  for (i=9;i>=0;i--)
    
    	    {
    
    	      printf ("%d\n",number[i]);
    	    }
    	      printf ("The sum is %d\n",sum);
    
    	      printf ("The largest integer entered was %d\n",findmax(number));
    
    
    	      printf ("The smallest integer entered was %d\n",findmin(number));
    
    
    	     printf("You want to enter another set of numbers\n");
    	    scanf("%c",&answer);
    	    if(answer=='n')
    	     exit(0);
           }
    
    
    
    
    
    
      getch();
    
       }
    
         int findmax (int number[10])
    
           {
    
    	 int max,i;
    	 max=number[0];
    
    	    for (i=0;i<10;i++)
    
    	      {
    
    		if (max<number[i])
    		   max=number[i];
    
    	      }
    
    
    
    	 return max;
    
          }
    
         int findmin (int number[10])
    
           {
    
    	 int min,i;
    	 min=number[0];
    
    	    for (i=0;i<10;i++)
    
    	      {
    
    		if (min>number[i])
    		   min=number[i];
    
    	      }
    
    
    
    	 return min;
    
          }

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Thanks for all your advice. I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM