English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I've tried everything I know and I don't know what to do now. I'm using a random number generator and I need helping getting the average of the numbers the generator makes plz help!


#include
#include
#include
#include

int main (void)
{
int n;
int repeat;
int random_integer;
float dem;
ifstream infile;


infile.open("C:\Numbers.DAT", ios::in);
if (infile)
{
infile >> repeat;
infile.ignore(80, '\n');
for(int x = 1; x <= repeat; x++)
{
infile >> random_integer;
infile.ignore(80, '\n');
cout << random_integer << endl;
dem = random_integer%2;

2007-02-03 15:33:18 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

///Hope the following helps

///average of n no.s= (sum of n.nos) / n
///Add a new variable
float sum=0.0f;

for(int x = 1; x <= repeat; x++)
{
infile >> random_integer;
infile.ignore(80, '\n');
cout << random_integer << endl;
sum += random_integer;
}

///find the average
dem = sum / repeat;

2007-02-03 15:46:04 · answer #1 · answered by ans_bros 1 · 0 0

You could declare an array enter the random numbers into the array. Total the values and divide by array length -1. The code you display is incomplete to offer a better answer entire code file should be shown.
To generate "random" number:
random_integer = rand() % 100;
Generates a psuedo random number between 0 & 99

2007-02-03 23:58:19 · answer #2 · answered by Tempest 2 · 0 0

One really easy way is to stick all the numbers in a vector, use accumulate on the vector, and divide by the vector length to get your average (avg = sum/num of elements, right).

You haven't even posted anything near compiling code, so I'm not going to waste my time pointing out problems in your code. I'll just give you a generic method, and you can go ahead and try to fix things.

#include
#include
using namespace std;

int main()
{
vector elements;
/*
Some code and function calls that populate elements
*/
float avg = accumulate(elements.begin(), elements.end(), 0.f)/vector.size();
return 0;
}

2007-02-03 23:49:28 · answer #3 · answered by csanon 6 · 0 0

Do you know how to use a debugger?
I suggest you learn how. Then, you can step through code, set breakpoints and check the values of the items in question.
At least that would point to a more specific problem.

2007-02-03 23:47:48 · answer #4 · answered by Anonymous · 0 0

try typing = average then type the numbers.

2007-02-03 23:39:38 · answer #5 · answered by Inugurl3 4 · 0 0

fedest.com, questions and answers