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 %26lt;iostream.h%26gt;
#include %26lt;fstream.h%26gt; 
#include %26lt;math.h%26gt;   
#include %26lt;iomanip.h%26gt; 
int main (void) 
{
     int n;
     int repeat; 
     int random_integer;
     float dem;
     ifstream infile;
     infile.open("C:\Numbers.DAT", ios::in);
     if (infile)
     {
                infile %26gt;%26gt; repeat;
                infile.ignore(80, '\n');
                for(int x = 1; x %26lt;= repeat; x++)
                {
                        infile %26gt;%26gt; random_integer;
                        infile.ignore(80, '\n');
                        cout %26lt;%26lt; random_integer %26lt;%26lt; endl;
                        dem = random_integer%2;
C++ Help Again =( !?
///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 %26lt;= repeat; x++)
{
infile %26gt;%26gt; random_integer;
infile.ignore(80, '\n');
cout %26lt;%26lt; random_integer %26lt;%26lt; endl;
sum += random_integer;
}
///find the average
dem = sum / repeat;
Reply:try typing = average then type the numbers.
Reply: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.
Reply: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 %26amp; 99
Reply: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 %26lt;algorithm%26gt;
#include %26lt;vector%26gt;
using namespace std;
int main()
{
    vector%26lt;int%26gt; elements;
    /*
    Some code and function calls that populate elements
    */
    float avg = accumulate(elements.begin(), elements.end(), 0.f)/vector.size();
    return 0;
}
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment