ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey guys , im a having a problem in figuring out how do i start on my assignment. Basically , i have been given a text file with data arranged in such a way....
Code:
2
Alice Tay S1234567 4
Procedural CSCI114 3 B
Object CSCI124 3 B
Algorithms CSCI103 4 A
Databases CSCI235 3 A
Jenny Koh S2345678 3
Systems CSCI212 3 C
Algorithms CSCI103 4 A
Databases CSCI235 3 B
The "2" above signifies the number of students in the text file.
The line above the grades signifies the student name , their id number , and the number of subjects they sat. The output expected is...
Code:
Student Name: Alice Tay
Student ID: S1234567
Number of courses enrolled: 4
Course No. Course Name Credits Grade
CSCI103 Algorithms 4 A
CSCI114 Procedural 3 B
CSCI124 Object 3 B
CSCI235 Databases 3 A
Total number of credits: 13
Student Name: Jenny Koh
Student ID: S2345678
Number of courses enrolled: 3
Course No. Course Name Credits Grade
CSCI103 Algorithms 4 A
CSCI212 Systems 3 C
CSCI235 Databases 3 B
Total number of credits: 10
I am not sure how to go about getting the expected output. From what i see , i have to store each individual information somewhere and then sorting it out based on the subject code. The subject with the lowest subject code should be displayed first then followed by the 2nd lowest and so on.
Hope for some help in how i can start saving and sorting the data out.
Hey guys , im a having a problem in figuring out how do i start on my assignment. Basically , i have been given a text file with data arranged in such a way....
Code:
2
Alice Tay S1234567 4
Procedural CSCI114 3 B
Object CSCI124 3 B
Algorithms CSCI103 4 A
Databases CSCI235 3 A
Jenny Koh S2345678 3
Systems CSCI212 3 C
Algorithms CSCI103 4 A
Databases CSCI235 3 B
The "2" above signifies the number of students in the text file.
The line above the grades signifies the student name , their id number , and the number of subjects they sat. The output expected is...
Code:
Student Name: Alice Tay
Student ID: S1234567
Number of courses enrolled: 4
Course No. Course Name Credits Grade
CSCI103 Algorithms 4 A
CSCI114 Procedural 3 B
CSCI124 Object 3 B
CSCI235 Databases 3 A
Total number of credits: 13
Student Name: Jenny Koh
Student ID: S2345678
Number of courses enrolled: 3
Course No. Course Name Credits Grade
CSCI103 Algorithms 4 A
CSCI212 Systems 3 C
CSCI235 Databases 3 B
Total number of credits: 10
I am not sure how to go about getting the expected output. From what i see , i have to store each individual information somewhere and then sorting it out based on the subject code. The subject with the lowest subject code should be displayed first then followed by the 2nd lowest and so on.
Hope for some help in how i can start saving and sorting the data out.
First describe step by step your actions when you do parsing with your eyes and fingers. Hint: you fingers are pointers in a wide sense of the word, i.e. your fingers point to certain places in input file or in memory or in array, etc.
In that language, do you know how to read a line from a text file into some kind of string variable?
What assumptions are you making about the syntactic rules of the input.
For example, if I were working from just the example (rather than from a reasonable spec)
I would not assume
There is exactly one blank within each student name
There are zero blanks in a subject name.
There is a blank line between students.
There are not blank lines elsewhere.
There are no trailing blanks on the lines.
I would assume
The last blank free chunk of a student line is the number of courses
The second to last blank free chunk of a student line is the student ID.
Everything before that is the student name.
Similarly for last three non blank chunks and everything before that for a course line.
Assumptions like that should be based on a spec, not just an example, so my assumptions might be seriously wrong. But you can't program a parser without making some set of assumptions like that. So decide and document such assumptions and then use them to design the parsing.
Sorry , i forgot to add that the assignment is in c++.
Hey sergei, well , from what i see , i first have to use getline to read each individual line. I can use the information given by the text data to find out how many students are there and how many subject each student take. But , from there, i am not very sure how am i able to store the information into an array as i have store different information into different array. For example, i must create an array to store subject code, an array to score the grade , an array to store the credits. Am i on the right track>?
In C, I would define a struct based on the desired format of the OUTPUT. Then, define an array of these structs. As you go thru the lines in the input file, simply assign each field to the appropriate member of the struct.
i am not very sure how am i able to store the information into an array as i have store different information into different array.
How much are you supposed to already know and/or be learning in this assignment about STL containers?
An important container to learn about is std::map. You can insert key,object pairs into a std::map in any sequence you like and then you can use an iterator to read them out in sorted sequence.
That is only one (I expect the easiest) solution to the sorting problem in your assignment. There are lots of other ways to sort in C++.
Quote:
For example, i must create an array to store subject code, an array to score the grade , an array to store the credits. Am i on the right track>?
No, not on the right track at all, though that could work.
You should be learning to program with objects. For example, the course name, number of credits and grade for a class taken by a student are a logical collection of data. A non object oriented approach has a separate array for each kind of data. An object oriented approach has a struct for the various data items that go together and then a container of those structs.
The key value (the thing you sort on) is also a logical member of that struct, but one of the compromises made for the ease of use of std::map is to deal with that logical member outside of the struct. There are alternative (slightly harder) sorting methods that would keep the key in its more logical place in the struct with the related data items. (But I would select std::map here because it is easier).
BTW, are the students supposed to be printed out in the sequence read in? Or sorted by ID? Or sorted by (first) name? Your example is unfortunately 3 way ambiguous on that detail.
Sorry , i forgot to add that the assignment is in c++.
Hey sergei, well , from what i see , i first have to use getline to read each individual line. I can use the information given by the text data to find out how many students are there and how many subject each student take. But , from there, i am not very sure how am i able to store the information into an array as i have store different information into different array. For example, i must create an array to store subject code, an array to score the grade , an array to store the credits. Am i on the right track>?
Struct is more appropriate then array. And, as others have already suggested, do it the OO way. I.e. hide data, though it's still logically equivalent to the struct fields, and in the class to contain the hidden (i.e. private) data write accessors (set*/get* methods).
Well, i read up about structs and i understand how to go about using it. The problem i am having is , how do i define how many struct to set up ? Each student can have more then 4 subjects so im not sure how to go about assigning them. For example, for this assignment my struct will be like this...
Well, i read up about structs and i understand how to go about using it. The problem i am having is , how do i define how many struct to set up ? Each student can have more then 4 subjects so im not sure how to go about assigning them. For example, for this assignment my struct will be like this...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.