Need help with array function.

I am working on a problem for my programming class. I am not very good at it and could use a bit of help. I am lost on how an array function works. I have the sample code with the function and function definition in it. I need to take what is given to me and while not modifying or deleting the scores being computed in main, have the scores computed again in the function and print to the screen. In essence, the scores would be printed to the screen twice. Once in the array in main, and again in the array function. My problem is what would go in between the {} for the array function at the bottom of the program.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// --------------------------------------------------------------
// Read a file of scores 0..100
// Count number of scores in following ranges
//   90 - 100 "A", and so on for B, C, D, F
// Store the scores so that they can be sorted later on
// *Assume exactly 10 scores
// --------------------------------------------------------------

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;

// =========================
// Global Declarations
// =========================
const char INVALID_GRADE = 'z';
const int NUM_SCORES = 10;

char letterGrade(double s);
  // Given a score in range 0..100, returns (uppercase) letter grade
  // Returns INVALID_GRADE if score input is outside 0..100 range

void printScores(const double s[], int len);

// =========================
// Main Function
// =========================
int main()
{
	ifstream infile;
	string filename;
	double scores[NUM_SCORES];  // declares 10 scores
	// Prompt user for the file name

	cout << "Enter input file name containing scores: ";
	getline(cin, filename);

	infile.open(filename.c_str());

	// Validate that the input file opened successfully
	assert(infile);
	char lgrade;
	int Acount = 0, Bcount = 0, Ccount = 0, Dcount = 0, Fcount = 0, invalidcount = 0;

	for ( int i = 0; i < NUM_SCORES; ++i)
	{
		infile >> scores[i];
		lgrade = letterGrade(scores[i]);

		switch (lgrade)
		{
			case 'A': 
				Acount++;  break;
			case 'B': 
				Bcount++;  break;
			case 'C': 
				Ccount++;  break;
			case 'D': 
				Dcount++;  break;
			case 'F': 
				Fcount++;  break;
			case INVALID_GRADE: 
				invalidcount++;  break;
		}
		
		cout << scores[i] << " => " << lgrade << endl;  
	}

	// Display counter statistics
	cout << endl;
	cout << "Summary Report" << endl;
	cout << "  " << Acount << " A's" << endl;
	cout << "  " << Bcount << " B's" << endl;
	cout << "  " << Ccount << " C's" << endl;
	cout << "  " << Dcount << " D's" << endl;
	cout << "  " << Fcount << " F's" << endl;
	cout << "  " << invalidcount << " invalid scores" << endl;

	infile.close();
	
	// TODO: Write a function printArray that prints the entire array
	// of scores.  Pass the array in as a parameter.

		cout << "Enter input file name containing scores: ";
	getline(cin, filename);

	infile.open(filename.c_str());

	// Validate that the input file opened successfully
	assert(infile);

	printScores(scores, NUM_SCORES);



	return 0;
}

// =========================
// Function Definitions
// =========================

char letterGrade(double s)
{
	if (s > 100.0 || s < 0.0)
		return INVALID_GRADE;
	else if (s >= 90.0)
		return 'A';
	else if (s >= 80.0)
		return 'B';
	else if (s >= 70.0)
		return 'C';
	else if (s >= 60.0)
		return 'D';
	else 
		return 'F';
}

void printScores(const double s[], int len)
{
	
//What code goes here in the function???

}

Last edited on
I think you need somthing similar:

1
2
3
4
5
6
void printScores(const double s[], int len)
{
	for(int i = 0; i < len; i++) {
		cout << s[i] << endl;
	}
}


I hope this is useful for you!
Topic archived. No new replies allowed.