-1

The questions that I'm trying to solve:

  • Ask the user for five numbers and store it in an array.
  • Determine the highest number entered and the average of the array
  • Will be using a for loop to solve this problem.

The code that I wrote to solve this problem but I don't know why it's not working. Can someone please walk me through what I'm doing wrong?

#include <stdio.h>

  int main()
   {
     int i, n;
     float arr[5], sum = 0, average;

     printf("Please enter five numbers, separated by spaces!\n");
     scanf("%d", &n);

     //Stores numbers entered into an array
     for(i = 0; i < n; i++)
     {
        printf("%d: ", i++);
        scanf("%d", &arr[0]);


        for(i = 1; i < n; i++)
        {
            if(arr[0] < arr[i])
                arr[0] = arr[i];
        }
        printf("The highest of the five numbers is %d\n", arr[0]);

        for(i = 0; i < n; i++)
        {
            printf("%d. Enter number: ", i+1);
            scanf("%f", &arr[i]);
            sum += arr[i];
        }

        average = sum / n;
        printf("The average of the five numbers is %f\n", average);
      }
    }    
S.I.
  • 3,250
  • 12
  • 48
  • 77
Jay Lee
  • 1
  • 1
  • Welcome to Stack Overflow. Please take the [tour] and read [ask]. Come on, just putting code and asking a question in the comments of code is below any standard. Please edit your question in a readable way. Show us that you at least care to take time to properly ask a question. – Pablo Jan 29 '18 at 03:19
  • 1
    Yes, if nothing else, please explain precisely how your code is failing. – Stephen Docy Jan 29 '18 at 03:24
  • Duplicate of [Average, max, and min program in C](https://stackoverflow.com/questions/20769834/average-max-and-min-program-in-c) – David C. Rankin Jan 29 '18 at 05:23
  • Possible duplicate of [Average, max, and min program in C](https://stackoverflow.com/questions/20769834/average-max-and-min-program-in-c) – Gerhard Jan 29 '18 at 07:43

3 Answers3

1
#include <stdio.h>

  int main()
   {
     int i, n;
     float sum = 0, average;

     printf("Please enter five numbers, separated by spaces!\n");
     scanf("%d", &n);

     float arr[n];

     // no need to do i++ inside
     //instead of saving all the values in arr[0], save it in arr[i]
     //for loop needs to be closed here itself,which you had not done
     for(i = 0; i < n; i++)
     {
       // printf("%d: ", i++); //no need to do this
        scanf("%f", &arr[i]);
     }
    //initialize a max variable equal to first element of array
     float max=arr[0];

      //compare the max element with rest of the elements of the array and update it as you get any greater element than it
        for(i = 1; i < n; i++)
        {
            if(max < arr[i])
                max = arr[i];
        }

        //use %f format specifier for float
        printf("The highest of the five numbers is %f\n", max);

        for(i = 0; i < n; i++)
        {
            //no need to do here what u did since the value is already stored in array
    //this for loop sums all the values present in array
            sum += arr[i];
        }

       //takes out average and prints it
        average = sum / n;
        printf("The average of the five numbers is %f\n", average);

    }    

i have pointed out your mistakes in comments. hope it helps!

anupam691997
  • 310
  • 2
  • 8
  • You should also point out, then if the user enter 6 or more, the code will produce a buffer overflow. – Pablo Jan 29 '18 at 04:41
0

Here's what you can do,

  • You can get your array size from the user and use a variable in & for loops which would help to input the array elements. That's what you are doing wrong mainly.

Here's the correct code,

#include <stdio.h>
int main()
   {
     int i, arrsize; //arrsize variable declared to store array size
     float arr[5], sum = 0, average;
     printf("Enter the size of the array : ");
     scanf("%d",&arrsize);

 printf("Please enter %d numbers, separated by spaces!\n",arrsize);

 for(i = 0; i < arrsize; i++)
 {
    scanf("%f", &arr[i]);
 }
 float max=arr[0];

    for(i = 1; i < arrsize; i++)
    {
        if(max < arr[i])
            max = arr[i];
    }
    printf("The highest of the five numbers is %f\n", max);

    for(i = 0; i < arrsize; i++)
    {
        sum += arr[i];
    }
    average = sum / arrsize;
    printf("The average of the five numbers is %f\n", average);

}

There's another mistake you did in the first scanf of your code, can you tell what exactly you wanted to input using that statement? I mean if it an array which it is, you obviously need loop to get the input. So that first scanf is worthless(of your code).

Hope it helps. I'll be looking forward to your reply.

Happy Coding.

Hasan
  • 247
  • 7
  • 22
0

Try this code . I am also attach the output of the code.

#include <stdio.h>

  int main()
   {
     int i, n=5;
     float arr[5], sum = 0.0, average,max;


     //Stores numbers entered into an array
     for(i = 0; i < n; i++)
     {
        printf("Enter value # %d :", i+1);
        scanf("%f", &arr[i]);
     }

     max=arr[0];
        for(i = 1; i < n; i++)
        {
            if(max < arr[i])
                max = arr[i];
        }
        printf("The highest of the five numbers is %f\n", max);

        for(i = 0; i < n; i++)
        {

            sum += arr[i];
        }

        average = sum / n;
        printf("The average of the five numbers is %f\n", average);

    }    

enter image description here

Usman
  • 1,983
  • 15
  • 28
  • Don't post pictures of text output, post the text itself properly indented by 4-spaces. (it adds up, text ~250-bytes, image 2500-bytes) – David C. Rankin Jan 29 '18 at 05:21