C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 30th, 2004, 08:06 PM
KRONOS11 KRONOS11 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 2 KRONOS11 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
  #2  
Old October 1st, 2004, 03:05 PM
Kernel Mustard Kernel Mustard is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 32 Kernel Mustard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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

Reply With Quote
  #3  
Old October 4th, 2004, 02:41 PM
KRONOS11 KRONOS11 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 2 KRONOS11 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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;
 
}		

Reply With Quote
  #4  
Old October 4th, 2004, 02:53 PM
Kernel Mustard Kernel Mustard is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 32 Kernel Mustard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
glad to hear i could help

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with simple program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT