I am trying to test my code for an assignment but the console in visual studio crashes before I can see any output. The program is supposed to read in two integers, one for a quiz score (corresponds to the index in the array) and one for the frequency (number of students who received that quiz score). I am supposed to read in quiz scores and frequencies until the sentinel data value is reached (-1 0). When I run the program, I enter in some samples data such as:
35 5
50 2
41 6
...
-1 0
After the -1 0, my program crashes. I don't know why my program crashed. Any help would be appreciated!
#include <iostream>
usingnamespace std;
//Function prototypes
int lowestQuizScore(int a[]);
int highesstQuizScore(int a[]);
int largestFrequency(int a[]);
constint MAX = 51; //0 - 50 for 51 possible quiz score values
constint SENTINEL = -1; //denotes end of data set
int main()
{
system("cls"); //clears extraneous texts
cout << "Welcome to the Quiz Score Frequency Analyzer, by " << endl;
cout << " " << endl; //spacing
cout << "Enter a list of pairs of values: \"QuizScoreValue ScoreCount\". " << endl;
cout << "Example: 35 5 indicates 5 more students received a score of 35." << endl;
cout << "Enter \"-1 0 \" when finsihed: " << endl;
int myArray[MAX]; //initialize array to the number of elements
for (int i = 0; i < MAX; i++)
{
myArray[i] = 0; //initialize all elements in the array to 0 (frequency count for scores)
}
int quizScore;
int frequency;
cin >> quizScore >> frequency; //reads in quiz score and reads in frequency associated with quiz score
while (quizScore != SENTINEL) //while quizScore does not equal -1
{
myArray[quizScore] += frequency; //adds frequency to associated quiz score
cin >> quizScore >> frequency;
}
int lowestScore = lowestQuizScore(myArray); //returns the lowest quiz score
int highestScore = highesstQuizScore(myArray); //returns highest quiz score
int largestFrequencyCount = largestFrequency(myArray); //returns largest frequency count
cout << "The smallest score value is " << lowestScore << endl;
cout << "The largest score value is " << highestScore << endl;
cout << "The largest frequency count is " << largestFrequencyCount << endl;
}
//This function.......
int lowestQuizScore(int a[])
{
int i = 0;
int frequency = a[i];
while (frequency == 0) //stops when it finds first quiz score with a value in it
{
i++; //increment through the array
int frequency = a[i];
}
return i;
}
//This function....
int highesstQuizScore(int a[])
{
int i = MAX - 1; //start at end of array
int frequency = a[i];
while (frequency == 0)
{
i--; //decrements through array
int frequency = a[i];
}
return i;
}
int largestFrequency(int a[])
{
int max = 0;
for (int i = 0; i < MAX; i++)
{
int num = a[i];
if (num >= max)
{
max = num;
}
}
return max;
}
You should run your program line by line to see which line causes the problem. Then, if you still don't know why that line causes the problem, update your question, so everyone can easily answer.
I think I sort of figured out the issue. My program has an infinite loop, therefore causing it to crash. In lines 56 - 76, the program seemed to keep looping forever. I don't see what the problem is though.
The way I would handle this is initialize a biggest grade, smallest grade, and biggest frequency variable with the first input. Then, take input from the keyboard until it equals to -1, 0. If the input of the grade is bigger than the biggest grade, change its value, if the input of the grade is smaller that the smallest grade change its value, and if the frequency is bigger that the biggest frequency change its value. This is the code.
#include<iostream>
usingnamespace std;
int main(){
int helper1, helper2;
cin>>helper1>>helper2;
int big_grade = helper1; //initializes with the first values
int big_freq = helper2;
int small_grade = helper1;
while(!cin.eof()){ //this means, while input from keyboard hasn't stopped
cin>>helper1>>helper2;
if (helper1==-1 && helper2==0){ //use break; so there won't be an infinite loop and stop the while loop
break;
}
elseif(big_grade<helper1){ //checks if big_grade is smaller than grade input
big_grade=helper1;
}
elseif(small_grade>helper1){ //checks if small_grade is bigger than grade input
small_grade = helper1;
}
if (big_freq<helper2){ //checks if big_frequency is smaller than frequency input
big_freq=helper2;
}
}
cout<<"The biggest grade is: "<<big_grade<<endl; //output the data.
cout<<"The smallest grade is: "<<small_grade<<endl;
cout<<"The biggest frequency is: "<<big_freq<<endl;
return 0;
}
If you have any questions as with how this code works just ask and I'd be glad to explain. We have to understand something in order to use it.