hello, can anyone help me figure out why my code won't execute to completion. i don't know why it crashes every time i get to the end of my function executions!!!
here is my code, and the compiled .exe file is attached!
------------------------------------------------------------------------------------------------------------------
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<string.h>
//#include "again.cpp"
using namespace std;
char name[30];
int main()
{
//variables
int num;
int* array;
int average;
int low_score = 0;
int index_low = 0;
//function prototypes
void describe();
void num_scores(int& num);
int allocate_memory(int num, int* array);
void fill_array(int num, int* array);
void selection_sort(int num, int* array);
void find_low(int num, int* array, int& low_score, int& index_low);
void calculation(int num, int* array, int& average);
void print2(int num, int* array, int average);
//function calls
describe();
cout<<"Enter the student's name: ";
cin.getline(name,30);
num_scores(num);
allocate_memory(num, array);
fill_array(num, array);
find_low(num, array, low_score, index_low);
selection_sort(num, array);
calculation(num, array, average);
print2(num, array, average);
delete[] array;
return 0;
}
void describe()
{
cout<<endl;
cout<<" This program gets grade input data from the user, then\n";
cout<<" dynamically allocates an array large enough to hold any\n";
cout<<" number of test scores the user wishes to enter and does\n";
cout<<" an analysis, providing information to the screen and \n";
cout<<" external file scores.txt. This information will be the\n";
cout<<" original test scores in the order in which they were entered,\n";
cout<<" the sorted test scores with an announcement of the lowest\n";
cout<<" score (which is to be dropped in calculation), and the\n";
cout<<" average test score, which will not include the lowest score.\n";
cout<<" An announcement will inform the user of the dropped score.\n\n ";
system("PAUSE");
system("CLS");
}
void num_scores(int& num)
{
cout<<"Enter the number of scores you will be processing: ";
cin>>num;
return;
//a function that gets the number of test scores to be entered from
//the user and then pass this number to another function that will dynamically
//allocate memory for the array of scores
}
int allocate_memory(int num, int* array)
{
array = new int[num];
cout<<"Memory has been allocated for "<<num<<" grades to be entered.\n\n";
cout<<endl<<endl;
//dynamically allocate memory for the array of scores passed from get_scores()
}
void fill_array(int num, int* array)
{
void print1(int num, int* array);
int grade;
cout<<endl;
for(int i=0; i<num; i++)
{
cout<<"Enter grade "<<i+1<<": ";
cin>>grade;
array[i] = grade;
}
print1(num, array);
return;
//fills this array with scores that are entered by the user
}
void find_low(int num, int* array, int& low_score, int& index_low)
{
int low = array[0];
for(int i=0; i<num; i++)
{
if(array[i] <= low)
{
low = array[i];
low_score = array[i];
index_low = i;
}
}
cout<<endl<<endl;
cout<<"The lowest score is test #"<<index_low+1<<", ";
cout<<"with a score of "<<low_score<<"%.\n\n";
system("PAUSE");
return;
//search through array and determine the subscript of the lowest score
}
void selection_sort(int num, int* array)
{
system("CLS");
cout<<endl;
cout<<"Grades are being sorted...\n\n";
system("PAUSE");
int p;
int minPosition;
int minValue;
for(p = 0; p<(num-1); p++)
{
minPosition = p;
minValue = array[p];
for(int i = p+1;i<num;i++)
{
if(array[i]<minValue)
{
minValue = array[i];
minPosition = i;
}
}
array[minPosition] = array[p];
array[p] = minValue;
}
return;
//sort the array from lowest to highest
}
void calculation(int num, int* array, int& average)
{
int counter = 1;
cout<<endl<<endl;
cout<<"Scores sorted. Lowest Dropped. Remaining results:";
cout<<endl;
for(int i=1; i<num; i++)
{
cout<<counter<<". ";
cout<<setw(3)<<array[i]<<"%"<<endl;
counter++;
}
double sum = 0;
double quotient = 0;
for(int j=1; j<num; j++)
{
sum += array[j];
}
quotient = (sum/(num-1)+.5);
average = static_cast<int>(quotient);
cout<<endl<<endl;
cout<<"After dropping the lowest score, the student average is ";
cout<<average<<"%.\n\n";
system("PAUSE");
return;
//returns the average score to the nearest integer value through a formal
//argument where the calculation is done dropping the lowest score.
}
void print1(int num, int* array)
{
ofstream outs;
outs.open("scores.txt", ios::app);
outs<<endl<<endl<<"---------------------------------------------\n";
outs<<"Student Name: "<<name<<endl<<endl;
for(int i=0; i<num; i++)
{
outs<<left<<setw(15)<<"Score on Exam #"<<setw(3)<<i+1<<": ";
outs<<setw(3)<<array[i]<<"%"<<endl;
}
outs.close();
system("CLS");
cout<<endl<<"---------------------------------------------\n";
cout<<"Student Name: "<<name<<endl<<endl;
for(int i=0; i<num; i++)
{
cout<<left<<setw(15)<<"Score on Exam #"<<setw(3)<<i+1<<": ";
cout<<setw(3)<<array[i]<<"%"<<endl;
}
return;
//The original test scores in the order in which they were entered
//in tabular form.
}
void print2(int num, int* array, int average)
{
ofstream outs;
outs.open("scores.txt", ios::app);
outs<<endl<<endl;
outs<<"After sorting scores and dropping the lowest, the results are:";
outs<<endl;
for(int i=1; i<num; i++)
{
outs<<left<<setw(15)<<"Score on Exam #"<<setw(3)<<i+1<<": ";
outs<<setw(3)<<array[i]<<"%"<<endl;
}
outs<<endl;
outs<<"The average score for "<<name<<", without counting the low ";
outs<<"score, is "<<average<<"%.\n\n";
outs.close();
return;
//The sorted test scores, with the lowest score dropped (listed in tabular
//form). The average test score, with the lowest score dropped, rounded off
//to the nearest integer value, along with an announcement of the lowest
//score that was dropped.
}