program that will calculate and print the sum and average (using an array). Did I code this correctly?

The assignment asked to modify a program that will calculate and print the sum and mean using an array. The program compiles and runs flawlessly. But did I use the array correctly? Im very new to coding just an FYI :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// This program calculates the average of any number of numbers.
// Using the for structure
#include <iostream>
using namespace std;
#include <iostream>
 
int main()
{
   int n, count;
   float x, sum, avg;
 
   sum = 0;
   cout << "How many numbers?  ";
   cin >> n;
   int size= n;
   int array[size];
   for (count=1; count<=n; count++){
       cout << "Enter Number:  ";
         cin >> array[n];
        sum = sum + array[n];
   }  //end for
   cout << "The sum is " << sum << endl;
   avg = sum / n;
   cout << "The average is  " << avg << endl;
   system("pause");
   return 0;   //successful termination

 }//end main 
Last edited on
1) You have included iostream twice.
2) You are using variable length arrays on line 16. This is unofficial extension an may not work on every compiler. It's fine for now, but when you know the right way to do this use it instead.
3) Use doubles instead of floats.
4) Your for statement should be:
for (count=0; count<n; count++)
Array indexes in C++ starts from 0! When ypu write int x[5] valid indexes will be x[0], x[1], x[2], x[3] and x[4].

EDIT:
5) Why you defined a size variable? Why didn't you use n as array size?
Last edited on
for (count=0; count<n; count++)

If the counter starts at 0 when the computer prompts the user "How many numbers?" If I say I want to use 2 numbers, it will prompt for 3 since the counter is starting at 0 instead of 1.
it won't
Did you see that I have used count<n instead of count<=n?
so there will be 2 iteration: for n==0 and n==1;
closed account (18hRX9L8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// you can change numberofitems to whatever you want in line 4
#include <iostream>

#define numberofitems 10

int main(void)
{
     int counter,average,sum=0,items[10];

     std::cout<<"This program gets a list of "<<numberofitems<<" integers and finds their average and sum."<<std::endl;
     
     for(counter=0;counter<numberofitems;counter++)
     {
          std::cout<<"Item #"<<counter+1<<":  ";
          std::cin>>items[counter];
          std::cin.ignore();
          sum+=items[counter];
     }

     average=sum/numberofitems;

     std::cout<<std::endl<<std::endl<<"Sum: "<<sum<<"."<<std::endl;
     std::cout<<"Average: "<<average<<".";
     std::cin.ignore();

     return(0);
}
Last edited on
Although an array is used, here, it isn't actually useful. You could just as well have used a single integer instead.

In any case, each value is stored in the same element, array[n]
But since the array index ranges from 0 to n-1, the element actually used is outside the array and is invalid.


Last edited on
I appreciate the code. usandfriends but the code i have is what i must modify. I was not to write new code but modify the one I had. Mii, I have made the adjustments and it works! Thank you
In my opinion, the best way to modify it, would be to ditch the array completely as there is no benefit in its being there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

int main()
{
   int n, count;
   float sum, avg;

   sum = 0;
   cout << "How many numbers?  ";
   cin >> n;

   int a;
   for (count=1; count<=n; count++) {
        cout << "Enter Number:  ";
        cin >> a;
        sum = sum + a;
   }
   cout << "The sum is " << sum << endl;
   avg = sum / n;
   cout << "The average is  " << avg << endl;

   return 0;
 }


Alternatively, if this is an exercise in the use of arrays, use two stages. In the first, get the input and store it in the array. In the second stage, loop through all the array elements and find the total, before finally getting the average.
Last edited on
closed account (18hRX9L8)
Wow, Chervil, I completely forgot that I wrote a program that finds the sum and average of a list without arrays. Nice thinking!

+1
Lol, you guys are absolutley right. The original code was without an array, the assignment was to modify the program so that an array was used. Although it is useless, it was the point of the assignment. I prob should have let you guys know that before.
Thanks for the clarification. Anyway, in my opinion, you can put the array to work by taking two passes through it, the first to get the user input and the second to get the sum. At least this will pick up any obvious errors, such as using the wrong index value etc.

I've used new and delete here as standard C++ doesn't allow the array size to be a variable (though some compilers have an extension to enable this).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
using namespace std;

int main()
{
   int size;
   double sum = 0;
   double avg;

   cout << "How many numbers?  ";
   cin >> size;

   // dynamically allocate array
   int * array = new int[size];

   // first loop - get the data
   for (int count=0; count<size; count++)
   {
       cout << "Enter Number:  ";
       cin >> array[count];
   }

   // second loop - accumulate the total
   for (int count=0; count<size; count++)
        sum += array[count];

   cout << "The sum is " << sum << endl;
   avg = sum / size;

   cout << "The average is  " << avg << endl;

   // de-allocate the array
   delete [] array;

   return 0;
 }
Topic archived. No new replies allowed.