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 :)
// This program calculates the average of any number of numbers.
// Using the for structure
#include <iostream>
usingnamespace 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
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?
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.
// 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);
}
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.
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
#include <iostream>
usingnamespace 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.
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).
#include <iostream>
usingnamespace std;
int main()
{
int size;
double sum = 0;
double avg;
cout << "How many numbers? ";
cin >> size;
// dynamically allocate array
int * array = newint[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;
}