Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-13-2006, 01:07 AM   #1
LQ Newbie
Registered: Mar 2006
Posts: 2

Rep: Reputation: 0
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.

Last edited by zeyad; 03-13-2006 at 06:54 AM.
Old 03-13-2006, 02:02 AM   #2
Registered: Jun 2003
Location: Boston, MA
Distribution: Debian
Posts: 57

Rep: Reputation: 15

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, 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)
Old 03-13-2006, 06:57 AM   #3
LQ Newbie
Registered: Mar 2006
Posts: 2

Original Poster
Rep: Reputation: 0
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
Old 03-13-2006, 08:22 AM   #4
Registered: May 2001
Posts: 29,383
Blog Entries: 55

Rep: Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558Reputation: 3558
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.

Last edited by unSpawn; 03-13-2006 at 08:24 AM. Reason: //have keybd, cant type


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:25 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration