Operating System - HP-UX
1827332 Members
5995 Online
109963 Solutions
New Discussion

Part II C program im stuck

 
SOLVED
Go to solution
Mikel Howel_1
Advisor

Part II C program im stuck

ive been working on this and im tryint to pull info from the structure. but its not working i need to add this for each students final grade. the 3 quizes are worth 30% of the grade and the 2 test are worth 70%
something like this

=(quizavg * 0.30)+(testsavg * 0.70)
if(c>=90)

then give it a final grade from the weighted grade. anyhelp with this?

printf("You get an A\n");
else if((c>=80)&&(c<89))
printf("You get a B\n");
else if((c>=70)&&(c<79))
printf("You get a C\n");
else if((c>=60)&&(c<69))
printf("You get a D\n");
else
printf("Failed the class\n");

#include

struct student_info{
char fname[26];
char lname[31];
int quiza[4];
int quizb[4];
int quizc[4];
int testa[4];
int testb[4];

};

typedef struct student_info SI;

int main()
{

SI student[5];
int iJunk;
int iCount;
int quiztot;
int quizavg;
int testtot;
int testavg;

for (iCount = 0; iCount < 3; iCount++){
printf("Students First Name:");
scanf("%s", student[iCount].fname );
iJunk = getchar();


printf("Students Last Name:");
scanf("%s", student[iCount].lname );
iJunk = getchar();

printf("Score for Quiz 1:");
scanf("%d", &student[iCount].quiza );
iJunk = getchar();

printf("Score for Quiz 2:");
scanf("%d", &student[iCount].quizb );
iJunk = getchar();

printf("Score For Quiz 3:");
scanf("%d", &student[iCount].quizc );
iJunk = getchar();

printf("Score For Test 1:");
scanf("%d", &student[iCount].testa );
iJunk = getchar();

printf("Score For Test 2:");
scanf("%d", &student[iCount].testb );
iJunk = getchar();

quiztot = SI.quiza + SI.quizb + SI.quizc;
quizavg = quiztot/3;

testtot = SI.testa + SI.testb;
testavg = testtot/2;

}
printf("\n");

printf("Press a key to continue...\n");
iJunk = getchar();

return 0;
}
26 REPLIES 26
Mikel Howel_1
Advisor

Re: Part II C program im stuck

anyone?
Ron Kinner
Honored Contributor
Solution

Re: Part II C program im stuck

I'll give it a shot since no one else has anything to say.

Seems to me you have defined a datastructure type called SI then made an instance of it called student[].


You then went through a routine which filled student[] with info from keyboard input. I assume this part works OK.

Now you say
"quiztot = SI.quiza + SI.quizb + SI.quizc;
quizavg = quiztot/3; "

But SI is now the type and not the student[] so it is not clear to me what SI.testa refers to. I would think you would need to say:

quiztot = student[iCount].quiza + student[iCount].quizb + student[iCount].quizc

Ron

Rajeev  Shukla
Honored Contributor

Re: Part II C program im stuck

Hi try this one with a structure. You have give First and Second name together and in the end it prints the summary also.

#include
struct data {
char fname[26];
char lname[31];
int quiza, quizb, quizc, testa, testb, grade;
};
main()
{
struct data student[3];
int i, q_total, t_total, q_avg, t_avg;
for (i=0; i<3; i++){
printf("Enter the record No. %d\n", (i+1));
printf("Enter the Students first Name & Second Name: ");
scanf("%s %s", &student[i].fname, &student[i].lname);
printf("Enter the Marks %s %s Got in Quiz A: ", student[i].fname, student[i].lname);
scanf("%d", &student[i].quiza);
printf("Enter the Marks %s %s Got in Quiz B: ", student[i].fname, student[i].lname);
scanf("%d", &student[i].quizb);
printf("Enter the Marks %s %s Got in Quiz C: ", student[i].fname, student[i].lname);
scanf("%d", &student[i].quizc);
printf("Enter the Marks %s %s Got in Test 1: ", student[i].fname, student[i].lname);
scanf("%d", &student[i].testa);
printf("Enter the Marks %s %s Got in Test 2: ", student[i].fname, student[i].lname);
scanf("%d", &student[i].testb);
q_avg = ((student[i].quiza)+(student[i].quizb)+(student[i].quizc))/3;
t_avg = ((student[i].testa)+(student[i].testb))/2;
student[i].grade = (q_avg*0.3)+(t_avg*0.7);
if((student[i].grade)>80)
printf("You have Got Grade A\n");
if((student[i].grade)>60 && (student[i].grade)<80)
printf("You have Got Grade B\n");
if((student[i].grade)>40 && (student[i].grade)<60)
printf("You have Got Grade C\n");
if((student[i].grade)<40)
printf("You Have failed\n");
printf("Average=%d\n", (student[i].grade));

}
puts("");
printf("________________________________________________\n");
for (i=0; i<3; i++)
printf("Entry No: %d, \n\t\t Name: %s %s\n\t\t Grade:%d\n", (i+1), student[i].fname, student[i].lname, student[i].grade);
printf("________________________________________________\n");
}


