Hi guys. This is my first post and I hope I can get some help. Thanks so much in advance for this. I have what I'm sure is simple but I'm missing something here. I have a file being read from (input.txt) which has no more than 50 grades w/ -1 being a Sentinel value to end the file when looped. I am trying to find a way to create a frequency array that tells me how many times a grade is used so that I can find the mode for it.

This is what I have so far but anytime I run it , the program crashes. I'm using VS2007 and it doesnt give any errors when compiling but crashes when I run it. Please help me out. Thanks!

// Analyze and Output Mean, Median, Mode of Grades from File
//************************************************************************

#include <iostream>	// I/O functions
#include <iomanip>	// I/O formatting functions
#include <string>	// string class
#include <cstdlib>  // abs(), rand(), atof(), etc.
#include <cmath>	// exp(), pow(), trig functions, floor/ceiling, etc.
#include <fstream>  // read to and from files
using namespace std;
 
void BubbleSort(int qAns[], int n);
const int MAX_SIZE_GRADE = 50; // Max number of grades.
const int MAX_SIZE_FREQ = 101; //Max for freq array
const int SENTINEL = -1;  //Value to end loop when read

int main() 
{
	
	int i, freqArray[MAX_SIZE_FREQ], gradeArray[MAX_SIZE_GRADE], mode;
	float mean, median;
	ifstream inFile;
	ofstream outFile;
	
	
	inFile.open("input.txt");
	outFile.open("output.txt");

	//Initialize frequency array elements to 0
	for(i=0; i<MAX_SIZE_FREQ; i++)
	{
	freqArray[i]=0;
	}
	
	i=0;
	//Read input file and store into array
	while(gradeArray[i] != SENTINEL)
	{
		
		inFile>>gradeArray[i];
		freqArray[gradeArray[i]]++;  //Creating frequency array
		i++;
	}
	
	
	//Output what was read in file (for debugging)
	i=0; 
	while(gradeArray[i] != SENTINEL)
	{
		cout<<gradeArray[i]<<", ";
		i++;

	}

	return 0;
}

>inFile.open("input.txt");
>outFile.open("output.txt");
Let's start here. You're not checking to see if the files were opened successfully. All kinds of gremlins can pop up if you use a file stream that failed to open.

>//Read input file and store into array
>while(gradeArray != SENTINEL)
You never initialized gradeArray, so gradeArray is garbage. You access a garbage value and expect predictable behavior.

>inFile>>gradeArray;
>freqArray[gradeArray]++;
This is scary on three levels:

  1. You don't check the input request for success
  2. You don't put a bound on i, so if the file has more than 50 numbers, you're overflowing the gradeArray
  3. You're using values from the file without checking their validity. This is also a potential overflow, but of freqArray this time.

That might help you pinpoint the problem more easily.

Thanks for the reply.

Questions though:

1. How do I know what to initialize gradeArray to? Since I technically dont know how many grades are in the array, should I create a loop and just initialize all the elements (50 of them even if there are lets say 10 grades) to 0?

2. I know its not the file stream error since if I take out the freqArray[gradeArray]++ statement, the output for debugging statements show all the grades in the file.

3. I guess my question then is how do I set up the frequency array since it seems thats whats causing my error.

>1. How do I know what to initialize gradeArray to?
Well, SENTINEL wouldn't be a bad idea, and you can initialize it just like you do the frequency array. But you can get away with not initializing it at all, if you're careful:

i = 0; // Restart the gradeArray iteration

while ( i < MAX_SIZE_GRADE ) {
  int input;

  // Bomb out if we can't read anything
  if ( !( inFile>> input ) )
    break;

  // We'll assign the sentinel after the loop
  if ( input == SENTINEL )
    break;

  gradeArray[i] = input;

  if ( input >= 0 && input < MAX_SIZE_FREQ )
    ++freqArray[input];

  ++i;
}

// Make sure the grades are terminated
gradeArray[i] = SENTINEL;

I bet if you use that code, you'll find that the problem goes away.

>if I take out the freqArray[gradeArray]++ statement, the
>output for debugging statements show all the grades in the file
Think about it for a second. If you take that statement out, it doesn't crash. Could it be that you're hitting one of the problems I mentioned? Re-read scary #3 and think about what happens when you read the sentinel from your file.

>3. I guess my question then is how do I set up the frequency array
You're on the right track. But this experience should teach you that validating your assumptions is priority 1.

Wow that fixed everything. Thanks so much and because of that I was able to find out all the other information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.