Question:
Write a C program to create a structure Student containing Name , Id and n (n number of the courses). Ask the user to enter data (Name, Id )?
slr
2014-04-22 07:52:13 UTC
Write a C program to create a structure Student containing Name , Id and n (n number of the courses). Ask the user to enter data (Name, Id , n) of m student (m number of the students entered by user) in the main function.
Find the Grade Point Average (GPA) (GPA = sum of grads of all courses/ number of the courses)of each student through the function Fun_GPA in your header file(name of the header file is your name), this function takes one parameter (number of the courses) and return float number (GPA), and print name, Id and GPA for all students in a text file called “STUDENTS".
Read information of all students (ID, Name and GPA) from the file STUDENTS, and find the highest GPA and the name of the student who secured it through function which is built in the header file. Develop the program to use a simple dynamic data structures
Five answers:
MichaelInScarborough
2014-04-23 03:16:23 UTC
The Student struct I offered contains an anonymous union.

The n member (number of classes) and the GPA member

(GPA average of student) can both be addressed in the same

way as any of the other struct members (. or -> notation)

As you can see I use n for input from user and GPA for

input from file (GPA average).

Try it out, it works. In case that is awkward you could

introduce 2 structs one with an int and the other with

a float member.

The last paragraph of your assignment is partly implemented ...

scanf's problem is that there are always some user inputs pending

in the keyboard buffer. I handled this with fgets and sscanf. However

you also could read this:

http://stackoverflow.com/questions/17248442/flushing-stdin-after-every-input-which-approach-is-not-buggy

and adapt your code accordingly.



Here is the code:



#include

#include



struct Student {

char szName[64];

int id;

union {

int n;

float GPA;

};

};



float Fun_GPA(int);

void get_student(struct Student *);

void find_GPA_HIGH(struct Student *, const int, const char *);



int main()

{

const char szFile[] = "STUDENTS";

float GPA = 0.0f;

struct Student s;

struct Student highest;

int m = 0;

char line[64];

printf("Number of student> ");

fgets(line, sizeof(line), stdin);

sscanf(line, "%d", &m);

FILE *f = fopen(szFile, "wt");

for (int i = 0; i < m; i++) {

get_student(&s);

float n = Fun_GPA(s.n);

s.szName[strlen(s.szName) - 1] = 0;

fprintf(f, "%s\n%d\n%g\n", s.szName, s.id, n);

}

fclose(f);

m = 3;

find_GPA_HIGH(&highest, m, szFile);

fprintf(stdout, "\nStudent with highest GPA\n%s\n%d\n%.2f\n", highest.szName, highest.id, highest.GPA);

return EXIT_SUCCESS;

}



void get_student(struct Student *s)

{

char line[128];

printf("name> ");

fgets(s->szName, sizeof(s->szName), stdin);

printf("id ");

fgets(line, sizeof(line), stdin);

sscanf(line, "%d", &s->id);

printf("How many courses> ");

fgets(line, sizeof(line), stdin);

sscanf(line, "%d", &s->n);

}



float Fun_GPA(int iCourseNumber)

{

float fSum = 0;

int iGrade = 0;

char buffer[124];

for (int i = 0; i < iCourseNumber; i++) {

printf("Grade for Course %d> ", i + 1);

fgets(buffer, sizeof(buffer), stdin);

sscanf(buffer, "%d", &iGrade);

fSum += iGrade;

}

fSum /= iCourseNumber;

return fSum;

}



void find_GPA_HIGH(struct Student *highest, const int m, const char *szFilename)

{

FILE *f;

struct Student s;

s.GPA = 0.0f;

f = fopen(szFilename, "rt");

if (!f) {

fprintf(stdout, "Could not open file %s\n", szFilename);

return;

}

char line[64];

for (int i = 0; i < m; i++) {

fgets(s.szName, sizeof(s.szName), f);

fgets(line, sizeof(line), f);

sscanf(line, "%d", &s.id);

fgets(line, sizeof(line), f);

sscanf(line, "%f", &s.GPA);

if (s.GPA > highest->GPA) {

*highest = s;

}

}

fclose(f);

}
ProudAirman1
2014-04-22 08:45:26 UTC
Above user misleads a little bit with the "struct Student fred;" line.

It should just say "Student fred;



After creating your student struct, read from the user the number of students in the class(simple)

Store it to m



Then dynamically create a new "Student" array using that number of students



Student *myStudents = new Student[m]



Write a for loop which progresses through each step in the array of Students, asks for the name, id and all that and stores it in the proper array space



for example, at index 0 you would store something to:

myStudents[index].id = whatevs



Your GPA function will need to know what the actual grades are, not just the number of courses, so I think you are missing some information, but that is a simple mathematical function.



Use ofstream or fstream to create and write the desired information to an output file.
amania_r
2014-04-22 08:18:35 UTC
struct Student {

char name[40];

int id;

int n;

};



struct Student fred;



...

strcpy(fred.name, "Fred Smith");

fred.id = 123456;

fred.n = 42;



You can adapt this to get user input
anonymous
2016-03-10 04:15:25 UTC
The "birthdate" error is probably capitalization. It's declared as "birthDate" and case is important in C. For allocation, the calloc() function (also in ) is nicer in many ways than malloc(). It takes the items size and item count as separate arguments, so there are cases where you don't need an extra set of parentheses. It also zeros the memory obtained, just like a static or global array declaration (and unlike an array that's a local variable). As for the pointer vs. array, you'll find that there's a strong association between pointers and arrays in C. It's sometimes called the "array-pointer duality", and leads a few to (mistakenly) say that arrays and pointers are identical in C. They are merely very similar. In C, every pointer is presumed to point to an array. Every array is passed to functions as a pointer to its first element. In fact, nearly all uses of an array name in an expression will be implicitly converted to a pointer to the first element. In nearly every case (sizeof expressions are one exception) use of an array name a is equivalent to (&a[0]). The array indexing operator a[x] is FORMALLY defined as *(a+x). So much so that if a is an array then a[1] means *(a+1). (Remember that a is not only an array, but a pointer its first element.) The totally weird part about the above is, since a+1 and 1+a are both valid pointer expressions, then the formal definition makes 1[a] a valid alternative to a[1]. Programmers who have worked in C/C++ for decades will swear that's a lie. Try it on your favorite compiler before swearing at me, though. Needless to say, you shouldn't use expressions like 1[a] if you want to make friends. (...and keep the ones you have! :^)
anonymous
2014-11-13 01:47:38 UTC
confusing task. look into at the search engines. that could actually help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...