2d arrays and global functions and Prototypes

Program 9
Write a program that reads input file “grades.txt” to EOF and inputs a 5 digit student ID number followed by 6 test grades then another 5 digit student ID number followed by another 6 test grades for up to 15 students. The grade data is to be loaded into a double two-dimensional array with 15 rows and 6 columns that would parallel an integer array storing the student ID numbers. The program then needs to produce the following output to a named “prog9_out_<YourNetID>.txt”. For each student: Student ID Number followed by the 6 test grades for each student. The test average for each student to the nearest whole number. (Use a function that has the grade array and row number as arguments and returns the test average for that student. This function should only perform this one task.)Example Prototype: double TestAverage(double[][NO_GRADES], int )For each test: The class average for each test to the nearest whole number. (Use a function that has the grade array, number of actual students and test number as arguments and returns the class average for that test. This function should only perform this one task.) Example Prototype: double ClassAverage(double [][NO_GRADES], int, int )
One additional required function: Use a function that has the grade array, ID number array and an integer passed by reference as its arguments. It should perform the task of reading the data from the input file into the arrays. The function should return the number of student records read from the file via an integer argument passed by reference. The return value of the function should be bool. It will return true if the input file is successfully opened and read; otherwise, it returns false. Example Prototype: bool GetData(double[][NO_GRADES], int [], int&) Array sizes should be implemented with global named constants.

I just need further explaining on this assignment. Are global functions just like functions? Also what is does an outline of a 2d array look like?
redheadAlyssa wrote:
Are global functions just like functions?
Yes, I don't know why the word 'global' was used - unless a function is a member function in a class it is always global.
redheadAlyssa wrote:
Also what is does an outline of a 2d array look like?
int My2DArray[ROWS][COLUMNS];

Or, if you want the size to vary,

1
2
3
4
5
int **MyArrayOfArrays = new int*[rows];
for(unsigned i = 0; i < rows; ++i)
{
    MyArrayOfArrays[i] = new int[columns];
}


Though at this point I strongly recommend vectors if you are allowed to use them.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int TEST = 15;
double TestAverage(double[][TEST], int);
double ClassAverage(double [][TEST], int, int);
bool StudId(double[][TEST], int [], int&);
int main ()
{

	 int test_sum,
	     id,
	     test;

		ifstream fin;

	    fin.open("grades.txt");
	    if (!fin)
	    {
	          cout << "Failed to open fin file, program terminated.";
	          fin.close();
	          return(1);
	    }

	    ofstream fout;

	    fout.open("prog8_out_acb95.txt");
	    if (!fout)
	    {
	        cout << "Failed to open output file, program terminated.";
	        fin.close();
	        fout.close();
	        return(2);
	    }

	    while( !fin.eof() )
	    {
	     fin >> test;
         for (int i; i < 100; i++)
         {
        	 test_sum = double TestAverage(double[][TEST], test);

        	 fout << test_sum;
         }
         
         else
         {
        	 test = id;
         }
         fin >> test;
	    }
fout.close();
fin.close ();
return 0;
}

/************************************************************************
 * Calculates test sums
 */
double TestAverage(double[][TEST], int);
{
	 test_1  test_2  test_3  test_4  test_5  test_6;
}
Would you like to ask a question?
where am i going wrong?
1
2
3
4
double TestAverage(double[][TEST], int);
{
	 test_1  test_2  test_3  test_4  test_5  test_6;
}


What's this about?
Sorry that was a rough rough draft. heres my newest version:
1
2
3
4
5
6
7
8
9
10
11
12
13
 for(int i= 0; i<15; i++)
	    {
	    fin >> StudId[i];
	    fout << "Student ID: " << StudId[i] << endl;
	    for (int i = 0; i < 6; i++)
	    {
	    fin >> StudGrades[i];
	    fout << StudGrades[i] << " ";
	    }
	    fout << endl;
        fout << "Test Average: " << test_sum;
        fout << endl << endl;
	    }

Does the for loop fit to to do what was asked?
Yes, I don't know why the word 'global' was used - unless a function is a member function in a class it is always global.

I suppose, to be pedantic, you might say that a function that was declared within a namespace wasn't truly "global". Particularly if it was declared in an anonymous namespace, to restrict it to the scope of a single translation unit.

Or, in old-fashioned C terms, if you declared a function as static, to restrict it to file scope, you wouldn't call that a "global function".

In any case, the tutor probably used the term in this assignment purely to emphasise the difference between a function and a method. I've been doing this for a long time, and I still get sloppy about calling a method a "function", so I'm sure it's even less obvious to a new student :D
Last edited on
Topic archived. No new replies allowed.