12 Replies - 2425 Views - Last Post: 22 March 2013 - 07:41 PM Rate Topic: -----

#1 admgr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-March 13

Array Grade Help?

Posted 17 March 2013 - 07:18 PM

Please consider giving me as little or as much help as you would like. Any helpful hints or nudges in the right direction are welcome, as well as program snippets and insight. Thank you.



"This program will read grades from two different files (one at a time) and do some simple analytical operations on those grades -- identifying the maximum, minimum, median, and mean."

"Information about the scores will be stored in an array. But, the array will not hold the scores themselves -- instead it will record the number of occurrences of each particular score."

So, I have an assignment where I need to input 2 datafiles, "data1.txt" and "data2.txt", and find the max, min, median, and mean. I need to use the main function, as well as readData and analyzeData functions.

The readData function opens and reads the specified data file, counting the scores that are found inside.

Parameters:

fileName: (string input) the name of the file to read
dataCounts: (integer array output) the scores found in the file
totalScores: (integer output) the total number of scores found

The analyzeData function analyzes the data described by the array given to it, computing desired statistics

Parameters:

dataCounts (integer array input) describes the values of data
totalScores (integer input) the number of scores described
maximum (integer output) the maximum score
minimum (integer output) the minimum score
median (integer output) the median scores
mean (double output) the average (mean)



this is what i have done so far;

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void readData():
			//opens and reads the specified data file, counting the scores that are found inside.
void analyzeData():
			//analyzes the data described by the array given to it, computing desired statistics

int main()
{
		/*analyzes the data in TWO data files, named "data1.txt" and "data2.txt"
and displays the maximum, minimum, median, and mean for each file.
There will be NO input from the keyboard*/


}




what should i do next? how do I use incorporate data1 and data2?

Is This A Good Question/Topic? 0
  • +

Replies To: Array Grade Help?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: Array Grade Help?

Posted 17 March 2013 - 07:21 PM

File I/O in C. That's a good starting point.

Edit: My bad- I missed that this was C++ rather than C!
Was This Post Helpful? 1
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Array Grade Help?

Posted 17 March 2013 - 07:35 PM

File IO in C++ may be more appropriate.
Was This Post Helpful? 2
  • +
  • -

#4 admgr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-March 13

Re: Array Grade Help?

Posted 19 March 2013 - 09:41 AM


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void readData()
{
	string line;
  ifstream myfile ("data1.txt");  // i need to read data1 and data2, how do i write that?
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      //????? what should i do here
    }
    myfile.close();
  }

  else cout << "Unable to open files";

			//opens and reads the specified data file, counting the scores that are found inside.
}
void analyzeData()
{
			//analyzes the data described by the array given to it, computing desired statistics
}
int main()
{



readData();				//data has to be read first

	
	
	int isHighest(int *a, int len);                 // this is to find the max
{
 int high = a[0];
 for (int n=0; n < len ; n++ )
 {

  if(n > 0)
   if(high < a[n])
    high = a[n];
 }
 return high;
}




	int isLowest(int *a, int len)               //this is to find the min
{
 int low = a[0];
 for (int n=0; n < len ; n++ )
 {
  if(n > 0)
   if(low > a[n])
    low = a[n];
 }
 return low;
}
	
	
	
	float findMean(int *a, int len)            // this is to find the mean
{
 float total = 0;
 for (int n=0; n < len ; n++ )
  total += a[n];
  return(total / len);
}



	float findMedian(int *a , int len)                // this is to figure the median

{

 if(len%2 == 0)
  return ((float)a[len/2]+a[(len/2)-1])/2 ;

 else
  return a[len/2] ;

}



	system ("pause");
	return 0
}




so I've done so far I know its not much although I'mm trying. So I'm guessing my text files have to be in the resource files. Although my question is what would the coding be to read the text files at the same time?
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Array Grade Help?

Posted 19 March 2013 - 01:35 PM

You can use getline or the extraction operator (>>) to read the contents of a file (there are other ways as well). Heres a quick getline example

#include <iostream>
#include <string>
#include <fstream>

int main() {
   std::ifstream file("data.txt"); //must be the current directory

   if(!file.is_open()) //if file could not be opened
   {
      std::cerr<<"Couldn't open file\n";
      return 1; //kill program
   }


   std::string line; //will hold a line of text for each iteraton
   while(getline(file, line)) { //read file line by line
      std::cout<<line<<std::endl; //output line to console
   }


   file.close(); //close file (good practice)
   return 0;
}


This post has been edited by jjl: 19 March 2013 - 01:36 PM

Was This Post Helpful? 1
  • +
  • -

#6 admgr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-March 13

Re: Array Grade Help?

Posted 19 March 2013 - 07:09 PM

ok i have done some more work on the coding although I'm still stuck with my void readData how am i supposed to output dataCounts and totalScores


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void readData(string fileName);

			//opens and reads the specified data file, counting the scores that are found inside.
			// the outputs will be		dataCounts		(integer array output)	the scores found in the file
			//							totalScores		(integer output)	    the total number of scores found
void analyzeData(int dataCounts[], int totalScores);

			//analyzes the data described by the array given to it, computing desired statistics

