Project help needed with arrays and fstream

C++ with arrays and functions, standard deviation too

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 4365 Views - Last Post: 27 January 2007 - 02:23 AM Rate Topic: -----

#1 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Project help needed with arrays and fstream

Posted 23 January 2007 - 09:45 AM

I am looking to finish a C++ source file. It need to accomplish reading a file (Grades.txt) and use an array to put it into the program for determining the average, standard deviation, the number of test scores, and the high and low scores... I need some serious help. I am a math 0930 student, so math is not my strong suit right now... Please Someone HELP!!! The code is well commented to help with deciphering but I still do not understand, this is my second semester of C++ so I am very new to it all... I also am attaching the grades file I was given... Thank you in advance.

Since it only seems to allow one attachment here is the source code for the program, the grades file will be the attachment, again thanks everyone...

// calculates the basic statistics of the test scores in the file grades.txt
// CS1410 Lab #1
//   - 01/18/07 BN  Basic framework developed
//
#include <iostream>	 // required for console i/o
#include <fstream>	  // required for file i/o
#include <cmath>		// required for sqrt()
using namespace std;

// OK to have a 'public' constant that does NOT change
const int SIZE=99;

// function prototypes
void get_data(int score[]);
int calc_numb(int score[]);
double calc_ave(int score[]);	  // 70 points
double calc_stddev(int score[]);   // 10 points
int calc_high(int score[]);		// 10 points
int calc_low(int score[]);		 // 10 points

// DO NOT modify the main function of this program!!!
//   -10 points if you do.
//
// modify the functions below the main function!!!
//
int main()
{
	// variables
	int score[SIZE];

	get_data(score);

	
	cout << "Basic Statistical analysis of scores in grades.txt \n\n";

	cout << "		  n		  = " << calc_numb(score) << "\n";
	cout << "		  average	= " << calc_ave(score) << "\n";
	cout << "		  stddev	 = " << calc_stddev(score) << "\n";
	cout << "		  high score = " << calc_high(score) << "\n";
	cout << "		  low score  = " << calc_low(score) << "\n";

	return 0;
}

// FUNCTION DEFINITIONS ------------------------------------
//
// GETDATA()
// reads the data from the file grades.txt and inserts into
// array score[]
//   input  - data in grades.txt (files ends with score=-999)
//   output - data in score[]
void get_data(int score[])
{
	ifstream input("grades.txt", ios::in);
	int data=0;
	int i=1;
	for (i=0; i<SIZE; i++)
	{
		score[i]=0;
	}
	i=1;
	while (data != 999)
	{
		input >> data;
		score[i]=data;
		i++;
	}
	score[0]=i-2;
}


// CALC_NUM()
// returns the number of test scores
//   input  - the score[] of test scores
//   output - the number of scores in the score[]
int calc_numb(int score[])
{
	return score[0];
}

// CALC_AVE()
// returns the average of test scores
//   input  - the score[] of test scores
//   output - the average of scores in the score[]
double calc_ave(int score[])
{
	return 0;
}

// CALC_STDDEV()
// returns the standard deviation of test scores
//   input  - the score[] of test scores
//   output - the stddev of scores in the score[]
double calc_stddev(int score[])
{
	return 0;
}

// CALC_HIGH()
// returns the high of test scores
//   input  - the score[] of test scores
//   output - the high of scores in the score[]
int calc_high(int score[])
{
	return 0;
}

// CALC_LOW()
// returns the low of test scores
//   input  - the score[] of test scores
//   output - the low of scores in the score[]
int calc_low(int score[])
{
	return 0;
}

Attached File(s)

  • Attached File  grades.txt (133bytes)
    Number of downloads: 115


Is This A Good Question/Topic? 0
  • +

Replies To: Project help needed with arrays and fstream

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Project help needed with arrays and fstream

Posted 23 January 2007 - 09:55 AM

Please post any errors that are being generated by the code, or describe any undesired behaviour being displayed by the program.
Was This Post Helpful? 0
  • +
  • -

#3 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Project help needed with arrays and fstream

Posted 23 January 2007 - 10:00 AM

So far I have compiled it with no errors the way it is... But when I ran it, I am not good with visual c++ 2005 express which is all I have right now... I did not know how to just run it, so it debugged it and when it tried to debug it is seemed to just be a blank console window... But it compiles without errors... When the teacher showed it on his system it showed all 0s when ran because there is no definition on the funtions except "return 0;".
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Project help needed with arrays and fstream

Posted 23 January 2007 - 10:18 AM

