5 Replies - 636 Views - Last Post: 03 October 2009 - 01:44 PM Rate Topic: -----

#1 loonyt7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 02-September 09

Need help with a C program

Posted 03 October 2009 - 09:40 AM

I have this program that I am working on using C. The code compiles and runs I am just a little lost with the last portion of it. The purpose of the program is for the user to enter grades for multiple students. When the user has finished with one student they enter -1 at that point the the average is displayed. Then it is suppose to reset and start with student 2 and so on.

***This is the part I am having problems with.
When the user is done they enter a -1 in the grade 1 to indicate no more
students. Then it should produce the average for all students.

Any assistance would be a big help.
#include <stdio.h>


int main()
{
int studentquantity = 1;
int gradenumber = 1;
int grade;
int studenttotal = 0;
int totalnumgrades;
int overallgrade;
float average;
float classaverage;


printf("To change change students enter -1\n");
printf("When completed with list enter -1 as first score\n\n");

do { 
do {
do {

printf("Student #%d, grade #%d = ", studentquantity, gradenumber);
scanf("%d", &grade );
if (grade > 100)
printf("Grade is invalid, try again\n");
else
break;

} while ( grade != -1 );

if (grade < 0 ) break; 

gradenumber = gradenumber + 1;
studenttotal = studenttotal + grade;


} while ( grade != -1 );
gradenumber--;
average = ( float ) studenttotal / gradenumber;
printf("Student #%d had %d grade(s), average is %.2lf\n", studentquantity, gradenumber, average);

totalnumgrades = gradenumber;
overallgrade = studenttotal;
gradenumber = 1;
studentquantity++;



} while ( ( gradenumber == 1) && (grade == -1) );
if ( ( gradenumber == 1) && (grade == -1) ) 
classaverage = (float) overallgrade / totalnumgrades;
printf("There were %d student(s) with an average of %.2fl\n", studentquantity, classaverage);


system("PAUSE"); 
return 0;
}


This post has been edited by loonyt7: 03 October 2009 - 10:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with a C program

#2 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 334
  • View blog
  • Posts: 1,178
  • Joined: 02-April 09

Re: Need help with a C program

Posted 03 October 2009 - 10:20 AM

Could you edit this and put the code inside CODE tags? It's pretty hard to read.
Was This Post Helpful? 0
  • +
  • -

#3 loonyt7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 02-September 09

Re: Need help with a C program

Posted 03 October 2009 - 10:31 AM

sorry about that it has been edited.
Was This Post Helpful? 0
  • +
  • -

#4 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: Need help with a C program

Posted 03 October 2009 - 12:43 PM

i think you are doing whatever you have to do in a very complicated manner, i have simplified the code a bit for you:
#include <stdio.h>
int main()
{
int studentquantity = 1;
int gradenumber = 1;
int grade;
int studenttotal = 0;
int totalnumgrades=0;
int overallgrade=0;
float average=0;
float classaverage=0;
printf("To change change students enter -1\n");
printf("When completed with list enter -1 as first score\n\n");
do {
do {
printf("Student #%d, grade #%d = ", studentquantity, gradenumber);
scanf("%d", &grade );
if (grade > 100 || grade<-2 || grade==0)
printf("Grade is invalid, try again\n");
else
{
gradenumber = gradenumber + 1;
studenttotal = studenttotal + grade;
}} while ( grade != -1 );
average = studenttotal / gradenumber;
printf("Student #%d had %d grade(s), average is %.2lf\n", studentquantity, gradenumber-1, average);
totalnumgrades = gradenumber;
overallgrade = overallgrade+studenttotal;
gradenumber = 1;
studentquantity++;
studenttotal=0;
} while (grade != -1);
classaverage = overallgrade / totalnumgrades;
printf("There were %d student(s) with an average of %.2fl\n", studentquantity, classaverage);
system("PAUSE"); 
return 0;
}


also notice some basic changes made, hopefully i have understood what you want to do correctly.....

one piece of advice, always try to minimise the number of nested loops in your program.. use multiple nesting only when it is absolutely essential
Was This Post Helpful? 0
  • +
  • -

#5 loonyt7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 02-September 09

Re: Need help with a C program

Posted 03 October 2009 - 12:58 PM

Thanks for the try but that doesn't work. I understand that I should watch how much I used nested loops but I can not use an array and the code I used was the best that I could come up with.
Was This Post Helpful? 0
  • +
  • -

#6 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: Need help with a C program

Posted 03 October 2009 - 01:44 PM

here you go:
#include <stdio.h>
#include  <iostream.h>
#include <conio.h>
int main()
{
clrscr();
char ch='y';
int studentnumber = 1;
int gradenumber = 1;
int grade;
int studenttotal = 0;
int totalnumgrades=0;
int overallgrade=0;
float average=0;
float classaverage=0;
printf("To change students enter -1\n");
while (ch=='y')
{
do
{
printf("Student #%d, grade #%d = ", studentnumber, gradenumber);
scanf("%d", &grade );
if (grade > 100 || grade<-1 || grade==0)
printf("Grade invalid\n");
else
{
if (grade!=-1)
{
studenttotal = studenttotal + grade;
gradenumber++;  }
}} while ( grade != -1 );
average = (float)studenttotal / (gradenumber-1);
printf("Student #%d had %d grade(s), average is %.2lf\n", studentnumber, gradenumber-1, average);
totalnumgrades = totalnumgrades+gradenumber;
overallgrade = overallgrade+studenttotal;
gradenumber = 1;
studentnumber++;
studenttotal=0;
printf("enter 'y' for another student & 'n' if all the students are done ");
cin>>ch;//sorry i am more comfortable in c++ and poor with format specifiers of c, you can use the equivalent scanf for character value here
}
classaverage = (float)overallgrade / (totalnumgrades-1);
printf("There were %d student(s) with an average of %.2fl\n", studentnumber-1, classaverage);
getch();
return 0;
}


one major flaw was that same condition was being used for both the inside & outside loops, even i overlooked it at the first glance... so i have modified it to get 'y' or 'n' from the user at the time of entering another student

the above code should do your work just fine :)

This post has been edited by aks29921: 03 October 2009 - 01:49 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1