Reputation: 85
I have a struct called student as such:
struct student{
char*lastName;
char*firstName;
int age;
float grade[3];
}
how would i read in a text file which should be read into an array of student records. All students' age should be randomly generated ranging from 18 to 35. each student has 3 text grades from 0 to 100.
Upvotes: 2
Views: 972
Reputation: 2678
First of all, if I understand your question, you're supposed to provide a linked list. Since linked lists are trickier than reading a file, generating numbers and so on, I'll only give you hints about that.
If you had to put your students in a simple array, it would be easy, a simple student myArray[100], for instance, would do it. It would mean that all your students would be stocked in the same part of the memory. Like that : (A)(B)(C). A, B and C are three students, stocked next to each other. This is not convenient if you're going to have a vast number of students, since you'll have to find a big enough zone in the memory to stock everything. This is why there are linked list : so you can put your students everywhere in the memory. For instance : (A) (some other data) (B) (some other data) (C). There, you do not need to have a big, unique, free space in your memory.
But there is a problem : when you're using an array, it is very easy to find your datas. You just need to know the beginning of the array, and then, calculate the number of the data you want starting from the first item of the array. This is what it means when you're doing : myArray[4], for instance.
Since you're going to use a linked list, you can't calculate where the datas are, because, and that's the idea of the linked list, they could be anywhere. That's why the list is linked : you need to provide a way to go from datas to datas. And you're going to use C programmers best-friends : pointers.
So you have to improve your struct :
struct student{
char* lastName;
char* firstName;
int age;
float grade[3];
student* next
}
And you're going to need a very important variable : student* listStart. This is the equivalent of the "array" variable you're using. The place where you're going to need to start if you want to access your data.
Now, what you have to do basically :
All right, it's a bit confusing at first. But if you manage to do this, you'll have learn something useful, without us doing your homework for you. Good luck;
Upvotes: 2
Reputation: 1057
Okay I might be able to help. However, some more info is needed. What does the text file contain? If you need randomly generated ages...that is easy just use a random number generator method (google) and define its bounds call this through a loop each time to get a random age. Also, do that for the grades. As for the array, I am pretty sure you can either create a List of your struct (c# is like List mylist = new List();) Or, you can do an array of your struct.
If you are using a text file that contains partial data (like only first and last names). I would read in the text file in a loop, assign the first and last name. Then loop through the length of the array/list you have calling a random number for age and grade each time. Then assign that position in the loop each age grade. So student[i].age = *random gen method returned val.
Upvotes: 0