Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...What a great service! This is the best site I've ever seen!!! It totally restores my faith in humanity when people take time out to help other people..."

Geography

Where in the world do Tek-Tips members come from?
CindyCSCK (Programmer)
16 Jan 02 16:01
I need to make a program that does the following in C++ . It allows input for three grades for 10 students,  It then displays the students three grades, the average, and their variance from the class average.  It needs to look like this:

Student #1  95   100   95   97.7   17.7 above the average
Student #2  67    70   75   70.7    9.3 below the average

Etc... for the ten students

I have tried several ways with absolutly no luck.

Thanks in advance
varocho (Programmer)
18 Jan 02 7:24
I think you might have better luck posting to one of the C++ forums.
dhourd (Programmer)
25 Feb 02 18:37
Cindy, this is a small program that should do the trick and allow you to enter up to 10 students.  Keep in mind that a function called strtok() is used to parse the data.  Also note that the program is easily configured to allow more than three marks per student (simply modify MaxMarks) and can be expanded to more than 10 students (by changing MaxStudents).

#include <iostream.h>
#include <string.h>
#include <tchar.h>
#include <stdlib.h>

void main(void) {
  const int MaxStudents = 10;
  const int MaxMarks = 3;
  struct {
    double Mark[MaxMarks];
    double Average;
  } StudentList[MaxStudents];

  int i, j;
  // Initialise the student list
  for (i = 0; i < MaxStudents; i++) {
    for (j = 0; j < MaxMarks; j++)
      StudentList[i].Mark[j] = 0.0;
    StudentList[i].Average = 0.0;
  }
  // Assume a <space> will separate the marks
  char *separtors = " ";

  // Now allow the user to enter the student marks
  char MarksInput[100];
  int StudentCount = 0;
  int MarkCount = 0;
  do {
    cout<<"\r\nEnter Student #"<<StudentCount+1<<" data - ";
    cin.getline(MarksInput, sizeof(MarksInput) / sizeof(char));
    char *token = strtok(MarksInput, separtors);
    MarkCount = 0;
    while ((token != NULL) && (MarkCount < MaxMarks)) {
      StudentList[StudentCount].Mark[MarkCount] = atof(token);
      MarkCount++;
      token = strtok(NULL, separtors);
    }
    if (MarkCount > 0) // Only increment the student count if it is valid
      StudentCount++;
  } while (MarkCount && (StudentCount < MaxStudents));

  // The user has completed entering the student marks
  if (StudentCount > 0) {
    // Calculate the average for each student and the total average
    double TotalAverage = 0.0;
    for (i = 0; i < StudentCount; i++) {
      for (j = 0; j < MaxMarks; j++)
        StudentList[i].Average += StudentList[i].Mark[j];
      TotalAverage += StudentList[i].Average;
      StudentList[i].Average /= MaxMarks;
    }
    TotalAverage /= (StudentCount * MaxMarks);

    // Now display the results
    cout<<"Student Average is:"<<TotalAverage<<" for "<<StudentCount<<" Students\r\n";
    for (i = 0; i < StudentCount; i++) {
      cout<<"Student #"<<i+1;
      for (j = 0; j < MaxMarks; j++)
        cout<<"\t"<<StudentList[i].Mark[j];
      cout<<"\t"<<StudentList[i].Average<<"\t";
      if (StudentList[i].Average < TotalAverage)
        cout<<TotalAverage-StudentList[i].Average<<" below";
      else if (StudentList[i].Average > TotalAverage)
        cout<<StudentList[i].Average-TotalAverage<<" above";
      cout<<" the average\r\n";
    }
  }
}

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close