1 Replies - 1609 Views - Last Post: 10 March 2011 - 06:17 AM Rate Topic: -----

#1 persons  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 23-February 11

average grade and standard deviation for the class C++ help check code

Posted 10 March 2011 - 12:11 AM

Input a class of student grades and student numbers. Calculate the average grade and standard deviation for the class. Print the student number and student grades in ascending order of the student grade.
Print the grade assigned to each student and the average for the class and the standard deviation. Assume the class has no more than 30 students.

input data
student number scores
321 60.5
542 50.5
524 75.0
235 83.0
565 95.5
123 65.0
257 75.6
258 79.5
259 45.6
260 99.0
261 94.5

Declare an array to hold the student numbers (int student[30]).
Declare another array to hold the corresponding scores
(float scores[30]).

Here is a code I tried:

#include <iostream>
#include <fstream>
#include <conio.h>
#include <cmath>
using namespace std;
#define in_file "data.txt"
#define out_file "result.txt"
int main ()
{
ifstream ins; // associates ins as an input stream
ofstream outs; // associates outs as an output stream
int student[30];
int count = 0;
float scores[30];
int counter = 0;

ins.open(in_file);
outs.open(out_file);
while (!ins.eof())
{
ins >> student[counter] >> scores[counter];
counter++;
ins.get(next_char); // to skip the end of line character
}
for (int i = 0; i < counter; i++)
{
cout << student[i] << " " << scores[i] << endl;
}
for (int i = counter -2; i >= 0 ; i = i  1)
for (int j = 0; j <= i; j++)
if (scores[j] > scores[j + 1])
{
swapFloat (scores[j], scores[j + 1]);
swapInteger(student[j], student[j + 1]);
}

char grades[30];
for (int i = 0; i < counter; i++)
if (scores[i] >= 90.0)
grades[i] = 'A';
else if (scores[i] >= 80.0)
grades[i] = 'B';
else if (scores[i] >= 70.0)
grades[i] = 'C';
else if (scores[i] >= 60.0)
grades[i] = 'D';
else grades[i] = 'F';
ins.close (); // closing input file
outs.close (); // closing output file
}
float findaverage(float scores[], int size)
{
float sum = 0.0;
// summing all the scores
for (int i = 0; i < size; i++)
sum = sum + scores[i];
// return the average which is sum divided by no of students
return sum/size;
}

float findsdev(float scores[], int size, float average)
{
float sumofdifference = 0.0;
for (int i = 0; i < size; i++)
{
sumofdifference += ((scores[i])  average) * ((scores[i])  average);
}
return sqrt (sumofdifference / size);
}



thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: average grade and standard deviation for the class C++ help check code

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,606
  • Joined: 23-August 08

Re: average grade and standard deviation for the class C++ help check code

Posted 10 March 2011 - 06:17 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1