int main()
{
}
	
	void readData( string fileName)				//data has to be read first
{	
	int totalScores = 0;
	string line;
  ifstream infile (fileName);
 
  string line;
  while(getline(file,line))
	  cout << totalScores <<endl;
	  
  
													 //make new array for analyzeData
												// make totalScores = totalScores +1    

    infile.close();
 

  else cout << "Unable to open files";
}	
	




	void analyzeData(int dataCounts[], int totalScores){
	
	//dataCounts is going to be from the first readData funct
	

	//instead a  you should have datacounts       and    for len either 100 or totalscores
	

 int maximum = dataCounts[0];
 for (int n=0; n <= 100 ; n++ )				//look at it again 
	{

  
   if(maximum < n &&  dataCounts[n] > 0)
    maximum = n;
	}
 

	
 int minimum = dataCounts[0];
 for (int n=0; n < 100 ; n++ )				//this is for min value
	 {
  if(n > 0)
  
	if(minimum > dataCounts[n])
    minimum = n;
	}
  
	
	
 int total = 0;
 for (int n=0; n < 100 ; n++ )					//this is for the mean value
  total = total + dataCounts[n];
  return(total / 100);


  int median = 0;
  median = dataCounts [100/2];

 if(dataCounts %2 == 0)
  return ((int)dataCounts[100/2]+dataCounts[(100/2)-1])/2 ;

 else
  return dataCounts[100/2] ;

 median = dataCounts[100/2];


 //cout everything you get for median exc.

 cout << maximum << endl;
 cout << minimum << endl;
 cout << mean << endl;
 cout << median << endl;
}



Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Array Grade Help?

Posted 19 March 2013 - 07:23 PM

Quote

int main()
{
}



Your main function is completely empty.
Was This Post Helpful? 0
  • +
  • -

#8 admgr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-March 13

Re: Array Grade Help?

Posted 20 March 2013 - 09:28 AM

could you guys help me arrange this now? how can i output for data1 and data2




#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void readData( string fileName, int dataCounts[], int &totalScore);

			//opens and reads the specified data file, counting the scores that are found inside.
			// the outputs will be		dataCounts		(integer array output)	the scores found in the file
			//							totalScores		(integer output)	    the total number of scores found
void analyzeData(int dataCounts[], int totalScores, int maximum, int minimum, int median, int mean);

			//analyzes the data described by the array given to it, computing desired statistics

int main()
{
	int dataCounts[101];
	for (int x = 0; x < 101; x++)
		dataCounts[x] = 0;
	int totalScores = 0;

	readData ( "data1.txt", dataCounts, totalScores);


	readData ( "data2.txt", dataCounts, totalScores);
	
	cout <<  totalScores;
 system ("pause");
}	
	void readData( string fileName, int dataCounts[], int &totalScore)			//data has to be read first
{	
	char line[4];
	int value = 0;
	
  ifstream infile (fileName);
 
	while (!infile.eof())
	{   infile >>  line; 
	value = atoi(line);
	dataCounts[value]++;
	totalScore++;
	}


												//make new array for analyzeData
												// make totalScores = totalScores +1    

    infile.close();
 

  //else cout << "Unable to open files";
	
	




	void analyzeData(int dataCounts[], int totalScores, int maximum, int minimum, int median, int mean)
	{
	
									
	

															//instead a  you should have datacounts       and    for len either 100 or totalscores


 int maximum = dataCounts[0];
 for (int n=0; n <= 100 ; n++ )				//look at it again 
	{

  
   if(maximum < n &&  dataCounts[n] > 0)
    maximum = n;
	}
 

	
 int minimum = dataCounts[0];
 for (int n=0; n < 100 ; n++ )				//this is for min value
	 {
  if(n > 0)
  
	if(minimum > dataCounts[n])
    minimum = n;
	}
  
	
	
 int total = 0;
 for (int n=0; n < 100 ; n++ )					//this is for the mean value
  total = total + dataCounts[n];
  return(total / 100);


  int median = 0;
  median = dataCounts [100/2];

 if(dataCounts %2 == 0)
  return ((int)dataCounts[100/2]+dataCounts[(100/2)-1])/2 ;

 else
  return dataCounts[100/2] ;

 median = dataCounts[100/2];


 //cout everything you get for median exc.

 cout << maximum << endl;
 cout << minimum << endl;
 cout << mean << endl;
 cout << median << endl;
}


Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,351
  • Joined: 25-December 09

Re: Array Grade Help?

Posted 20 March 2013 - 09:41 AM

Also posted here.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Array Grade Help?

Posted 20 March 2013 - 09:58 AM

It is often a good idea to find your solution program in steps.

A first step might be:

Code for a function that reads and processes a file

just finding the min, max, average, numValues for that file

Then your main could just call that function, passing in that file name.

So ... the prototype could look like this ...
void readAndProcess( const string& fName );


Once you have that working the way you want it to work ...

then you could add a function to get your median value ...
and then add that median output also.

That function could have a prototype like this ...
double getMedian( const int countsAtVal[], int sizeCounts, int  numScores );

Note: I think you may need to return a double value here, to handle cases where the total number of scores in a file is an even number.

To read your scores from a (valid) file of numbers
(
i.e. a file where there are only integers with values in the range 0..100
and each int is separated by only 'white spaces' ...
i.e. a file like this:
  99 77 44 0 7
  66   55   3  88 99 100  44
  77


)

You could use a loop like this ...

// open file to read as a file object ... say you call it fin
// to test if file opened ok ... can just code: if( fin ) { /* ... */ }

cout << "For file " << fName << " ... \n";
int countsAtVal[101];
// then initial all this above array to zero
int i = 0, min = 0, max = 0, sum = 0;
while( fin >> val )
{
   if( i != 0 )
   {
      if( val < min ) // update min
      if( val > max ) // update max
   }
   else // i was zero
       min = max = val;

   ++ countsAtVal[ val ];  // update count this val
   sum += val;
   ++i ; // Note: i holds total count of scores 
}
fin.close();

// now can output min, max, sum/i, i
// Note: handle case of i possibly zero above, so you don't try to divide by zero



Note: the above example solution process does NOT conform to your design spec's
But may give you a start ...

To conform to your design spec's ...
your analyze function might have a prototype like this ...
void analyzeAndOutput( const int ary[], int size );

and would find/output min, max, mean, median, numScores

You would have to revise the above example read function ...
int fillFromFile( int ary[], int size, const string& fName );
but you could still use your getMedian function and call it in your anylyzeAndOutput function

The only really tricky part of your program could be to code the getMedian function correctly ?

But get all the rest of your program working to spec ... first :)