I guess you have to write the code to go into the functions calc_ave(), calc_stddev(), calc_high() and calc_low()
Was This Post Helpful? 0
  • +
  • -

#5 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: Project help needed with arrays and fstream

Posted 23 January 2007 - 10:20 AM

Ctrl+F5 to run the program or menu->debug->start without debugging.
Was This Post Helpful? 0
  • +
  • -

#6 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Project help needed with arrays and fstream

Posted 24 January 2007 - 08:05 AM

Yes I am supposed to write the code into the sections that are empty... Those are the pieces I am having trouble with, but also I am totally new to Standard Deviation formulas as well.

And thank you for telling me how to run it.... I will now be able to run it without worrying about the dang debugger getting in the way.
Was This Post Helpful? 0
  • +
  • -

#7 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Project help needed with arrays and fstream

Posted 24 January 2007 - 09:10 AM

View Postgryphin, on 24 Jan, 2007 - 03:05 PM, said:

Yes I am supposed to write the code into the sections that are empty... Those are the pieces I am having trouble with, but also I am totally new to Standard Deviation formulas as well.

And thank you for telling me how to run it.... I will now be able to run it without worrying about the dang debugger getting in the way.

the thing to do is to implement the functions one at a time - once one is working you can use some of the code for the others, e.g. implement the calculation of the average first.

for standard deviation, see
http://en.wikipedia....ndard_deviation
Was This Post Helpful? 0
  • +
  • -

#8 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Project help needed with arrays and fstream

Posted 25 January 2007 - 09:20 AM

My problem is I am a complete newbie and do not know how to really implement the functions nor what to put into them... Any help with it is very much appreciated. For average I figure it should be something along the lines of reading the array that is pulling the grades.txt file in and doing sum/# of items... but I do not know how to do this...
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Project help needed with arrays and fstream

Posted 25 January 2007 - 09:34 AM

The code below shows how to use a for loop in order to add up all the elements in an array. Onve all elements have been added, you divide by the number of elements in the array.

double calc_ave(int score[], int size)
{
   for(int i=0;i<size;i++)
   {
	  total += score[i];
   }
   return total/size;
}



The other functions will follow a similar process, depending on the formula required.

Is it the mathematical formulas that you are unfamiliar with, or translating those formulas into valid code?
Was This Post Helpful? 0
  • +
  • -

#10 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: Project help needed with arrays and fstream

Posted 25 January 2007 - 09:36 AM

Yes, that's how you get the average. You will need a variable to hold the average score, and a variable to hold the total scores. Then just divide the total score by the size of the array. Like this:

double average;
double total;

for ( i = 0; i < array_size; i++ )
{
	total += array_name[i];
}

average = total / array_size;

return average;



Edit: bit late. Exactly what Amadeus said. Lol

This post has been edited by BitByte: 25 January 2007 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#11 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Project help needed with arrays and fstream

Posted 25 January 2007 - 09:47 AM

Sorry that I am so slow at this but another question....

double average;
double total;

for ( i = 0; i < array_size; i++ )
{
	total += array_name[i];
}

average = total / array_size;

return average;



------------------------------------
Where it says "array_size" I put in "score" which is my array in main program marked as below....

// variables
int score[SIZE];

right? so I would put "score" into where array_size is above? and array name should be the same?
Was This Post Helpful? 0
  • +
  • -

#12 BitByte  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 194
  • Joined: 09-August 06

Re: Project help needed with arrays and fstream

Posted 25 January 2007 - 09:52 AM

Yes, you put the name of your array that you have passed to the function. Just like Amadeus has put. The size is the name of your array size that you also pass to the function. I just put them names as an example of how it is done, your have to replace them with the names that you use.
Was This Post Helpful? 0
  • +
  • -

#13 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Project help needed with arrays and fstream

Posted 26 January 2007 - 02:24 PM

Here is a newbie ?... What is the name of my array?
Was This Post Helpful? 0
  • +
  • -

#14 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Project help needed with arrays and fstream

Posted 26 January 2007 - 02:29 PM

You have already declared it:
int score[SIZE];


did you code the original file, or was it supplied to you? It may be difficult to write the functions you need without a good understanding of the basics.
Was This Post Helpful? 0
  • +
  • -

#15 gryphin  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 128
  • Joined: 29-October 05

Re: Project help needed with arrays and fstream

Posted 26 January 2007 - 02:35 PM

It was supplied... and this is all pretty new to me... I am taking CS 1410 (the object oriented programming C++ class, second part of CS 1400 intro to C++ programming class)... I am a new programmer, VERY new...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2