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.
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 :
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;