C : program to calculate GPA for students - c

Whenever I insert 2 or more students, the program gives the same gpa from the first student. I insert for both every time, and how can I display the max gpa and who got it?
#include <stdio.h>
FILE *f;
struct student
{
char name[20];
int id;
int n;
float sum;
float gpa;
} s[100];
float FUN_GPA(int n);
int main ()
{
int m,i,x,b;
float max=0;
s[i].sum=0;
printf("Please Enter Number Of Students : ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("\nStudent %d : \n",i+1);
printf("Name : ");
scanf("%s",&s[i].name);
printf("ID : ");
scanf("%d",&s[i].id);
printf("Number Of Courses : ");
scanf("%d",&s[i].n);
}
printf("\nEnter Student Marks");
for(i=0;i<m;i++)
{
printf("\n\n\t\t\t......[ Student %d ]......\n",i+1);
for(x=0;x<s[i].n;x++)
{
printf("Course %d Mark : ",x+1);
scanf("%d",&b);
s[i].sum+=b;
}
printf("\nSum Of The Courses Marks = %3.f",s[i].sum);
printf("\nGPA For Student %d = %f",i+1,FUN_GPA(s[i].gpa));
}
if (s[i].gpa>max)
max=s[i].gpa;
printf("\n\nHighest GPA is done by Student %d with GPA = %f",i,max);
{
if((f=fopen("d:\\STUDENTS.txt","w"))==NULL)
printf("\ncant open file ");
// if((f=fopen("d:\\STUDENTS.txt","r"))==NULL)
// printf("\ncant open file ");
fprintf(f,"Name : %s\t ID : %d\t GPA = %f \n",s[i].name,s[i].id,s[i].gpa);
fclose(f);
}
}
float FUN_GPA(int i)
{
s[i].gpa=0;
s[i].gpa=s[i].sum/s[i].n;
return s[i].gpa;
}

Replace FUN_GPA(s[i].gpa) by FUN_GPA(i).
Put this inside the loop; not much use having it outside:
if (s[i].gpa>max)
max=s[i].gpa;
The same applies to this initializer, by the way:
s[i].sum=0;
In general, do not use variable i outside the loop; this is bad practice and in your particular case, totally wrong!
i is not the index of the best performing student; it is not even a correct index once the loop ends, since it refers to the array element immediately following the last registered student. Please introduce a new variable to keep track of the index of the best performer so far. Instead of i, use this new variable in the lines to follow:
fprintf(f,"Name : %s\t ID : %d\t GPA = %f \n",s[i].name,s[i].id,s[i].gpa);
Same here; do not forget to add 1 like you did everywhere else already!
printf("\n\nHighest GPA is done by Student %d with GPA = %f",i,max);
General advice: use descriptive names. i is OK afaic; s and n are not so great, m, x and b would have been good for 50 lashes from my old teachers for sure!

This needs to be in a for loop (you're only checking one as it is currently written):
if (s[i].gpa>max)
max=s[i].gpa;
You'll probably also want to keep track of which i had the max.
So something like:
int iwithmax;
for ( i = 0 ; i < m ; i++) {
if (s[i].gpa > max) {
max = s[i].gpa;
iwithmax = i;
}
}
and change print statement accordingly:
printf("\n\nHighest GPA is done by Student %d with GPA = %f",iwithmax,max);

Related

Failing to calculate amount

This C program is to print an invoice, the only problem is that it won't output the unit price and calculate the amount. Can someone show me how. This is now the full code. And if there is any other error, please help.
struct PRODUCTSINFO {
int code; // products number
int qty;
char name[ 50 ]; // products name
double unit_price; // account unit_price
}; // end structure PRODUCTSINFO
struct PRODUCTSINFO products[100] ;
int main(){
int p;
int x ;
int i=0;
double amount=0;
printf("Enter the amount of products to be purchased : ");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("\nEnter product code #%d : ",i+1);
scanf(" %d",&products[i].code);
printf("\nEnter product name#%d:",i+1);
scanf("%s",&products[i].name);
printf("\nPlease quantity#%d : ",i+1);
scanf("%d",&products[i].qty);
printf("Enter unit price#%d:",i+1);
scanf("%.lf",&products[i].unit_price);
fflush(stdin);
}
system("cls");
printf("************************INVOICE*******************************\n");
printf("-----------------------------------------------\n);
printf("S/N | CODE | NAME OF PRODUCTS | QUANTITY | UNIT PRICE |AMOUNT \n");
printf("------------------------------------------------------\n");
for(i=0;i<x;i++){
printf("\n%d",i);
printf("\t %d",products[p].code);
printf("\t %s",products[p].name);
printf("\t\t\t%d",products[p].qty);
printf("\t\t%.2f",products[p].unit_price);
p++;
amount=products[p].qty*products[p].unit_price;
printf("\t%.2f\n",amount);
}
}
You haven't shown the complete code.
Q: Is x declared to be an integer (or better,unsigned)?
Q: Are you sure x > 0 when you start the loops?
... AND ...
If you're running the program as a CMD prompt in Windows ... be sure to add getchar() before the end of the program. Otherwise, the program will exit and your window could disappear before you see any output.
ALSO:
Note Weather Vane's suggestion about your scanf formatting errors. Here are two good links:
https://beej.us/guide/bgc/output/html/multipage/scanf.html
https://linux.die.net/man/3/scanf
'Hope that helps
Your code has lot of syntax errors. I just fixed it. And here is the working code of it.
Compare this code with the previous one to realize your mistakes.
struct PRODUCTSINFO {
int code; // products number
int qty;
char name[ 50 ]; // products name
double unit_price; // account unit_price
}; // end structure PRODUCTSINFO
struct PRODUCTSINFO products[100] ;
int main(){
int p;
int x ;
int i=0;
double amount=0;
int total = 0;
printf("Enter the amount of products to be purchased : ");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("\nEnter product code #%d : ",i+1);
scanf(" %d",&products[i].code);
printf("\nEnter product name#%d:",i+1);
scanf("%s",products[i].name);
printf("\nPlease quantity#%d : ",i+1);
scanf("%d",&products[i].qty);
printf("Enter unit price#%d:",i+1);
scanf("%lf",&products[i].unit_price);
}
printf("************************INVOICE*******************************\n");
printf("-----------------------------------------------\n");
printf("S/N | CODE | NAME OF PRODUCTS | QUANTITY | UNIT PRICE |AMOUNT \n");
printf("------------------------------------------------------\n");
for(i=0;i<x;i++){
printf("\n%d",i);
printf("\t %d",products[i].code);
printf("\t %s",products[i].name);
printf("\t\t\t%d",products[i].qty);
printf("\t\t%.2f",products[i].unit_price);
p++;
amount=products[i].qty*products[i].unit_price;
total += amount;
printf("\t%.2f\n",amount);
}
Printf("%d",total);
return 0;
}

