Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,300 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,025 people online right now. Registration is fast and FREE... Join Now!




help needed with reading data from a file and do some math calcuations

 
Reply to this topicStart new topic

help needed with reading data from a file and do some math calcuations

coni
post 12 Mar, 2008 - 06:39 AM
Post #1


New D.I.C Head

*
Joined: 12 Mar, 2008
Posts: 2

CODE

// Program Assignment #3
// Program will display a 7 option switch/case menu
// each selection has functions
#include <iostream>
#include <cstdlib> // so the program can see rand()
#include <fstream>
using namespace std; // So the program can see cout and endl

// Used Functions starts here..

void generate() // functiong for generation of numbers.
{
ofstream outputfile;

int x[100],input,y;
cout <<"Please enter a number between 10 and 100 to generate numbers.\n";
        cin >>input;
while(input <10 || input >100)
    {
    cout <<"Please enter a number between 10 and 100 to generate numbers.\n";
    cin >>input;
    }
outputfile.open("num.dat");
for (int y=0;y<input;y++)
{
    x[y]=rand();
    outputfile << x[y]<<endl;
}
outputfile.close();
}

int read_data(char n[],int y[]) // function used for reading data.
{
    int i=0;
    ifstream infile;
    infile.open(n);
    while (!infile.eof())
    {
        infile>>y[i];
        i++;
    }
    return i-1;
}

void display_data() // display data
{
    int x[100],y,i;
    y= read_data("num.dat",x);
    if (y<=0)
    {
        cout << "Reading from file failed.\n";
    }
    else
        for(i=0;i<y;i++)
            cout<<x[i]<<" "<<endl;
}


void sum_data() // sum the numbers
{
    int x[100],y,i,sum;
    y= read_data("num.dat",x);
    if (y<=0)
    {
        cout << "Reading from file failed.\n";
    }
    else
    for(i=0;i<y;i++)
        sum=sum+x[i];
    cout<< "Sum of the generated numbers is " << sum << endl;
}

void ave_data() // ave the numbers
{
    int x[100],y,i;
    double sum,ave;
    y= read_data("num.dat",x);
    if (y<=0)
    {
        cout << "Reading from file failed.\n";
    }
    else
    for(i=0;i<y;i++)
    ave=(ave+x[i])/y;
    cout<< "Average of the generated numbers is " << ave << endl;
}
void findstddev()// std deviation
{
    int x[100],y,i;
    double stddev,sum,ave;
    y= read_data("num.dat",x);
    if (y<=0)
    {
        cout << "Reading from file failed.\n";
    }
    else
    for(i=0;i<y;i++)
    {
     ave=(ave+x[i])/y;
     sum=sum+x[i];
    }
stddev=(((sum+(ave*ave))/y))/(((sum+(ave*ave))/y));
cout << "Standard deviation of the generated numbers is " << stddev << endl;
}

void finddivison() // function finds numbers divisiable by 4 & 8
{
    int x[100],y,i;
    y= read_data("num.dat",x);
    if (y<=0)
    {
        cout << "Reading from file failed.\n";
    }
    else
        for(i=0;i<y;i++)
            if(x[i]%4==0 || x[i]%8==0)
            cout<<x[i]<<" "<<endl;
}

void max_min() // finds largest & smallest number
{
    int x[100],y,i,largest=0,smallest;
    y= read_data("num.dat",x);
    if (y<=0)
    {
        cout << "Reading from file failed.\n";
    }
    else
        for(i=0;i<y;i++)
        {
            if(x[i]>largest)
            largest=x[i];
        }
        smallest=largest;
        for(i=0;i<y;i++)
            if(smallest>x[i])
            smallest=x[i];

    cout << "Maximum of the generated numbers is " << largest<<endl;
    cout << "Minumum of the generated numbers is " << smallest<<endl;
}

int main()
{
int selection;
for(int z=0;z<100;)
    {
        cout<<"1. Generate Random Numbers.\n";
        cout<<"2. Read generated numbers from file\n";
        cout<<"3. Find Sum and Average\n";
        cout<<"4. Find Maximum and Minimum\n";
        cout<<"5. Find Standard Deviation\n";
        cout<<"6. Find Numbers Divisible by 4 and 8\n";
        cout<<"7. Quit\n";
        cout<<"Selection: ";
        cin>> selection;
        switch(selection)
        {
            case 1:
            generate();
            break;
            case 2:
            display_data();
            break;
            case 3:
            sum_data();
            ave_data();
            break;
            case 4:
            max_min();
            break;
            case 5:
            findstddev();
            break;
            case 6:
            finddivison();
            case 7:
            return 1;
        }
    }
}


in this program i used switch/case with menu.
generate ,read, display,find division,min/max functions work properly.
but the functions i use to find sum average and std deviation doesnt work properly.
sumd and ave comes out a random number and std divation comes out always 1.

This post has been edited by coni: 12 Mar, 2008 - 06:45 AM
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 12 Mar, 2008 - 07:02 AM
Post #2


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


just looking though now.. but I did notice that for your standard deviation you use this formula:

stddev=(((sum+(ave*ave))/y))/(((sum+(ave*ave))/y));


just a reminder... x/x=1
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 12 Mar, 2008 - 07:12 AM
Post #3


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,857



Thanked 47 times

Dream Kudos: 550
My Contributions


for your sum and average... you need to initialize your accumulator variables. i.e. You did not set sum=0 when you calculate the sum. To find the average you should probably use the more traditional sum/numberOfElements.
User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 12 Mar, 2008 - 07:29 AM
Post #4


Jawsome++;

Group Icon
Joined: 17 Oct, 2007
Posts: 518



Thanked 2 times

Dream Kudos: 825
My Contributions


Also it is good practice to check if the file was opened properly, just as a good form of error checking. For a bit more information on reading in/out http://www.cplusplus.com/doc/tutorial/files.html
User is offlineProfile CardPM

Go to the top of the page

coni
post 12 Mar, 2008 - 08:24 AM
Post #5


New D.I.C Head

*
Joined: 12 Mar, 2008
Posts: 2

well in std deviation i tried to take square root i tried pow function but that didnt work either how i can take square root in c++?
User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 12 Mar, 2008 - 08:40 AM
Post #6


Jawsome++;

Group Icon
Joined: 17 Oct, 2007
Posts: 518



Thanked 2 times

Dream Kudos: 825
My Contributions


QUOTE(coni @ 12 Mar, 2008 - 09:24 AM) *

well in std deviation i tried to take square root i tried pow function but that didnt work either how i can take square root in c++?


cpp

/* sqrt example */
#include <stdio.h>
#include <math.h>

int main ()
{
double param, result;
param = 1024.0;
result = sqrt (param);
printf ("sqrt(%lf) = %lf\n", param, result );
return 0;
}



Note all you have to do is include the math header and then take a number for example "4" and perform the following:

cpp

cout <<sqrt(4);

The above would print out 2.



This post has been edited by captainhampton: 12 Mar, 2008 - 08:42 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 01:44AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month