Input a class of student scores and student numbers for CPSC 1103. Find and prin
ID: 3678940 • Letter: I
Question
Input a class of student scores and student numbers for CPSC 1103. Find and print the student grades. Calculate the average score and standard deviation for the class. Print the student number and student scores and grades in ascending order of the student scores. Print the grade assigned to each student and the average for the class and the standard deviation. Assume the class has no more than 35 students. Input the data from file and output to file
input data
student number scores grades
123 50.5 F
234 67.8 C
445 90.2
235 59.5
135 88.5
256 98.0
257 75.6
258 79.5
259 45.6
239 99.0
260 94.5
Declare an array to hold the student numbers (int student[50]).
Declare another array to hold the corresponding scores
(float scores[50]) and another array to hold student grades (char grades[50]).
#include <iostream>
#include <conio.h>
#include <fstream>
#include <cmath>
using namespace std;
#define in_file “gradesData.txt”
#define out_file “gradesResult.txt”
// declare your file streams globally ifstream ins; ofstream outs;
void inputGrades(int[], float[], int&); void outputGrades(const int[], const float[], int);
void sort(int[], float[], int); // sorting the lists before letter grade void assignGrades(const float[], char[], int); void outputAll(const int[], const float[], const char[], int);
float findaverage(const float[], int);
double findsdev(const float[], int, float);
void main()
{ int student[50]; // holds student numbers
float scores[50]; // holds student scores
char grades[50]; // holds the letter grades
Page 46
int counter; // counting number of students
ins.open(in_file);
outs.open(out_file); inputGrades(student, scores, counter);
// echo printing the two lists outputGrades(student, scores, counter);
getch();
ins.close();
outs.close();
}
// You have to declare files as global variables. void inputGrades(int student[], float scores[], int& counter)
{ char next_char; //local variable
counter = 0;
while (!ins.eof()) { if (counter < 50) { ins >> student[counter] >> scores[counter];
counter++;
ins.get(next_char); // to skip the end of line character
}
else break;
}
}
You also need a counter to count the number of students read in.
Set counter to 0 to start and then increment it each time you read in a student.
After reading everything in, you need to output them to validate your input.
Page 47
void outputGrades(const int student[], const float scores[], int counter)
{ for (int i = 0; i < counter; i++)
{
cout << student[i] << “ “ << scores[i] << endl;
}
}
To assign grades, you will need another array.
char grades[50];
// function to assign letter grades
void assignGrades(const float scores[], char grades[], int counter)
{ 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] >= 65.0)
grades[i] = ‘C’;
else if (scores[i] >=55.0)
grades[i] = ‘D’;
else grades[i] = ‘F’;
}
}
Page 48
void outputAll(const int student[], const float scores[], const
char grades[], int counter)
{
for (int i = 0; i < counter; i++)
{
cout << student[i] << “ “ << scores[i] <<
“ “ << grades[i] << endl;
}
}
Page 49
When you do the sorting, you compare the scores. When you switch you switch both the student number and the corresponding score. Need to write the swapFloat and swapInteger functions
void sort(int studentNo[], float scores[], int size)
{ for (int i = size -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(studentNo[j], studentNo[j + 1]);
}
}
void swapFloat(float& a, float& b)
{ float temp;
temp = a;
a = b;
b = temp;
}
void swapInteger(int& a, int& b)
{ int temp;
temp = a;
a = b;
b = temp;
}
Page 50
An improved 'bubble sort'
void improve_sort(int listC[], float listD[], int size)
{
int sorted = 0;
int LAST = size ; while ((sorted == 0) && (LAST >= 2)) // number of passes
// go through the list once – stop when there is no switching
{ sorted = 1; // assume there is no more swapping
for (int i = 0; i < LAST - 1; i++) //no. of comparisons
{
if (listD[i] > listD[i+1]) // if swapping is required { sorted = 0; // the list will be checked again
swapInteger(listC[i], listC[i+1]);
swapFloat(listD[i], listD[i]);
}
}
LAST -= 1;
};
}
13
53
65
74
87
95
103
The list is already sorted when there is no switching involved.
Page 51
void swapFloat(float& A, float& B)
{
float temp;
temp = A;
A = B;
B = temp;
}
void swapInteger(int& A, int& B)
{
int temp;
temp = A;
A = B;
B = temp;
}
Page 52
Function to calculate the average:
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;
}
Page 53
Function to find the standard deviation:
double findsdev(float scores[], int size, float average)
{ double sumofdifference = 0.0;
for (int i = 0; i < size; i++)
{
sumofdifference += (scores[i] –average) *
(scores[i] –average)
}
return sqrt (sumofdifference / size);
}
Standard Deviation calculation;
Calculate the average of all scores.
Find the difference of each score and the average (i.e. scores[i] – average) and square the difference.
Sum the squares of all differences and divide it by n (the number of students).
Take a square root of this sum and that is the standard deviation.
Page 54
output data
student number scores grades
543 20.5 F
342 35.4 F
505 50.4 P
321 60.3 C
. . .
. . .
Average for the class = 67.3%
standard deviation for the class = 10.3
Page 55
An improved 'bubble sort'
void improve_sort(int listC[], int size)
{
int sorted = 0;
int LAST = size ;
while ((sorted == 0) && (LAST >= 2))
{ sorted = 1; // assume there is no more swapping
for (int i = 0; i < LAST - 1; i++)
{
if (listC[i] > listC[i+1]) // if swapping is required { sorted = 0; // the list will be checked again
exchange(listC[i], listC[i+1]);
};
}
LAST -= 1;
};
}
If you go through the list and there is no swapping of values, this means the list is sorted and you can stop further passes
Explanation / Answer
Below is the program to calculate avarage and standard deviation of student grades:
#include <iostream>
#include <fstream>
#include <conio.h>
#include <cmath>
using namespace std;
#define in_file "gradesdata.txt"
#define out_file "gradesresult.txt"
int main ()
{
ifstream ins; // associates ins as an input stream
ofstream outs; // associates outs as an output stream
int student[50];
int count = 0;
float scores[50];
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[50];
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);
}
Below is a similar type of program to read an input text file and outputs Students name, Course name, Numeric grade, Letter grade: