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