a function for numbers read from file

a function for numbers read from file

Page 1 of 1

1 Replies - 992 Views - Last Post: 01 May 2008 - 08:27 AM Rate Topic: -----

#1 dandona  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-May 08

a function for numbers read from file

Posted 01 May 2008 - 05:30 AM

Write a program that reads the student number (ID) and the five grades D1, D2, D3,
D4, D5, for 37 students from a file called “marks.txt”, then use a function to determine
the average grade for each student. Let the main program prints the student number, the
total grades and the average for each student.

the file marks.txt is like that:
1
20
30
40
50
60

2
11
22
33
44
55

3
33
44
55
96
98

4
85
88
70
60
55

5
90
80
70
60
50

6
68.5
99.9
100
55
33

7
66.6
44.5
70
30
60

8
95.5
88
84
58
60

9
55.5
70
77
60
20

10
50
30
20
40
95

11
90
80
70
60
55

12
33
44
55
66
77

13
37
29
70
59
40

14
69
25
49
55
90

15
59
79
85
95
60

16
80
85
30
20
95

17
28
49
56
64
28

18
24
93
84
43
95

19
53
74
83
95
91

20
29
19
39
48
45

21
89
45
30
57
34

22
44
58
38
67
71

23
38
57
38
47
23

24
92
82
72
93
64

25
57
48
29
47
19

26
85
36
77
33
76

27
21
31
91
82
53

28
90
32
35
78
54

29
11
22
33
44
55

30
80
40
50
60
70

31
35
25
15
26
27

32
93
94
75
86
92

33
76
84
39
47
39

34
74
93
85
73
22

35
88
99
66.5
84.5
30.4

36
77.4
33.4
76.5
99
44

37
55
23
45
92
44.5

i've done every thing except the function needed above , i also made the average which he wants me to do in a function but not in a function .. i need anyones help plzzzzzzzzzzzzzzzzzz



#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int ID;
double D1;
double D2;
double D3;
double D4;
double D5;
double average;
double total=0;
ifstream inFile;

inFile.open("marks.txt");

if(!inFile)
{
cout << "Error" << endl;
return 1;
}
for (int i=1; i<=37; i++)
{

inFile >> ID;
inFile >> D1;
inFile >> D2;
inFile >> D3;
inFile >> D4;
inFile >> D5;

total=D1+D2+D3+D4+D5;
average=total/5;

cout << "ID: " << ID<< endl;
cout << "D1: " << D1 << endl;
cout << "D2: " << D2 << endl;
cout << "D3: " << D3 << endl;
cout << "D4: " << D4 << endl;
cout << "D5: " << D5 << endl;

cout << "The average grade for student " << ID << " is " << average << endl;

}

inFile.close();
return 0;
}



i did the program but i can't do the function that makes the average

This post has been edited by dandona: 01 May 2008 - 06:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: a function for numbers read from file

#2 lockdown  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 394
  • Joined: 29-September 07

Re: a function for numbers read from file

Posted 01 May 2008 - 08:27 AM

Here is what your instructor is basically asking for. You need to create a function called Average that is passed 5 double variables and returns a double variable.

double Average(double,double,double,double,double); // Definition of Average function (Placed outside main function like all function definition)


You will notice that double is listed before the word Average (Average is the functions name). This double is the return type, and the 5 doubles inside () are the data types that will be passed to the function. Their is 5 doubles since you are passing it 5 double variables.

Here is what the code would look like for what you need to do.
double AverageF(double,double,double,double,double); // Declare function and values it will return/get passed

int main()
{
	double average = 0; // Average avariable that will be set equal to what the average function returns
	double D1 = 0, D2 = 0, D3 = 0, D4 = 0, D5 = 0; // You can also declare variable such as this if they are all the same data type

	// You should place this calling statement after you read in the D values from your file, Put ir were you place your average calculation orginally
	average = AverageF(D1,D2,D3,D4,D5); // Calls AverageF function which is passed D1-D5 values and sets average variable equal to what the function returns

	return 0;
}

double AverageF(double D1,double D2,double D3,double D4,double D5)
{
	double average = 0; // re-decalre average varibale for inside this function (variables declared inside function cannot be access from other functions
						// They technicaly can be accessed in other ways and passed but you will learn that in your class later

	average = (D1 + D2 + D3 + D4 + D5) / 5; // Calulates average and sets it equal to average variable

	return average;		// returns average variables value to calling function (In this case main)
}



Read threw the code and you should be able to place it into your code to get it all working. Also a quick note of good programming practice is to set your variables you declare set to 0. Not everyone dose this but I generally like to because then your variables are not equal to crazy numbers before you set them equal to something else. Like when you declare average without setting it equal to 0 it will have some crazy long number. But thats just me.

Best of luck.

This post has been edited by lockdown: 01 May 2008 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1