This post has been edited by David W: 20 March 2013 - 11:42 PM

Was This Post Helpful? 0
  • +
  • -

#11 admgr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-March 13

Re: Array Grade Help?

Posted 21 March 2013 - 08:27 PM

void readData( string fileName, int dataCounts[], int &totalScore);

			//opens and reads the specified data file, counting the scores that are found inside.
			// the outputs will be		dataCounts		(integer array output)	the scores found in the file
			//							totalScores		(integer output)	    the total number of scores found
void analyzeData(int dataCounts[], int totalScores, int maximum, int minimum, int median, float mean);

int main()
{
			//analyzes the data described by the array given to it, computing desired statistics
	int totalScore = 0;
	int value = 0;
	int dataCounts[101];
	for (int x = 0; x < 101; x++)
		dataCounts[x] = 0;
	int totalScores = 0;
	int counter = 0;
	int maximum = 0;
	int minimum = 0;
	int median = 0;
	float mean = 0;
	
	readData ( "data1.txt", dataCounts, totalScores);

	analyzeData(dataCounts, totalScores, maximum, minimum, median, mean); 
cout << "datacounts is " << dataCounts[value]<< endl;
cout << "Total Score is " <<   totalScores << endl;
cout << "max " << maximum << endl;
//cout << "maximum is " <<  maximum << endl;
//cout << "minimum is " << minimum << endl;
//cout << "median is " << median << endl;
//cout << "mean is " << mean << endl;
	//readData ( "data2.txt", dataCounts, totalScores);
	
	
 system ("pause");
}	
	void readData( string fileName, int dataCounts[], int &totalScore)			//data has to be read first
{	

	char line[4];
	int counter = 0;
	int value = 0;
  ifstream infile (fileName);
 
	while (!infile.eof())
	{   infile >>  line; 
	value = atoi(line);
	
	dataCounts[value]= dataCounts[value] + 1;
	totalScore++;
	/*totalScore += value;
	dataCounts[value];*/
	}
	
	
}
												//make new array for analyzeData
												// make totalScores = totalScores +1    

   

  
	

	void analyzeData(int dataCounts[], int totalScores, int maximum, int minimum, int median, float mean)
{

	 dataCounts[0] ;
 for (int n=0; n <= 100 ; n++ )				
	{
   if(maximum < n &&  dataCounts[n] > 0)
	   maximum =dataCounts[n];
	}
 



im trying to get datacounts and totalscore but i cant get both could you guys help me?
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1327
  • View blog
  • Posts: 4,552
  • Joined: 19-February 09

Re: Array Grade Help?

Posted 21 March 2013 - 08:58 PM

They look fine, try printing them out after readData.
Was This Post Helpful? 0
  • +
  • -

#13 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Array Grade Help?

Posted 22 March 2013 - 07:41 PM

It IS a good idea to get your code working ... IN STEPS !

That way you/we can see you are making? progress ?

A first step ...

Get this first function working ...

And then show it ...

by printing out the contents of the array to screen ...

int readData( const string& fileName, int valCounts[] )
{	
   int numScores = 0;
   int val = 0;
   ifstream fin( fileName.c_str() );
   while( fin >> val )
   {
      ++ valCounts[val ];
      ++ numScores;
   }
   fin.close();
   return numScores;
}


Note:
This code 'assumes' valid data in file, i.e. only int's in range 0..100
It also assumes, before you pass in the array, you have reserved space for 101 int's and all the places in that int array were initialed to zero

This post has been edited by David W: 22 March 2013 - 07:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1