473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

I went a help

I am a new student for language C
, I do not have any course related to

C and this question no related to any kind of home work or assignment

I am truing to learning a C language and i went to execute some
problem here and I went the code of this program . If you can help
me do it please other wise think you for every body .
------------------------------------------------------------------------
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.

I have to know the solution of this program then I will truing to
solve some kind of these Questions?

Mar 15 '06 #1
15 1927
On Wednesday 15 March 2006 19:06, join opined (in
<qK******************************@giganews.com>) :
I am a new student for language C, I do not have any course related to
C and this question no related to any kind of home work or assignment
If you really want to learn C, read a good textbook (try K&R2 for a
start), and try to solve this problem by yourself. Once you come across
a specific problem, come back here and ask.
I am truing to learning a C language and i went to execute some
problem here and I went the code of this program . If you can help
me do it please other wise think you for every body .
What follows is so obviously a homework assignment... Even if you're
using somebody else's just in order to do some self-learning, you'd
still be better off following my advice above.
-------------------------------------------------------------------
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.

I have to know the solution of this program then I will truing to
solve some kind of these Questions?


--
BR, Vladimir

"Mind if I smoke?"
"I don't care if you burst into flames and die!"

Mar 15 '06 #2

join wrote:
I am a new student for language C
, I do not have any course related to

C and this question no related to any kind of home work or assignment

I am truing to learning a C language and i went to execute some
problem here and I went the code of this program . If you can help
me do it please other wise think you for every body . if ur real intent is to learn C then why don't u give it a shot. Try to
write a program and if u face any problem, comp.lang.c is here ------------------------------------------------------------------------
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.

I have to know the solution of this program then I will truing to
solve some kind of these Questions?


Mar 15 '06 #3
an********@gmail.com wrote:

if ur real intent is to learn C then why don't u give it a shot. Try
to write a program and if u face any problem, comp.lang.c is here

It facilitates communication on this newsgroup if you don't use
shorthand like "u" and "ur".


Brian
Mar 15 '06 #4
Hi join,

There are lots of sites you can get help learning C. I visit here quite
often :-) Join up (its free) and post a question and you should get
help. However, you will be expected to do the coding yourself so try
the exercise and post what you've managed to write.

http://www.experts-exchange.com/Prog...g_Languages/C/

Paul

Mar 15 '06 #5
"Paul" <Ca**********@AOL.com> writes:
Hi join,
There are lots of sites you can get help learning C. I visit here quite
often :-) Join up (its free) and post a question and you should get
help. However, you will be expected to do the coding yourself so try
the exercise and post what you've managed to write.

http://www.experts-exchange.com/Prog...g_Languages/C/


Please provide context; see <http://cfaj.freeshell.org/google/>.

I haven't looked at the above-mentioned site, so I can't comment on it
directly. A lot of people have posted links here to C programming
sites; most of them aren't very good. Caveat emptor.

I'd be (pleasantly) surprised if that site where as good as
comp.lang.c in terms of participation by actual experts and correction
of mistakes.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 16 '06 #6
Keith Thompson said:
http://www.experts-exchange.com/Prog...g_Languages/C/

<snip>
I'd be (pleasantly) surprised if that site where as good as
comp.lang.c in terms of participation by actual experts and correction
of mistakes.


I don't think you're going to be pleasantly surprised.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 16 '06 #7
that what i did auntil now but i cause i problem , the output of this
code id

null 0.000 0
what the problem here?

also, what is the best way of finding the max,min ,average of the
gpa?
code:
#include <stdio.h>

#include <stdlib.h>
typedef struct student {
char *name;
int id;
float gpa;
} stu;

