![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Posts: 5
Rep Power: 0
![]() |
Help
hi guys
i'm a beginner in c++ and i have H.W to submit but i need some help this is the question Write a C-Program that reads five grades (Homework, Quizzes, Major1, Major2, Final) for a number of students n (determined by the user). o For each student 1- Calculates the total grade x: (Sum of the five grades) 2- Assign a letter grade (A+,A, B+, B, C+, C, D+, D, F) according to the following rules : Total Grade Letter Grade 95<= x <100 A+ 90<= x < 94 A 85<= x < 89 B+ 80<= x < 84 B 75<= x < 79 C+ 70<= x <74 C 65<= x <69 D+ 60<= x < 64 D x<60 F Print the total grade x and the letter grades 3- Calculate and print the number of students with each letter grade, the format of the output must be like : Letter Grade Number of students ------------------ -------------------------- A+ -- A - B+ - . . 4- Calculate and print the max Total grade and the minimum Total grade i solved #1 but there is a problem i can solve 2,4 but 3 i need someone to explain it #include<stdio.h>
#include<math.h>
main()
{
int n,i ;
double x,hw,q,m1,m2,f,sum ;
sum=0 ;
printf("enter number of students:");
scanf("%d",&n);
printf("enter the grade of hw,q,m1,m2 and f:");
scanf("%lf %lf %lf %lf %lf",&hw,&q,&m1,&m2,&f);
for(i=1,i<=n,i++)
{
scanf("%lf",&x);
sum+=x ;
}
x= hw+q+m1+m2+f ;
scanf("%lf",&x);
printf("x=%lf",x);
}Last edited by Pizentios; Apr 5th, 2006 at 9:36 AM. |
|
|
|
|
|
#2 | |
|
Professional Programmer
|
couldn't you just do something like
for(int i = 0; i < maxstudents; i++) std::cout << x << n << std::endl;
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 242
Rep Power: 3
![]() |
Make your Code more readable by structuring it properly, and put it in code tags, and your much more likely to get some help, also this maybe best suited for the C section rather than C++.
I can't help you with your code as I'm not too hot on printf or scanf as its more for C... that and also the format of your code is making my eyes bleed. |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Hi. Your post should be in the C forum
Making your code look like this makes it much easier to read: #include <stdio.h>
#include <math.h>
main()
{
int n, i;
double x, hw, q, m1, m2, f, sum;
sum = 0;
printf("Enter number of students: ");
scanf("%d", &n);
printf("Enter the grade of Homework, Quizzes, Major1, Major2 and Final: ");
scanf("%lf %lf %lf %lf %lf", &hw, &q, &m1, &m2, &f);
for(i = 1; i <= n; i++)
{
scanf("%lf",&x);
sum+=x ;
}
x = hw + q + m1 + m2 + f;
scanf("%lf",&x);
printf("x = %lf\n",x);
}Please read the "How to post a question" thread at the top of the C or C++ forum. And restructure your question so it looks readable.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
If you want to impress your teacher (or maybe not...), you can do this for your first part:
#include <stdio.h>
#include <math.h>
int main()
{
int number = 0, current;
double sum;
char *gradenames[] =
{
"Homework", "Quizzes", "Major1", "Major2", "Final"
};
double grades[] =
{
0, 0, 0, 0, 0
};
printf("Enter number of students: ");
if(scanf("%d", &number) != 1)
{
printf("IDIOT!\n");
return 1;
}
for(current = 0; current < number; current++)
{
int t;
sum = 0;
printf("\nStudent #%d\n", current+1);
for(t = 0; t < 5; t++)
{
printf("Enter the grade of %s: ", gradenames[t]);
if(scanf("%lf", &grades[t]) != 1)
{
printf("IDIOT!\n");
return 1;
}
sum += grades[t];
}
printf("Sum of student #%d is %.2lf\n\n", current+1, sum);
}
return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() |
fixed code tags
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Mar 2006
Posts: 60
Rep Power: 3
![]() |
Wouldn't flushing (fflush(stdout)) after the printf's that doesn't use '\n' be a good idea?
|
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You might consider flushing as a preemptive action. You will rarely find it necessary to flush to the display on a typical desktop-type system with a common multi-processing OS. Don't forget, though, that you may redirect stdout to something other than the display. If one mixes printf with cout in C++, one should investigate making sure they're synchronized.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Mar 2006
Posts: 60
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|