Jump to content

help me to solve this problem..please..

- - - - -

  • Please log in to reply
3 replies to this topic

#1
purple_coat

purple_coat

    Newbie

  • Members
  • Pip
  • 3 posts
I have been able to get the average numerical scores, but I want to be able to get the average quiz1 score, quiz2 score, midterm, final, and entire course course once I type n (for no). How could I possibly do it?

#include <stdio.h>

#include <ctype.h>

#define NAME_LEN 50

#define STUD_LEN 100


int read_line(char str[], int n);

int find_students(int students);


struct test_result {

char name[NAME_LEN+1];

int number;

int grade1;

int grade2;

int midterm;

int final;

float numeric;

}studen[STUD_LEN];


void insert(void);

void print(void);


int num_students = 0;


int main(void)

{

struct test_result test;

printf("Enter the student's name: ");

read_line(test.name, NAME_LEN);

printf("Enter the student's grade for quiz #1: ");

scanf("%d", &test.grade1);

printf("Enter the student's grade for quiz #2: ");

scanf("%d", &test.grade2); 

printf("Enter the student's grade for midterm: ");

scanf("%d", &test.midterm);

printf("Enter the student's grade for final: ");

scanf("%d", &test.final);

test.numeric = (((test.grade1 + test.grade2) * 1.25) + (test.midterm * 0.25) + (test.final * 0.50)); 

printf("%s's numeric score for the entire course is %.1f\n", test.name, test.numeric);


char code;

for (;;) {

printf("\n");

printf("Would you like to enter another student record? y(yes) or n(no)?");

scanf(" %c", &code);

while (getchar() != '\n') /* skips to end of line */

;


switch (code) {

case 'y': insert();

break;

case 'n': print();

return 0;

default: printf("Invalid entry. Try again.\n");

return 0;

} 

}

}


int find_students(int students)

{

int i;


for (i = 0; i < num_students; i++)

if (studen[i].number == students)

return i;

return -1;

}



void insert(void)

{ 

int part_number;

if (num_students == STUD_LEN) {

printf("Sorry, cannot enter any more students.\n");

return;

}


studen[num_students].number = part_number;

printf("Enter the student name: ");

read_line(studen[num_students].name, NAME_LEN);

printf("Enter the student's grade for quiz #1: ");

scanf("%d", &studen[num_students].grade1);

printf("Enter the student's grade for quiz #2: ");

scanf("%d", &studen[num_students].grade2);

printf("Enter the student's grade for midterm: ");

scanf("%d", &studen[num_students].midterm);

printf("Enter the student's grade for final: ");

scanf("%d", &studen[num_students].final);


studen[num_students].numeric = 

(((studen[num_students].grade1 + studen[num_students].grade2) * 1.25) + (studen[num_students].midterm * 0.25) + (studen[num_students].final * 0.50));

printf("%s's numeric score for the entire course is %.1f\n", studen[num_students].name, studen[num_students].numeric);

num_students++;

}



void print(void)

{

printf("The average score on quiz1 is\n");

printf("The average score on quiz2 is\n"); 

printf("The average score on midterm is\n"); 

printf("The average score on the final is\n");

printf("The average score for the entire course is\n");


}



int read_line(char str[], int n)

{

int ch, i = 0;


while(isspace(ch = getchar()))

;

str[i++] = ch;

while ((ch = getchar()) != '\n') {

if (i < n)

str[i++] = ch;

}

str[i] = '\0';

return i;

}

Edited by ZekeDragon, 06 February 2011 - 10:51 AM.
Please use [CODE] tags (the # button) when posting code.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You could simply sum up the values as they are entered, along with a count.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
purple_coat

purple_coat

    Newbie

  • Members
  • Pip
  • 3 posts
can u show it to me

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You'll need a sum variable for each of the test items. After you read in each new test score, increment the sum variable for that test score.
printf("Enter the student's grade for quiz #1: ");
scanf("%d", &test.grade1);
sumgrade1 += test.grade1;

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users