Reputation: 45
I am a new programmer and had some problem using logical operator.
I make up a program that get the following data from the user including no of subject total marks of the exam the marks of the individual subjects than add all those marks and get the percentage. Then I use logical operator to print some messages based on the percentage obtained. Program is running but its not showing the correct messages based on the percentages.
Why is it not running correctly?
#include<stdio.h>
#include<stdlib.h>
int main(){
int total_marks = 0;
int no_of_subjects = 0;
int subjects[no_of_subjects];
int sum=0;
float per=0;
float gradeAplus=0;
float gradeA=0;
float gradeB=0;
float gradeC=0;
float gradeD=0;
float gradeE=0;
printf("What is the total number of subjects : ");
scanf("%d",&no_of_subjects);
printf("What is total number of marks in your exam : ");
scanf("%d",&total_marks);
gradeAplus = .9*total_marks;
gradeA = .8*total_marks;
gradeB = .7*total_marks;
gradeC = .6*total_marks;
gradeD = .5*total_marks;
gradeE = .4*total_marks;
for(int c=0;c<no_of_subjects;c++){
printf("Enter the marks of your %d subject : ",c+1);
scanf("%d",&subjects[c]);
sum+=subjects[c];
}
per = ((float)sum/(float)total_marks)*100.0;
if((per == gradeAplus)||(per > gradeAplus)){
printf("\nCongratulation you have achieve A plus grade.You are excellent.");
}
else if((per == gradeA)||((per > gradeA) && (per < gradeAplus))){
printf("\nCongratulation you have achieve A grade.You are very good.");
}
else if((per==gradeB)||((per > gradeB) && (per < gradeA))){
printf("\nWell you have achieved grade B.You are good.");
}
else if((per==gradeC)||((per > gradeC) && (per < gradeB))){
printf("\nYou have achieve grade C.You have to do some hard work.");
}
else if((per==gradeD)||((per > gradeD) && (per < gradeC))){
printf("\nYou have obtained grade D.You must work hard in order to pass the exam.");
}
else if((per==gradeE)||((per > gradeE) && (per < gradeE))){
printf("\nYou have obtained grade E.You put a really bad performance.If you do not work hard you will be fail.");
}
else if(per<gradeE){
printf("\nSorry you are fail.Try next time.\n");
}
else{
printf("You have entered wrong data.");
}
return EXIT_SUCCESS;
}
Upvotes: 2
Views: 267
Reputation: 972
Hello Muslim,
Your mistake in code, when you calculate per
that time calculation get wrong.
And other mistake in this below condition,
else if((per==gradeE)||((per > gradeE) && (per < gradeE))){
printf("\nYou have obtained grade E.You put a really bad performance.If you do not work hard you will be fail.");
}
When user enter marks above 100(Note: If exam paper 100 marks per subject.) per subject wise so also give the grade but it's not valid so improve code.
When user enter mark per subject wise above 100(Note: If exam paper 100 marks per subject.) so give the message(i.e Subject marks not above 100) and ask to again marks.
#include<stdio.h>
#include<stdlib.h>
void main(){
int total_marks = 0;
int no_of_subjects = 0;
int subjects[no_of_subjects];
int sum=0;
float per=0.0;
float gradeAplus=0;
float gradeA=0;
float gradeB=0;
float gradeC=0;
float gradeD=0;
float gradeE=0;
printf("What is the total number of subjects : ");
scanf("%d",&no_of_subjects);
printf("What is total number of marks in your exam : ");
scanf("%d",&total_marks);
gradeAplus = .9*total_marks;
gradeA = .8*total_marks;
gradeB = .7*total_marks;
gradeC = .6*total_marks;
gradeD = .5*total_marks;
gradeE = .4*total_marks;
for(int c=0;c<no_of_subjects;c++)
{
printf("Enter the marks of your %d subject : ",c+1);
scanf("%d",&subjects[c]);
sum+=subjects[c];
}
per = ((float)(sum*100.00)/(float)(total_marks*no_of_subjects));
if(per >= gradeAplus)
{
printf("\nCongratulation you have achieve A plus grade.You are excellent.");
}
else if((per >= gradeA) && (per < gradeAplus)){
printf("\nCongratulation you have achieve A grade.You are very good.");
}
else if((per >= gradeB) && (per < gradeA)){
printf("\nWell you have achieved grade B.You are good.");
}
else if((per >= gradeC) && (per < gradeB)){
printf("\nYou have achieve grade C.You have to do some hard work.");
}
else if((per >= gradeD) && (per < gradeC)){
printf("\nYou have obtained grade D.You must work hard in order to pass the exam.");
}
else if((per >= gradeE) && (per < gradeD)){
printf("\nYou have obtained grade E.You put a really bad performance.If you do not work hard you will be fail.");
}
else if(per<gradeE){
printf("\nSorry you are fail.Try next time.\n");
}
else{
printf("You have entered wrong data.");
}
}
Upvotes: 1
Reputation: 126518
Among the other problems you have, this code:
int no_of_subjects = 0;
int subjects[no_of_subjects];
declares the subjects
array to have a size of 0. When you later change no_of_subjects
, that doesn't change the size of the array, so you then use out-of-range indexes.
You need to move the declaration of subjects
after you get the value of no_of_subjects
Upvotes: 0
Reputation: 3386
You're calculating grade
as an absolute value of totalmarks but later compare them with the percentage value per
.
gradeAplus = 90;
gradeA = 80;
gradeB = 70;
gradeC = 60;
gradeD = 50;
gradeE = 40;
...
per = sum * 100.0 / total_marks;
if (per >= gradeAplus) {
printf("\nCongratulation you have achieve A plus grade.You are excellent.");
}
else if (per >= gradeA) {
printf("\nCongratulation you have achieve A grade.You are very good.");
}
else if (per >= gradeB) {
...
I've optimzed the logic a bit and dropped a few parentheses. Note: A clean syntax is all the way to go to become a good programmer.
Upvotes: 2