I need a help please

Hi friends
I need a help in c++
I have to write a program that will find the average in the array x of n
size of array n<=100
Thanks
Then write it. We won't do the homework for you. If you bump into a specific problem regarding the C/C++ language, or maybe that your program doesn't fully work on specification, then yes, we might help. We just won't hand this out for you.
Thanks but i don't want all the program i want one thing only:
How can i write th average for n<=100
i know that ,, for example the average for 3 numbers is:
numb1+numb2+numb3/3
Great, now you are closer. I suppose you know about loops? Do a FOR loop from start to finish, and keep adding numbers into a variable. Once you are done with the loop, divide that number using the total number of elements in the array, which could be 100 or less. I suppose you are using C-styled arrays, so I guess you have the array variable and another variable that knows the total number of valid elements inside the array.

If you have trouble with this explanation, please state where.
Hmm like this:
#include <iostream>
using namespace std;
int main()
{ int m;
double average,total=0;
int n;
int sum;
int x [100];
double average = sum/100;
for (n =0;n<=100;n++)
cout<<"The average is"<<average<<endl;
return 0;
}
?
Nop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main()
{ int m;
double average,total=0;
int n;
int sum;
int x [100];
double average = sum/100; // sum hasnt been defined, remove this line i think
for (n =0;n<=100;n++) // in the loop you should ask for the numbers and add them to the sum
// here you should calculate the average
cout<<"The average is"<<average<<endl;
return 0;
}
Thanks but Can You explain more about:
in the loop you should ask for the numbers and add them to the sum?
Breadman's sample has several errors, most notably the attempt to use sum when sum hasn't been calculated. But he gave you a decent starting point. You must correct the vices in that code, though. The most important one is to always initialize your variables. Line 7 definitely requires changing to int sum = 0; or you'll end up with ridiculous numbers.
Last edited on
Thank You very much..Yes.. i forgat .. sum =0
now I have the program most of it right?Because if somethings are wrong i will try to correct them
#include <iostream>
using namespace std;
int main()
{ int m;
double average,total=0;
int n;
int sum=0;
int x [100];
average =sum/100;
for (n =0;n<=100;n++)
cout<<"The average is"<<average<<endl;
cin>>m;
return 0;
}
Ok.. I read the book an i wrote the new code by a new way:
#include <iostream>
using namespace std;
int main()
{
int m;
int sum =0;
int average =0;
int array [100] = {1:100}; HERE PLEASE HOW CAN I WRITE NUMBERS FROM 1 TO 100? BECAUSE IF I WRITE IT LIKE THIS { 1,2,3,4,5,6,ETS}IT WILL TAKE MANY NUMBERS...
for ( int n=0 ; n<100; i++)
sum +array [n];
average = sum/100;
cout<<"average:"<<average;
cin>>m;
return 0;
}


Uh... just intialize the array with a for loop.
like this?
because it gave me wrong
#include <iostream>
using namespace std;
int main()
{
int m;
int sum =0;
int average =0;
int array [100] = for {1:100};
for ( int n=0 ; n<100; i++)
sum +array [n];
average = sum/100;
cout<<"average:"<<average;
cin>>m;
return 0;
}


Topic archived. No new replies allowed.