LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help me with this problem (https://www.linuxquestions.org/questions/programming-9/help-me-with-this-problem-424313/)

zeyad 03-13-2006 01:07 AM

help me with this problem
 
Hi evreone My name is zeyad and i am beginner in C language
And I tried to solve this question but I couldn’t

Would you plz help me and solve it

Thank you for helping


Q. Write a program which can read a list of names, ids and gpas from a text file and store it into an array of structures. The text file might look as follows:

Faisal 991234 3.5
Aref 212334 4.0
Fifi 913542 2.7
Sarim 678235 1.3
Majed 124523 4.0
……………………………………………………………………

After the entire file is read, calculate the and print
(i) the maximum of all gpas and the corresponding student name and ID
(ii) the minimum of all gpas and the corresponding student name and ID
(iii) the average of all gpas.

bc8o8 03-13-2006 02:02 AM

This seems a lot like a homework problem, and although I'm not comfortable giving you a solution to it (because you really don't learn much that way) I can offer some help.

Since this is LinuxQuestions.org, I'll assume your on a Linux (or at least Unix) system. Given that assumption, you need to know that man pages are your best friend.

All the information you need about reading and writing file streams can be found by typing the following into the command line:
man fopen
man fscanf

(Once you've used fopen, you can read a file with fscanf exactly the same as you would read from the command line with scanf) One thing of particular interest to notice is that fscanf returns EOF when no data is left, this can be very useful in loops.

When setting up the data structures, make sure to reserve enough room for the longest possible name. Once you've read in all the data (into an array), printing it out for verification would be helpful. When you can verify that all that is working correctly you can loop through and pretty easily calculate averages and obtain pointers to the largest and smallest.

For efficiency, I must point out that if you never need the data again you can get away with only one loop and three instances of your data structure. You can simply read a line from the file, keep a running average of GPAs, compare the current GPA to variables containing the largest and smallest GPAs previously read. Once you have (if necessary) updated the largest and/or smallest variables then you can read the next line to the same variable as your last one.

Hope that helps...

(I also must point out that, in order to get the best results from a forum, a more specific subject than "help me with this problem" should be used)

zeyad 03-13-2006 06:57 AM

thanx bc8o8 for trying to help
as you said it is HW but not for me
i want to solve it to learn more about structiure

i try to solve it but when i try to run it
the it gives me segmentation falt

so plz if you give me the solution i eill be more thankful

unSpawn 03-13-2006 08:22 AM

Like bc8o8 already said, from solution-only answers you'll learn nothing. I suggest you post what code you've tried and *then* maybe someone could point you in the right direction.


All times are GMT -5. The time now is 11:45 AM.