i have an input file that im writing an an array then printing out that array to a file but for some reason its printing out more than once. also the format for my functions such as findMin is coming out incorrectly. please help!
Code:
#include
<iostream>
#include
<fstream>
#include
<iomanip>
using
namespace std;
void
readData(ifstream &inputFile, int StudID[],float Scores[],int& size);
void
printData(ofstream &outputFile, int StudID[],float Scores[], int& size);
void
FindMin ( int StudID[], int& size);
void
FindMax (int StudID[], int& size);
void
ComputeAvg(float Scores[], int& size);
void
BubbleSort (float Scores [ ], int& size);
//***************************************main fucntion*****************************************************
int
main()
{
//Declare Program variables and Arrays
int size = 0;
int StudID[50];
float Scores[50];
// char Name[15];
ifstream inputFile;
ofstream outputFile;
// open the input and output files
outputFile.open (
"practice.txt");
inputFile.open (
"data1");
// set the formatting codes for output
outputFile.setf(ios::fixed);
outputFile.precision(2);
//outputFile.setf(ios::fixed);
//outputFile.precision(2);
// prints the column titles to the file
outputFile <<
" The original list of test scores is: \n";
outputFile <<
"Student ID # Test Scores \n";
// runs the fucntions, prints ids and test scores
readData( inputFile, StudID, Scores, size);
printData (outputFile, StudID, Scores, size);
FindMin ( StudID, size);
ComputeAvg( Scores, size);
FindMax ( StudID, size);
BubbleSort ( Scores, size);
//ofstream outputFile;
//outputFile.open("practice.txt");
// Prints the required texts to the output file
outputFile <<
" the lowest test score was " ; //FindMin << " and was acheived by student #" << StudID<< endl;
outputFile <<
" the highest test score was " ;// FindMax << " and was acheived by student #" << StudID<< endl;
outputFile <<
" The average score of the group was ";// << ComputeAvg<< endl;
outputFile << BubbleSort<< endl;
inputFile.close();
outputFile.close();
return 0;
}
//***************************************************************************************************************
void
readData(ifstream &inputFile, int StudID[],float Scores[],int& size)
{
// GIVEN: The Data file for the Students Ids and Test Scores
// TASK: Read the Scores and Ids from the file into an empty array
// RETURNS: The Student Ids from the File and Their test Scores
//reads in the students IDs and test scores
for (int i = 0;i<50; i++)
{
inputFile>> StudID[i] ;
// sentinel
if(StudID[i]<0)
return;
// Reads in the test scores
inputFile>> Scores[i];
size++;
}
}
//*************************************************************************************************************
void
printData(ofstream &outputFile, int StudID[],float Scores[], int& size)
{
// GIVEN: The output file, the list of students with their
// Test Scores, and The students Ids
// TASK: Print out the list of students Ids and Test Scores
// RETURNS: Nothing
// prints the arrays to the file
for (int i = 0;i<50; i++)
for (int i = 0;i<size; i++)
{
outputFile <<
" " << StudID[i] << " " << Scores[i];
outputFile << endl;
}
return ;
}
//*****************************************************************************************************************
void
FindMin ( int StudID[], int& size)
{
// GIVEN: The students Ids and test scores
// TASK: Compute the lowest quiz score
// RETURNS: The lowest quiz score
int i, MinLoc=0 ;
int MinValu = 110;
//finds the lowest test score
for ( i = 0; i < size; i++)
if (StudID[i]< MinValu )
{
MinValu = StudID[i];
MinLoc = i;
}
return ;
}
//****************************************************************************************************
void
FindMax (int StudID[], int& size)
{
// GIVEN: The arrays of students ids and test scores
// TASK: Compute the highest test score
// RETURNS: The highest test score and the Student Id
int i, MaxLoc=0 ;
int MaxValu = -10;
// finds the student with highest test score
for ( i = 0; i < size; i++)
if (MaxValu > StudID[i])
{
MaxValu = StudID[i];
MaxLoc = i;
}
return ;
}
//******************************************************************************************************
void
ComputeAvg(float Scores[], int& size)
{
// GIVEN: The arrays of the Student Ids and test Scores
// TASK: Compute the classes average test score
// RETURNS: The classes average test score
int i, total = 0;
int average;
//adds up all the scores and finds the average
for ( i = 0; i < size; i++)
{
total += Scores[i];
average = total/size;
}
return ;
}
//*********************************************************************************************************
void
BubbleSort (float Scores [], int& size) // Sorts list LOW to HIGH
{
int M, N, temp;
for ( M = 0 ; M < size-1; M++) // M controls how many times we pass thru the array
{
for ( N = size-1; N > M ; N--) // N controls at which end of the array we begin and which
if (Scores[N] < Scores[N-1] ) // elements are compared and swapped if needed.
{
temp = Scores[N];
Scores[N] = Scores[N-1];
Scores[N-1] = temp;
}
}
return;
}