Thread: Help with arrays and max value

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    28

    Help with arrays and max value

    Hey, me and my friend are trying to write a programme that uses an array and gets inputs from the keyboard and puts the value into the array and works on the sum/average/max number, while just using the standard stdio.h libary.

    We have successfully written all the code for the sum and average but are having a bit of trouble with the max number code, can anyone help?

    Below is the code, you can see we have attempted it xD

    Code:
    #include <stdio.h>
    
    void print_arr(int myArray[], int elements);
    int sum_arr(int myArray[], int elements);
    
    int main(void)
    {
    
    
      int numbers[12];
      int count = 12;
      long sumtotal = 0L;
      float average = 0.0f;
      int sum;
      int maxValue( int numbers[12], int size);
    {
      printf("\nEnter the 12 numbers:\n");
      int i;
    
      for(i = 0; i < count; i ++)
      {
        printf("%2d> ",i+1);
        scanf("%d", &numbers[i]);
        sumtotal += numbers[i];
      }
    }
    sum = sum_arr(numbers,12);
    printf("The sum of the array is : %d\n",sum);
    {
      average = (float)sumtotal/count;
    
      printf("\nAverage of the ten numbers entered is: %.2f\n", average);
    }
    
    {
    
    	{
    	    int i, maxValue;
    	    maxValue=numbers[12];
    
    
    	    for (i=0;i;)
    	        {
    	        if (numbers[i]>maxValue)
    	        maxValue=numbers[i];
    	        }
    	        printf("The max value is: %d\n", maxValue);
    
    	}
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    maxValue=numbers[12];
    You are reading past the end of the array here. numbers[12] is undefined.
    Code:
    for (i=0;i;)
    That's not the correct way to iterate over the array. You did it correctly up above in your code, why are you doing it wrong here?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    oh yeah lol, didnt realise the
    Code:
    for (i=0;i;)

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    also did you mean put it something like this:
    Code:
      int numbers[12];
      int count = 12;
      long sumtotal = 0L;
      float average = 0.0f;
      int sum;
       int i2 ;
       int maxValue;
      int i;
      printf("\nEnter the 12 numbers:\n");
    
    
      for(i = 0; i < count; i ++)
      {
        printf("%2d> ",i+1);
        scanf("%d", &numbers[i]);
        sumtotal += numbers[i];
      }
    
    sum = sum_arr(numbers,12);
    printf("The sum of the array is : %d\n",sum);
    
      average = (float)sumtotal/count;
    
      printf("\nAverage of the numbers entered is: %.2f\n", average);
    
    	    maxValue=numbers[12];
    
    
    	    for (i2 =0; i2 ;  )
    	        {
    	        if (numbers[i2]>maxValue)
    	        maxValue=numbers[i2];
    	        }
    	        printf("The max value is: %d\n", maxValue);
    as in removing most of the { } so they all in one?
    if you did mean that it still doesnt work

    also ignore the for loop, this was an old version that i was testing and just realised it isnt set right xD

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by trueman1991 View Post
    also did you mean put it something like this:
    Code:
      int numbers[12];
      int count = 12;
      long sumtotal = 0L;
      float average = 0.0f;
      int sum;
       int i2 ;
       int maxValue;
      int i;
      printf("\nEnter the 12 numbers:\n");
    
    
      for(i = 0; i < count; i ++)
      {
        printf("%2d> ",i+1);
        scanf("%d", &numbers[i]);
        sumtotal += numbers[i];
      }
    
    sum = sum_arr(numbers,12);
    printf("The sum of the array is : %d\n",sum);
    
      average = (float)sumtotal/count;
    
      printf("\nAverage of the numbers entered is: %.2f\n", average);
    
    	    maxValue=numbers[12];
    
    
    	    for (i2 =0; i2 ;  )
    	        {
    	        if (numbers[i2]>maxValue)
    	        maxValue=numbers[i2];
    	        }
    	        printf("The max value is: %d\n", maxValue);
    as in removing most of the { } so they all in one?
    if you did mean that it still doesnt work

    also ignore the for loop, this was an old version that i was testing and just realised it isnt set right xD
    If that's an old version, then post the new version. I can't tell you why it's not working if I can't even see the same code you're using.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    that is the code im using, im just updating a different copy with the stuff i know is working, e.g. basicly all of it apart from working out the max value

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by trueman1991 View Post
    that is the code im using, im just updating a different copy with the stuff i know is working, e.g. basicly all of it apart from working out the max value
    In that case, read my first post again. You didn't address either of my points. The two lines of code that I quoted are still the same...
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    i changed it abit after my mate said a few things and this is the code i have now:
    Code:
      int numbers[12];
      int count = 12;
      long sumtotal = 0L;
      float average = 0.0f;
      int sum;
      int i;
      int max = numbers[0];
      int min = numbers[0];
      int i2;
      printf("\nEnter the 12 numbers:\n");
    
    
      for(i = 0; i < count; i ++)
      {
        printf("%2d> ",i+1);
        scanf("%d", &numbers[i]);
        sumtotal += numbers[i];
      }
    
    sum = sum_arr(numbers,12);
    printf("The sum of the array is : %d\n",sum);
    
      average = (float)sumtotal/count;
    
      printf("\nAverage of the numbers entered is: %.2f\n", average);
    
    
    
      for (i2 = 0; i2 < numbers[12]; i2++)
        {
          if (numbers[i2] > max)
            {
              max = numbers[i2];
            }
          else if (numbers[i2] < min)
            {
              min = numbers[i2];
            }
        }
      printf ("Maximum element in an array : %d\n", max);
      printf ("Minimum element in an array : %d\n", min);
    it compiles, however when i run it, it comes up with an error and closes the programme, the error is the standard this programme is not responding error

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When does the error occur? My guess is in sum_arr, since you haven't shown us that. For that matter, the top and bottom of your program seem to be missing...


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

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    28
    this is the whole programme:
    Code:
    #include <stdio.h>
    
    void print_arr(int myArray[], int elements);
    int sum_arr(int myArray[], int elements);
    
    int main(void)
    
    {
      int numbers[12];
      int count = 12;
      long sumtotal = 0L;
      float average = 0.0f;
      int sum;
      int i;
      int max = numbers[0];
      int min = numbers[0];
      int i2;
      printf("\nEnter the 12 numbers:\n");
    
    
      for(i = 0; i < count; i ++)
      {
        printf("%2d> ",i+1);
        scanf("%d", &numbers[i]);
        sumtotal += numbers[i];
      }
    
    sum = sum_arr(numbers,12);
    printf("The sum of the array is : %d\n",sum);
    
      average = (float)sumtotal/count;
    
      printf("\nAverage of the numbers entered is: %.2f\n", average);
    
    
    
      for (i2 = 0; i2 < numbers[12]; i2++)
        {
          if (numbers[i2] > max)
            {
              max = numbers[i2];
            }
          else if (numbers[i2] < min)
            {
              min = numbers[i2];
            }
        }
      printf ("Maximum element in an array : %d\n", max);
      printf ("Minimum element in an array : %d\n", min);
    
    
    }
    void print_arr(int numbers[12], int elements)
    {
    int i;
    for(i = 0;i < elements;i++)
    {
    printf("%d ",numbers[i]);
    }
    printf("\n");
    }
    
    int sum_arr(int numbers[12], int elements)
    {
    int i, sum = 0;
    for(i = 0;i < elements;i++)
    {
    sum = sum + numbers[i];
    }
    return(sum);
    }
    and nah i dont think it would be that, this is because the programme runs fine without the following code:
    Code:
      for (i2 = 0; i2 < numbers[12]; i2++)
        {
          if (numbers[i2] > max)
            {
              max = numbers[i2];
            }
          else if (numbers[i2] < min)
            {
              min = numbers[i2];
            }
        }
      printf ("Maximum element in an array : %d\n", max);
      printf ("Minimum element in an array : %d\n", min);

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for (i2 = 0; i2 < numbers[12]; i2++)
    Any particular reason you're doing that?


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

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Here is the soulution:
    ...dont use numbers[12] as a limit, you can aswell use count, as u did before and initial max and min must be put after you read the numbers in the program...

    Code:
    #include <stdio.h>
    
    void print_arr(int myArray[], int elements);
    int sum_arr(int myArray[], int elements);
    
    int main(void)
    
    {
      int numbers[12];
      int count = 12;
      long sumtotal = 0L;
      float average = 0.0f;
      int sum;
      int i;
      printf("\nEnter the 12 numbers:\n");
    
    
      for(i = 0; i < count; i ++)
      {
        printf("%2d> ",i+1);
        scanf("%d", &numbers[i]);
        sumtotal += numbers[i];
      }
    
    sum = sum_arr(numbers,12);
    printf("The sum of the array is : %d\n",sum);
    
      average = (float)sumtotal/count;
    
      printf("\nAverage of the numbers entered is: %.2f\n", average);
    
    
      int max = numbers[0];
      int min = numbers[0];
      for (i = 0; i < count; i++)
         {
          if (numbers[i] > max)
            {
              max = numbers[i];
            }
          else if (numbers[i] < min)
            {
              min = numbers[i];
            }
        }
      printf ("Maximum element in an array : %d\n", max);
      printf ("Minimum element in an array : %d\n", min);
    
    
    }
    void print_arr(int numbers[12], int elements)
    {
    int i;
    for(i = 0;i < elements;i++)
    {
    printf("%d ",numbers[i]);
    }
    printf("\n");
    }
    
    int sum_arr(int numbers[12], int elements)
    {
    int i, sum = 0;
    for(i = 0;i < elements;i++)
    {
    sum = sum + numbers[i];
    }
    return(sum);
    }

    I have a similar problem, finding out max and min from the list in a file, in case somebody has any idea:

    Determing the maximum number (coordinate) from imput file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-26-2009, 02:46 PM
  2. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Using pointers instead of subscripts for arrays
    By Sir Andus in forum C Programming
    Replies: 7
    Last Post: 11-09-2006, 10:59 AM
  5. Array's Help
    By bmx4christ in forum C Programming
    Replies: 15
    Last Post: 12-08-2003, 12:40 PM