Let me know if u need more help.

Cheers
Mikel Howel_1
Advisor

Re: Part II C program im stuck

on the printf function on the bottom with the lines, what do they do?

also if i wanted to get a average grade of everyone i entered how do i do that?
Rajeev  Shukla
Honored Contributor

Re: Part II C program im stuck

The last printf function prints each students name and then their final score which is (q_avg*0.3)+(t_avg*0.7).
Just compile and try running it you'll know what is does.

Cheers
Mikel Howel_1
Advisor

Re: Part II C program im stuck

what does the \t mean?

%s %s\n\t\t
Rajeev  Shukla
Honored Contributor

Re: Part II C program im stuck

Hi Mikel,
Thats for a "TAB" singe \t is single TAB. This is used to have a formatted output on the screen.

Cheers
Rajeev
Mikel Howel_1
Advisor

Re: Part II C program im stuck

How do i get a average for all the students averages together to give the class a grade
Rajeev  Shukla
Honored Contributor

Re: Part II C program im stuck

Hi,
The structure element grade has all data for the class so make a loop and add all the elements and divide by number of students which you have defined as 3.
for(i=0;i<3;i++)
sum+=(student[i].grade);
av_class_grade=(sum)/3;

Cheers
Rajeev
Mikel Howel_1
Advisor

Re: Part II C program im stuck

could i just plug that code in and it would work? where would be the best place to put it?
Mikel Howel_1
Advisor

Re: Part II C program im stuck

is this how i would get the average for the class? i tried it and it didnt work, i declared the variable. help?

for(iCount=0;iCount<3;iCount++)
sum+=(student[iCount].grade);
av_class_grade=(sum)/3;

if((av_class_grade)>90)//Gives letter grade
printf("They have a A\n", student[iCount].fname, student[iCount].lname);

else if((av_class_grade=(sum))>80 && (av_class_grade=(sum))<= 90)
printf("They have a B\n");

else if((av_class_grade=(sum))>70 && (av_class_grade=(sum))<= 80)
printf("They have a C\n");

else if((av_class_grade=(sum))>60 && (av_class_grade=(sum))<= 70)
printf("They have a D\n");

else if((av_class_grade=(sum))<60)
printf("They failed the class\n");
}
Mikel Howel_1
Advisor

Re: Part II C program im stuck

also i think i did the grades wrong because if they get a 89 79 69 they fail,i tried to make it equal or greater than but it still didnt work.
Rajeev  Shukla
Honored Contributor

Re: Part II C program im stuck

As per the code i gave before you need to define
int sum, av_class_grade;
after #include
and then just at the end add the following code
sum=0;
for(i=0;i<3;i++)
sum+=(student[i].grade);
av_class_grade=(sum)/3;
printf("%d %d\n", av_class_grade, sum);
if(av_class_grade>80)
printf("The Class has Got Grade A\n");
if(av_class_grade>60 && av_class_grade<80)
printf("The Class has Got Grade B\n");
if(av_class_grade>40 && av_class_grade<60)
printf("The Class has Got Grade C\n");
if(av_class_grade<40)
printf("The Class has failed\n");


it should work.

Cheers
Rajeev
Mikel Howel_1
Advisor

Re: Part II C program im stuck

im implementing that into theprogram right now, so i just put it after the student grades correct? and if i wanted it to only print the class average at the end of the program it could do that right? also, Im also thinking that if i made the program so you could enter x amount of people. and have a question asking if you want to continue y/n. then if you said n, it would print of all the class results, but if you push yes you could keep entering students.

could i just make a variable for the students so ican add more than 3?
Rajeev  Shukla
Honored Contributor

Re: Part II C program im stuck