New to programming. Need help adjusting code to allow for undetermined number of students

Need help adjusting code to allow for undetermined number of students. Tried to modify with help I received last week, but it seems like I am not doing it correctly.
#include <stdio.h>
int main ()
{
/* variable definition: */
char StudentName[100];
float ExamValue, Sum, Avg;
int students,exams;
// Loop through 5 Students
for (students=0; students <5 ; students++)
{
// reset Sum to 0
Sum =0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
// Nested Loop for Exams
for (exams=0; exams < 3; exams++)
{
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0;
}
Not sure how I need to go about changing the code. Any help will be greatly appreciated
Not sure I understand correctly, I understood you want to loop an undertemined amount of students.
I will assume you want to stop if certain keyword is added as a student name, let's say "quit".
Without changing your structure much, it would look like this.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main (){
char StudentName[100];
float ExamValue, Sum, Avg;
int students,exams;
bool in = true;
while (in) {
Sum =0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
if( strcmp(StudentName, "Quit") == 0 || strcmp(StudentName, "quit") == 0){
in = false;
break;
}
for (exams=0; exams < 3; exams++){
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0;
}

Wrong output by C Program

Can any one find the flaw and explain the below program? I will brief you,
1) Using structures in the program to store information about a products.
2) Finding the lowest price and displaying it.
All works fine but output does not give the lowest value.
#include <stdio.h>
#include <malloc.h>
struct product{
int code;
char name[30];
float price;
int qty;
};
void prodata(struct product *p,int n){
int i;
for (i=0;i<n;i++){
printf("\n Enter the Item Code. : ");
scanf("%d",&p[i].code);
fflush(stdin);
printf("\n Enter the Item Name. : ");
scanf("%s",&p[i].name);
printf("\n Enter the Item Price. : ");
scanf("%f",&p[i].price);
printf("\n Enter the Item Quantity in hand. : ");
scanf("%d",&p[i].qty);
}
}
void dispdata(struct product *p, int n){
int i;
int min;
min = 0;
for ( i = 1 ; i < n ; i++ ){
if (p[i].price < p[min].price) {
min = i;
}
}
printf("\n ** The Cheapest Product ** \n");
printf("\n The Product Code : %d \n",p[min].code);
printf("\n The Product Name is : %s \n",p[min].name);
printf("\n The Product Price is : %f \n",p[min].price);
printf("\n The Product Stock : %d \n",p[min].qty);
}
main(){
struct product *p=NULL;
int n;
printf("\n Product Information. \n");
printf("\n Please Enter the Number of Items : ");
scanf("%d",&n);
while(n<=1){
printf("\n Please Enter correct number of items : ");
scanf("%d",&n);
}
p =(struct product*)malloc(sizeof(struct product)*n);
prodata(p,n);
dispdata(p,n);
return 0;
}
if (p[i].price < min) {
min = i;
}
You're setting the min to be the index, not the actual price of the object.
In addition to Daniel's response make sure to index i at 0. Your for statements should be:
for(i = 0; i < n; i++) {....
Right now your missing 1 whole product since your for loop starts counting at 1 and ends after i iterates to (n-1) and executes the code block in the for loop

I have had a lot of trouble modifying this current code to set an undetermined amount of students for this C code

#include <stdio.h>
int main ()
{
/* variable definition: */
char StudentName[100];
float ExamValue, Sum, Avg;
int students,exams;
// Loop through 5 Students
for (students=0; students <5 ; students++) {
// reset Sum to 0
Sum =0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
// Nested Loop for Exams
for (exams=0; exams < 3; exams++)
{
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0; }
This is the code I need to change so that instead of inputing 5 student names with 3 exam scores, It will be an undetermined amount of students with 3 quiz scores. I could write it so that the user could enter the amount of students they want, but I dont think thats what they mean by "undetermined". What is the way that you could write it so you could enter as many student names you want. Im always willing to learn more, and any help is appreciated. Thank you.
You could do that by replacing the for loop with the while loop:
int bContinue = 1;
while (bContinue)
{
// your code goes in here
printf("Enter 0 if you wanna stop\n");
scanf("%d", &bContinue);
}
#include <stdio.h>
int main ()
{
/* variable definition: */
char StudentName[100];
float ExamValue, Sum, Avg;
int students,exams,num_values;
printf("How many students to find the average for? ");
scanf("%d", &num_values);
// Loop through 5 Students
for (students=0; students < num_values ; students++) {
// reset Sum to 0
Sum =0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
// Nested Loop for Exams
for (exams=0; exams < 3; exams++)
{
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0; }
I figured out that the users could input the amount so I came up with this code. However I am going to practice the other answers given. Thank You!

C: Array of Structs (Input into int array within array of structs)

Hi I have to create a database that stores students number, name and also stores an array of course marks (1-N) in C Programming Language.
Everything worked until I started coding for the array of course marks. Then every time I compiled the code it kept crashing as soon as it asked to input the course marks.
Can you please tell me where I'm going wrong in my programming for this task? I have attached it to this message.
The program worked for inputting the name, student number, however I could not get the program to input array of marks. I have asked how many course marks to be entered and then used a for loop within the "void insert(void)" function to keep inputting the course marks into the array *marks. I am referring specifically to lines 24 to 30 in my programming code.
Always at this point the program kept crashing and I could not proceed further to enter more names or print the stored student details.
I think there is a problem with this part:
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n");
scanf("%d", &(list[num_students].marks[num_marks]));
}
Anyway here is the full code:
#include <stdio.h>
#include <string.h>
struct student{
int number;
char name[10];
int marks[5];
};
struct student list[10];
int num_students = 0;
int num_marks = 0;
int *p;
void insert(void)
{
int student_number;
int i;
printf("Enter number: \n");
scanf("%d", &list[num_students].number);
printf("Enter NAME: \n");
scanf("%s", &list[num_students].name);
printf("Enter NO of courses: \n");
scanf("%d", num_marks);
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[num_marks]));
}
num_students++; // HOW DO WE INPUT ARRAY MARKS??? MARK1: , MARK2: , MARK3 ,
}
void printtest(void)
{
int i;
for (i=0; i < num_students; i++)
{
printf("Name: \n");
puts(list[i].name);
printf("Number: %d \n", list[i].number);
printf("Mark: %d /100 \n", list[i].marks);
printf("\n");
}
}
int main(void)
{
int code;
int opt1;
int courses, i, k, j, counter;
for (;;){
printf("Enter operation code: \n");
printf("(1) ADD NEW STUDENT DETAILS: \n");
printf("(2) DISPLAY REPORT OF ALL STUDENTS: \n");
scanf(" %d", &code);
switch (code){
case 1 :
insert();
break;
case 2 :
printtest();
break;
default:
printf("Illegal code\n");
printf("\n");
}
}
}
Apart from what others pointed out, I'd like to draw your attention towards the following:
void insert(void)
{
int student_number;
int i;
printf("Enter number: \n");
scanf("%d", &list[num_students].number);
printf("Enter NAME: \n");
scanf("%s", &list[num_students].name);
printf("Enter NO of courses: \n");
scanf("%d", num_marks);
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[num_marks]));
}
num_students++;
}
void printtest(void)
{
int i;
for (i=0; i < num_students; i++)
{
printf("Name: \n");
puts(list[i].name);
printf("Number: %d \n", list[i].number);
printf("Mark: %d /100 \n", list[i].marks);
printf("\n");
}
}
There are three problematic statements:
scanf("%s", &list[num_students].name); This is why beginners should use compilers with all warnings enabled.
printf("Mark: %d /100 \n", list[i].marks); What did you declare marks as in the first place?
scanf("%d", num_marks); Seems like you put & operator where not needed and ignore where needed. Read your textbook before asking a question next time.
Seems like you're having a tough time understanding the concept of arrays and pointers. Read your text book thoroughly before venturing into the world of pointers. If you don't use them correctly, even compiler can't help you.
Also, even if I don't expect your program to have a robust input mechanism, at least array bounds checking is expected. Learn good habits from the beginning. They'll save a lot of your time while debugging later.
Seems to be an error in:
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[num_marks]));
}
The crash is probably because num_marks as index indexes beyond the array. Change to:
for (i= 0; i < num_marks; i++)
{
printf("Enter Course Mark: \n" );
scanf("%d", &(list[num_students].marks[i]));
}

Resources