Array Programming Problems

I am new to programming and have no background or anything in c++. Any help on the following problem would be greatly appreciated! I know the basics but not enough to correctly write the function without any syntax errors or bugs.

Write a void function named ReadScores() which has two parameters. The first parameter is an array of integers named s; the second parameter is an integer variable, n, which is passed-by-reference from the calling function. The function shall open the file scores.dat for reading. This file contains a list of quiz scores, one integer per line, with each integer in [0, 10]. The end of the list of quiz scores is indicated by a sentinel of -1. ReadScores() must read the scores and store them in s starting with the first element of s. On return, n should be the number of quiz scores that were stored in s. You may assume that the size of s is sufficient to store all of the scores.
Well people will probably be reluctant to help you unless they can see that you've made an effort. Have a read of this site's tutorials if you don't understand how to solve the problem http://cplusplus.com/doc/tutorial/ then write some code and if it doesn't work post it here
Hi,

Share with us your code...
this is what i have. and for the record, i have made an effort on my end, but i'm thoroughly confused, hence why i'm asking for help, even a nod in the right direction...

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
void ReadScores(int s[ ], int& n);
int count;
ifstream fin;

fin.open("scores.dat");
if (!fin)
cout << "Error opening file\n";
else
{
for (count = 0; count != -1; count++)
fin >> s[count];
fin.close();
}
return 0;
}
for (count = 0; count != -1; count++) fin >> s[count];


I think the above is wrong. The requirement state -1 is the sentinel and it is to be read from the input file scores.dat to indicate end of input.

1
2
3
4
5
6
7
8
9
int s[256]; //assume size is enough to accomodate all based on requirement
int i = 0, score = 0;
fin.open("scores.dat");
if (!fin)
  cout << "Error opening file\n";
else {
  while (fin >> score && score != -1) s[i++] = score;    
  fin.close();
}
so where did you get 256 from? just a # large enough to assume to hold all of the scores? thanks for the help, i am so lost!
On return, n should be the number of quiz scores that were stored in s. You may assume that the size of s is sufficient to store all of the scores.
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
#include <iostream>
#include <fstream>
using namespace std;

void ReadScores(int s[], int &n) {
  ifstream fin; int score = 0, i = 0;
  fin.open("scores.dat");
  if (!fin)
    cout << "Error opening file\n";
  else {
    while (fin >> score && score != -1) {
      s[i++] = score;
      n++;
    }
    fin.close();
  }
}

int main() {
  int n = 0; int s[256];
  ReadScores(s, n);
  cout << "total number of scores: " << n << "\n";
  
  return 0;
}
Topic archived. No new replies allowed.