Yes you could ask more students than 3 by asking for argument in the program. Change the main program to look something like this.
main(argc, argv)
int argc;
char *argv[];
{
if (argc != 2){
printf("Usage: %s \n", argv[0]);
exit(1);
}
Then continue with the normal codes...

cheers
Rajeev
Mikel Howel_1
Advisor

Re: Part II C program im stuck

if i did that then it would let me enter x number of people right?

im having problems with it printing to the final output. it gives some funky numbers.
Rajeev  Shukla
Honored Contributor

Re: Part II C program im stuck

Yes it would let you x number of people but what you need to do is convert this character to intiger by atoi function. So the number of students will actually be .
no of student say x=atoi(argv[1]);

and then u'll have to change the student count from 3 to the x variable where ever you are doing for(i=0;i<3...make x

The program should be fine after that.

Cheers
Rajeev
Mikel Howel_1
Advisor

Re: Part II C program im stuck

 
Rajeev  Shukla
Honored Contributor

Re: Part II C program im stuck

Here is the full code.
#include
int sum, av_class_grade;
struct data {
char fname[26];
char lname[31];
int quiza, quizb, quizc, testa, testb, grade;
};
main(argc, argv)
int argc;
char *argv[];
{
struct data student[3];
int i, q_total, t_total, q_avg, t_avg;
int x;
if(argc != 2){
printf("Usage: %s \n", argv[0]);
exit(1);
}
x=atoi(argv[1]);
for (i=0; i printf("Enter the record No. %d\n", (i+1));
printf("Enter the Students first Name & Second Name: ");
scanf("%s %s", &student[i].fname, &student[i].lname);
printf("Enter the Marks %s %s Got in Quiz A: ", student[i].fname, student[i].lname);
scanf("%d", &student[i].quiza);
printf("Enter the Marks %s %s Got in Quiz B: ", student[i].fname, student[i].lname);
scanf("%d", &student[i].quizb);
printf("Enter the Marks %s %s Got in Quiz C: ", student[i].fname, student[i].lname);
scanf("%d", &student[i].quizc);
printf("Enter the Marks %s %s Got in Test 1: ", student[i].fname, student[i].lname);
scanf("%d", &student[i].testa);
printf("Enter the Marks %s %s Got in Test 2: ", student[i].fname, student[i].lname);
scanf("%d", &student[i].testb);
q_avg = ((student[i].quiza)+(student[i].quizb)+(student[i].quizc))/3;
t_avg = ((student[i].testa)+(student[i].testb))/2;
student[i].grade = (q_avg*0.3)+(t_avg*0.7);
if((student[i].grade)>80)
printf("You have Got Grade A\n");
if((student[i].grade)>60 && (student[i].grade)<80)
printf("You have Got Grade B\n");
if((student[i].grade)>40 && (student[i].grade)<60)
printf("You have Got Grade C\n");
if((student[i].grade)<40)
printf("You Have failed\n");
printf("Average=%d\n", (student[i].grade));

}
puts("");
printf("________________________________________________\n");
for (i=0; i printf("Entry No: %d, \n\t\t Name: %s %s\n\t\t Average Grade:%d\n", (i+1), student[i].fname, student[i].lname, student[i].grade);
printf("________________________________________________\n");
sum=0;
for(i=0;isum+=(student[i].grade);
av_class_grade=(sum)/3;
if(av_class_grade>80)
printf("The Class has Got Grade A\n");
if(av_class_grade>60 && av_class_grade<80)
printf("The Class has Got Grade B\n");
if(av_class_grade>40 && av_class_grade<60)
printf("The Class has Got Grade C\n");
if(av_class_grade<40)
printf("The Class has failed\n");
}

Just copy and run this with number of students as argument.

Cheers
Rajeev
Mikel Howel_1
Advisor

Re: Part II C program im stuck

im trying to run it, but it returns 125 errors?
Mikel Howel_1
Advisor

Re: Part II C program im stuck

Could it just be my compiler?
Rajeev  Shukla
Honored Contributor

Re: Part II C program im stuck

Hi
I can attach the program thats maximum i can do or else you might have to refer for some books.
Or contact some consultant to make some custom program for you.
Attached is the program which works fine for me.

Cheers
Rajeev
Mikel Howel_1
Advisor

Re: Part II C program im stuck

what compiler are you using? im using bloodshed, and so far im not too happy with it.
Rajeev  Shukla
Honored Contributor

Re: Part II C program im stuck

I am using a normal c compiler under Unix (HPUX 11.0)

using cc comand

Cheers
Rajeev