stu nig[5];
int main(){
int i,n,avg,min,max;
int id;
float gpa;
char name[5];
FILE *inputFile;
inputFile = fopen("q4.txt","r");

if (inputFile) printf("File succesfully opened for
reading!\n");
else {
printf("File not opened\n");
exit(1);
}
for(i=0;i<5; i++){
fscanf(inputFile, "%s %d
%f",nig.name,nig[i].id,nig[i].gpa);

printf("are:%s%d,%f",nig[i].name,nig[i].id,nig[i].gpa);

//}
fclose(inputFile);

}
return 0;

}

Mar 17 '06 #8
always leave some context in your post. Ie. include what you are
replying to (like I did).

join wrote:
that what i did auntil now but i cause i problem , the output of this
code id

null 0.000 0

what the problem here?
see my comments furthur down
also, what is the best way of finding the max,min ,average of the
gpa?
for min and max simply scan the array (use a for loop) for the
smallest and largest values so far. For an average add the numbers
up and divide by the number of numbers. If you're clever you'll be
able to use one loop to do all three tasks!
code:
<snip>

by the time I saw your code it looked like this:-
stu nig[5];
int main(){
int i,n,avg,min,max;
int id;
float gpa;
char name[5];
FILE *inputFile;
inputFile = fopen("q4.txt","r");


<snip>

well-laid out code is much easier to read. Your code may have
been corrupted by your posting software (or my news reader).
Avoiding tabs in posted code may help.

This is your code relaid out:-
My comments are tagged like this /***

<code>

#include <stdio.h>
#include <stdlib.h>

typedef struct student
{
char *name;
int id;
float gpa;
} stu;

stu nig[5];
int main()
/***
int main (void)
is better */

{
int i, n, avg, min, max;
/*** try to avoid multiple declarations on one line */

int id;
float gpa;
/*** use double unless there is good reason */

char name[5];
/*** what if someone has a name with more than 4 characters in it? */

FILE *inputFile;

inputFile = fopen("q4.txt", "r");

if (inputFile)
/*** good! you checked the return value! */

printf("File succesfully opened for reading!\n");
else
{
printf("File not opened\n");
exit(1);
/*** technically exit(1) is implementation defined. Use EXIT_FAILURE */

}

for (i = 0; i < 5; i++)
/*** try to avoid embedding constants in your code. Use a macro like
#define NUMBER_OF_RECORDS 5
*/

{
fscanf(inputFile, "%s %d %f", nig.name, nig[i].id, nig[i].gpa);
/*** eek!
1. ***always*** check the return value of fscanf() etc.
2. name doesn't point at anything. You just wrote the name to
the null pointer. This is Very Bad
3. fscanf(0 wants *pointers* to integers and floats. You've written
to address zero. Very Bad. (note these values are only zero
because nig is at file scope).
*/

printf("are:%s%d,%f", nig[i].name, nig[i].id, nig[i].gpa);
//}
/** what's this?

fclose(inputFile);
}

return 0;
/*** good! main() returned something */
}

</code>
--
Nick Keighley

Mar 17 '06 #9
char name[5];
/*** what if someone has a name with more than 4 characters in it? */
5 name for the number of names in input file that what I did.

printf("are:%s%d,%f", nig.name, nig[i].id, nig[i].gpa);
//}
/** what's this?

I went to read the names and ids and gpas than I went to print them

if it is wrong what is the correction?

Mar 17 '06 #10
LEAVE SOME CONTEXT IN YOUR POST!!!!!

I told you this last time. I posted an example with context. Please do
the same. If you are using google then follow the advice given in other

posts on how to work around it. Note *I* post from google and I can
make it work.
join wrote:
char name[5];
/*** what if someone has a name with more than 4 characters in it? */

5 name for the number of names in input file that what I did.
I'm having troble understanding you. There are names in the original
data file that are five characters long. These names will require 6
characters of storage (one for the terminating '\0' (nul character)).
6 characters won't fit in array of 5 chars.

The fact that your data file contained five records is irrelevent.
printf("are:%s%d,%f", nig.name, nig[i].id, nig[i].gpa);
//}
/** what's this?
I meant the "//}". I assume it's just sloppiness

I went to read the names and ids and gpas than I went to print them

if it is wrong what is the correction?


see my previous post, where I pointed out several errors in your code.
--
Nick Keighley

Mar 17 '06 #11
Nick Keighley wrote:
LEAVE SOME CONTEXT IN YOUR POST!!!!!

I told you this last time. I posted an example with context. Please do
the same. If you are using google then follow the advice given in
other posts on how to work around it.


That doesn't seem to be his problem, his headers indicate:

User-Agent: newsSync (C++Talk.NET)
I'm guessing that's one of those other usenet->web forum sites, but I'm
not sure. It's possible that it has no auto-quote functionality, so he
needs to do it himself in that case.


Brian
Mar 17 '06 #12
"Default User" <de***********@yahoo.com> writes:
Nick Keighley wrote:
LEAVE SOME CONTEXT IN YOUR POST!!!!!

I told you this last time. I posted an example with context. Please do
the same. If you are using google then follow the advice given in
other posts on how to work around it.


That doesn't seem to be his problem, his headers indicate:

User-Agent: newsSync (C++Talk.NET)
I'm guessing that's one of those other usenet->web forum sites, but I'm
not sure. It's possible that it has no auto-quote functionality, so he
needs to do it himself in that case.


Then maybe (and I can't believe I'm saying this) he should switch
to Google Groups.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 17 '06 #13
Keith Thompson wrote:
"Default User" <de***********@yahoo.com> writes:
Nick Keighley wrote:
LEAVE SOME CONTEXT IN YOUR POST!!!!!

I told you this last time. I posted an example with context.
Please do >> the same. If you are using google then follow the advice
given in >> other posts on how to work around it.

That doesn't seem to be his problem, his headers indicate:

User-Agent: newsSync (C++Talk.NET)
I'm guessing that's one of those other usenet->web forum sites, but
I'm not sure. It's possible that it has no auto-quote
functionality, so he needs to do it himself in that case.


I took a bit of a search, and I was right. It's a usenet to PHPBB forum:

http://www.cpptalk.net/
Then maybe (and I can't believe I'm saying this) he should switch
to Google Groups.


I notice that the messages there have a button that says, "reply with
quotes", but without registering I can't try it to see how that works.
Most of those forums have their own quoting method using a tag system
that I doubt would translate well to newsgroups.

Brian

Mar 17 '06 #14
On Fri, 17 Mar 2006 05:06:17 -0600,
h_*******@hotmail-dot-com.no-spam.invalid (join) wrote:
that what i did auntil now but i cause i problem , the output of this
code id

null 0.000 0
How did you execute the code when it has syntax errors? Is this your
real code or did you retype it? You should always use cut and paste.


what the problem here?

also, what is the best way of finding the max,min ,average of the
gpa?
code:
#include <stdio.h>

#include <stdlib.h>
typedef struct student {
char *name;
int id;
float gpa;
} stu;

stu nig[5];
int main(){
int i,n,avg,min,max;
int id;
float gpa;
char name[5];
FILE *inputFile;
inputFile = fopen("q4.txt","r");

if (inputFile) printf("File succesfully opened for
reading!\n");
else {
printf("File not opened\n");
exit(1);
}
for(i=0;i<5; i++){
fscanf(inputFile, "%s %d
%f",nig.name,nig[i].id,nig[i].gpa);
nig.name is syntactically incorrect. You need nig[i].name, just like
with id and gpa. nig is an array of struct. nig[i] is the i-th
struct in the array. And nig[i].name is the pointer to char in that
struct.

Unfortunately, you have an additional problem. nig is a global array.
It is initialized by default such that each element of the array (in
this case that means each member of each element) is set to the
correct form of zero. Since name is a pointer, name is initialized to
NULL. Your call to fscanf directs that function to store the input
name there. You are not allowed to store data where a NULL pointer
points. You need to initialize all five nig[i].name pointers to point
somewhere in memory that belongs to your program.

printf("are:%s%d,%f",nig[i].name,nig[i].id,nig[i].gpa);
You should add a \n to the end of your format string.

//}
fclose(inputFile);

}
return 0;

}

Remove del for email
Mar 18 '06 #15
On Fri, 17 Mar 2006 09:06:42 -0600,
h_*******@hotmail-dot-com.no-spam.invalid (join) wrote:
char name[5];
/*** what if someone has a name with more than 4 characters in it? */
5 name for the number of names in input file that what I did.


It doesn't matter what you do with this array since you never use it
in your program.
Remove del for email
Mar 18 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ryan R. Rosario | last post by:
Hi - **I apologize for posting this again. I dont think my first one went thru** I am in the process of converting a table based website over to a CSS based website and I am having a problem....
1
by: Kenny | last post by:
I have an aspx page (containing a framesets) that appears just fine in the browser (IE6). But there is a little yellow "caution" icon that shows up at the bottom of the browser window with a...
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
21
by: sathyashrayan | last post by:
Went to an interview and got f**ked ... (OT): They wanted me to do this in 2 hr. Create a address book, insert the data in mysql. And create a admin panel where the user has the privileges to...
13
by: RichardL | last post by:
Hi, I tried creating "left" and "right" links on the top of a page I'll be building. I intended them to be on the opposite sides of the first line. That failed. What am I missing? TIA,...
1
by: jonny | last post by:
Went from using Visual Web Develop express to Visual Studio 2005 and getting error when trying to open project. Error message: "One or more projects in the solution could not be loaded for the...
6
by: kang jia | last post by:
hi this is my database in models.py in the Django application, however, i don not know where is the error, i have tried many times, but always got error to set up database, sometimes they claim...
6
by: angi35 | last post by:
Yesterday (while I was out sick), the Access 2000 database I've been refining and further developing for a company suddenly went read-only for a day. Near as I can tell, it's the backend that went...
5
by: Anoj | last post by:
Hi I was surprised to see "went" keyword in Visual Basic Language Specification 9.0 document which I downloaded this morning from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.