0

The problem...... This is followed by the student id and then several marks student scored in various assessments, one per line. A small segment of the file might look like the following,,,,

2
S1234567
55
70
4
S2222222
96
67
88
88

So, according data presented in this file, the first student has 2 scores, student id is S1234567, and the assessment scores are 55 and 70. The second students has 4 scores, student id is S2222222, and the assessment scores are96, 67, 88 and 88.

So what I want to know is if I was asked to save this to an array and display in a meaningful way can I save it to a 2d array? Because number of columns changes in each row...

S1234567     55, 70
S2222222     96, 67, 88, 88

Is this possible?

4
  • 1
    Open the file, read lines, handle lines. Could you write a step-by-step instruction how a half-way intelligent human person could perform the task of extracting the relevant data? Start with that instruction first, then code the whole in C++. Commented Oct 4, 2015 at 7:47
  • I think you'd need to create two separate arrays. Commented Oct 4, 2015 at 7:47
  • I would make a struct capable of holding all the information for one student (including the variable number of marks), then figure out how to get the info from one record in the file to one object of that struct. Commented Oct 4, 2015 at 7:51
  • Also I don't know how a 2d array helps you particularly. Either a single dimensional array of the above mentioned struct objects or something like std::map<std::string, std::vector<int>> make sense to me. Commented Oct 4, 2015 at 7:53

1 Answer 1

1

The simplest thing would be to make std::vector<std::vector<int>>, where the first element in each std::vector<int> is student's id.

Better would be to use a Student class:

class Student
{
public:
    int id;
    std::vector<int> scores;

    ...
};

std::vector<Student> students;

You can easily overload >> and << operators for this class and much more for the future.

Sign up to request clarification or add additional context in comments.

4 Comments

Since I'm still a beginner we haven't learned how to use classes and we are asked to save the data to an array and then display it in a meaningful way...
You can't even use std::vector?
I advised std::vector<std::vector<int>> and @Galik recommended std::map<std::string, std::vector<int>>, you can get it to work with one of these.
1) will use a function called readFile() invoked from the main()to open the text file marks.txt for reading and store all of the student marks to an array named marksArray and then use another function called displayMarks() also called from the main()to display the contents of the array on the screen in an appropriate format. The readFile()function has two parameters: one for receiving the file variable and one for the array, both receiving arguments passed by reference. The displayMarks() function has two parameters: the array and the number of values in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.