| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with simple program
This program takes input from a file containing data, then outputs 25 lines of data
and no more than that. This program will also sort the data from lowest to highest and show the average income for the households. */ #include <iostream> #include <fstream> #include <stdio.h> using namespace std; //Prototypes int ReadStuData(ifstream&rss,int scores[],int id[],int& count,bool& tooMany); void PrintGrade(int oneScore,double average); void PrintTable(int scores[],int id[],int count); double mean(int scores[],int count,double average); int main() { ifstream rss; const int MAX_SIZE=100; // max values for array int id[MAX_SIZE]; //number of elements in array int scores[MAX_SIZE]; //number of elements in array int count=0; int oneScore=0; double average=0; bool tooMany=0; ReadStuData(rss,scores,id,count,tooMany); mean(scores,count,average); PrintTable(scores,id,count); } /*--------------------------------------------------------- This Function Reads the household into an array and counts the numbers of elements in the array. --------------------------------------------------------*/ int ReadStuData(ifstream& rss,int scores[],int id[],int& count,bool& tooMany) { const int MAX_SIZE=100; // max values for array char fname[MAX_SIZE];//In File name that the user inputted cout<<"What is the name of the file? "<<flush; cin>>fname; rss.open(fname,ios::in); //open if (!rss) { cout << "Error opening " << rss <<endl; return 0; } for (int i=0;i<count;i++) { id[i]=scores[i]=0; } if(count>MAX_SIZE) { tooMany=count; cout<<fname<<" has"<<tooMany<<"elements in it"; cout<<" please enter a new file with less than 100 values"<<endl; exit(-1); } while (!rss.eof() && (count<MAX_SIZE) ) { rss >>id[count]>>scores[count]; if (rss.eof()) break; else if ( rss.fail() ) { cout << "Error reading from file " << fname << " at line " << count << endl; break; } count++; } rss.close(); } /*-------------------------------------------------------- This Function prints ID, the students Score, and the assigned grade -------------------------------------------------------*/ void PrintTable(int scores[],int id[],int count) { int oneScore; double average=0; cout<<"ID\t"<<"Scores\t"<<"Grade Assigned"<<endl; for(int i=0;i<count;i++) { oneScore=scores[i]; cout<<id[i]<<"\t"<<scores[i]<<"\t"; PrintGrade(oneScore,average); } } /*---------------------------------------------------------- This prints the student grade after comparing oneScore to Average --------------------------------------------------------*/ void PrintGrade(int oneScore,double average) { const int MAX_SIZE=100; int scores[MAX_SIZE]; int count; mean(scores,count,average); if (oneScore<average-10) cout<<"Unsatisfactory"<<endl; /*else if(oneScore<=(mean(scores,count,average)-10 &&(oneScore>=(mean(scores,count,average)+10)))) cout<<"Satisfactory"<<endl; else if(oneScore>((mean(scores,count,average)+10))) cout<<"Outstanding"<<endl;*/ } /*-------------------------------------------------------- This Function uses scores[] to find the average of count students scores. -------------------------------------------------------*/ double mean(int scores[],int count,double average) { for (int i=0;i<count;i++) { average+=scores[i]; } average=average/count; } i get a crash everytime i call mean(scores,count,average) im not quite sure why this is doing it, also the value for average is off someone please throw me in the right direction thanks! |
|
#2
|
|||
|
|||
|
Here's a few ideas:
1) Make the const MAX_SIZE a global variable so you don't need to define it in each function that uses it. This cleans it up and makes it easier if you want to change the value of MAX_SIZE, because you only have to change it in one place (globally, that is outside of all the functions). 2) In the function mean, I assume what you want to do is to have the mean stored in the variable average and have this result available to the function that called mean in the average variable it passed in. For Example: function1(double &average) { average = ***do the average calculation here***; } function2() { double average = 0; function1(average); //after the call to function1, the variable "average" here will hold the result calculated by function1 } To do this make the parameter average a reference like so "double &average", make sure to do the same thing in the forward declaration of the the mean function at the top of the page. Also, the return value of mean can be void since the value you calculated will be stored in the parameter average. 3) In the function PrintGrade, the variable count is not initialized. Note that this count is not the same count as you used in other functions, here you're declaring a new one with the same name. Either pass the variable count into this function, or make count a global variable and remove it from the function definitions of all the functions which use it. Actually, in looking at the function again, you're calling mean again, which isn't necessary. You calculated it once in the main function, and by passing the variable average to mean as a reference (as mentioned in part 2), the varible average declared in the main function will contain your average, so why calculate it again. You could pass the variable average to PrintTable, which in turn could pass it to PrintGrade, all without the need to call the function mean again, or once again you could make the variable average global, so you don't need to pass it between functions. 4) Look at the following portion of the function ReadStuData: ************************* for (int i=0;i<count;i++) { id[i]=scores[i]=0; } if(count>MAX_SIZE) { tooMany=count; cout<<fname<<" has"<<tooMany<<"elements in it"; cout<<" please enter a new file with less than 100 values"<<endl; exit(-1); } ************************** When you call the function ReadStuData() from main, count is zero. What this means is that the for loop doesn't do anything and that count will never be greater than MAX_SIZE at this point, since you don't increment count until you're in the while loop after this section. In the for loop change count to MAX_SIZE to make sure you initialize the two arrays. This is what you want anyway since they are both of size MAX_SIZE. As for the (count > MAX_SIZE) thing, it wont' do anything, and your program will only read the first MAX_SIZE elements anyway, because of the conditions of your while loop. One thing you could do if you still want to detect files with more than MAX_SIZE scores, is to remove the (count<MAX_SIZE) condition from the while loop, and move the whole "if (count>MAX_SIZE>) ..." block of code inside the while loop. Just a few things to get you going, hope it helps. Kernel Mustard |
|
#3
|
|||
|
|||
|
thanks, that helped program finished
![]() finished code below Code:
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
//Prototypes
int ReadStuData(ifstream&rss,int scores[],int id[],int& count,bool& tooMany);
void PrintGrade(int oneScore,double average,int& count);
void PrintTable(int scores[],int id[],int count);
double mean(int scores[],int count,double average);
int main()
{
ifstream rss;
const int MAX_SIZE=100; // max values for array
int id[MAX_SIZE]; //number of elements in array
int scores[MAX_SIZE]; //number of elements in array
int count=0;
int oneScore=0;
double average=0;
bool tooMany=0;
ReadStuData(rss,scores,id,count,tooMany);
PrintTable(scores,id,count);
}
/*---------------------------------------------------------
This Function Reads the household into an array
and counts the numbers of elements in the array.
--------------------------------------------------------*/
int ReadStuData(ifstream& rss,int scores[],int id[],int& count,bool& tooMany)
{
const int MAX_SIZE=100; // max values for array
char fname[MAX_SIZE];//In File name that the user inputted
cout<<"What is the name of the file? "<<flush;
cin>>fname;
rss.open(fname,ios::in); //open
if (!rss) {
cout << "Error opening " << rss <<endl;
return 0;
}
for (int i=0;i<count;i++)
{
id[i]=scores[i]=0;
}
if(count>MAX_SIZE)
{
cout<<fname<<" has"<<tooMany<<"elements in it";
cout<<" please enter a new file with less than 100 values"<<endl;
exit(-1);
}
while (!rss.eof() && (count<MAX_SIZE) ) {
rss >>id[count]>>scores[count];
if (rss.eof())
break;
else if ( rss.fail() ) {
cout << "Error reading from file " << fname <<
" at line " << count << endl;
break;
}
count++;
}
rss.close();
}
/*--------------------------------------------------------
This Function prints ID, the students Score, and the
assigned grade
-------------------------------------------------------*/
void PrintTable(int scores[],int id[],int count)
{
int oneScore;
double average=0;
average=mean(scores,count,average);
cout<<endl<<"ID\t"<<"Scores\t"<<"Grade Assigned"<<endl;
cout<<endl;
for(int i=0;i<count;i++)
{
oneScore=scores[i];
cout<<id[i]<<"\t"<<scores[i]<<"\t";
PrintGrade(oneScore,average,count);
}
}
/*----------------------------------------------------------
This prints the student grade after comparing oneScore to
Average
--------------------------------------------------------*/
void PrintGrade(int oneScore,double average,int& count)
{
const int MAX_SIZE=100;
int scores[MAX_SIZE];
if (oneScore<(average)-10)
cout<<"Unsatisfactory"<<endl;
else if(oneScore>=(average)-10
&&(oneScore<=(average)+10))
cout<<"Satisfactory"<<endl;
else if(oneScore>(average)+10)
cout<<"Outstanding"<<endl;
}
/*--------------------------------------------------------
This Function uses scores[] to find the average of count
students scores.
-------------------------------------------------------*/
double mean(int scores[],int count,double average)
{
for (int i=0;i<count;i++)
{
average+=scores[i];
//cout<<scores[i]<<endl;
}
average=average/count;
return average;
}
|
|
#4
|
|||
|
|||
|
glad to hear i could help
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with simple program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|