This is my code so far

#include<fstream>
#include<iostream>
#include<cstdlib>
#include<stdlib.h> 
using namespace std; 

const int MAX_GENERATED = 100;

void fillArray(int a[], int size, int& numberUsed); //prototype

int main ()
{
     
     int array[MAX_GENERATED], numberUsed;
     fillArray (array, MAX_GENERATED, numberUsed);
     
    
    getchar ();
    getchar ();
    return 0;
}

void fillArray( int array[], int size, int& numberUsed)
{
     
     ifstream inStream;
     string fileName;
     cout << "What file would you like to work with?" << endl; 
     cout << "Remember to include the file extension." << endl;
     cin >> fileName;
     inStream.open(fileName.c_str()); //to connect with file
     
     int next, index = 0;
     inStream >> next;
     while (index < size) //loop fills array
     {
           array[index] = next;
           cout << array[index] << endl;
           index++;
           inStream >> next;
     } //end while loop
     
     numberUsed = index;//maybe can do something with this
     
     //might need 2d array??
}

It reads the input from a text file with one number per line with a maximum of 100 numbers in the file. These were numbers from -20 to 20 and were randomly generated.
I now have to display each number and the number of times it appears in the input file
It has to look something like this:

--------------
N COUNT
--------------
-12 4
3 3
4 2
1 4
-1 1
2 2
--------------
Total = 16
--------------

The problem with the code as it is, is that there are 20 numbers in the file, the last one being a randomly generated 15. When the program displays the numbers it does fine, but displays the number "15" eighty times. I want to know how to fix this, if possible.

Also, how may I go about counting the number of times each number appears?
I know I can do an if statement that if the number is -20, increment a counter and do that for up to 20. However, this method is tedious and I don't think it is desired for this.

I appreciate any help at all on this, I hope I've given enough information.

I don't understand why you have a inStream >> next; statement before your while loop...

The reason why you are getting such strange results when inputting is that you are inputting data past the end of file (ie loading in 100 values when the file only contains 16). Instead of comparing the index to the size, compare it to an input failure like this:

while (!inFile.fail())
{
...code...
}

To associate each number with its count, a number of methods may be used, such as a hash table, a struct containing size and count, a linked list, or a test as the output is being fed (ie when outputting create a nested for loop, the outer one counting up from the lowest value inputted to the highest, and the inner one iterating the array and counting the number of occurences - and display nothing if no values are found). I'm not too sure what your technical knowledge is of the above methods, so just ask about whichever you feel more comfotable with and I will explain it to you more thoroughly.

Note that a 2d array won't help you much at all :P

Thanks for helping me. I really appreciate it. In fact, you helped me more in this than did the TAs in one hour. (She kept suggesting that I use another loop to break me out of the eighty "15"s which did not work at all)

As for counting the numbers, could you elaborate on counting them using the nested for loops? We are just getting into sorting array from high to lowest and such. I don't think, I have enough skill for the other methods you mentioned, plus the professor might not like me being ahead of the class.

Thanks for helping me. I really appreciate it. In fact, you helped me more in this than did the TAs in one hour. (She kept suggesting that I use another loop to break me out of the eighty "15"s which did not work at all)

As for counting the numbers, could you elaborate on counting them using the nested for loops? We are just getting into sorting array from high to lowest and such. I don't think, I have enough skill for the other methods you mentioned, plus the professor might not like me being ahead of the class.

One, show your updated code please, as far as how you are reading in the values. Until you have that working, don't worry about counting the occurrences. How do you intend to store the number of occurrences? In an array? A vector? Are you going to set up a struct? If it is an array, you could do something like this:

int occurrences[41];; // since range is -20 to 20 (41 different numbers in that range)

occurrences[0] could represent the number of times -20 occurs, occurrences[1] could represent the number of times -19 occurs, and so on. I don't see anything like this in your code though, so until I know how you are planning to store it, I don't know where you are trying to go with the nested loop. Certainly you don't want a massive bunch of 41 if statements.

For the loop, do something like this:

int iCount[41] = {0};
for (int i(0); i < size; i++)
     iCount[array[i] + 20]++;

Although this is a pretty awkward way of doing it. It requires you to know the range of values before hand, will only ever work for integer values (since an array can only be passed an unsigned int), and will take up a ludicrous amount of stack space if anticipating very large numbers (since a seperate int is pushed onto the stack for every number anticipated, even if it never occurs). A more dynamic approach would be most robust and useful.

Here is a pretty crappy version of a more dynamic solution. It would make more sense to make the array internal to the class, and allocate memory more dynamically, but it still demonstrates my point

#include<iostream>
using namespace std;


class Numbers
{
	int iValue;
	int iCount;
public:
	inline bool bExists (Numbers * numarray, int iSize, int iVal)
	{
		for (int i(0); i < iSize; i++)
			if (numarray[i].iValue == iVal)
				return true;
		return false;
	}
	inline int GetCount (Numbers * numarray, int iSize, int iVal)
	{
		for (int i(0); i < iSize; i++)
			if (numarray[i].iValue == iVal)
				iCount++;
		return iCount;
	}
	inline int GetIndex (Numbers * numarray, int iSize, int iVal)
	{
		for (int i(0); i < iSize; i++)
			if (numarray[i].iValue == iVal)
				return i;
		return -1;
	}
	inline void AddNum (Numbers * numarray, int iSize, int iVal)
	{
		if (!bExists(numarray, iSize, iVal))
			numarray[GetFreeSpot(numarray, iSize)].iValue = iVal;
		else
			numarray[GetIndex(numarray, iSize, iVal)].iCount++;
	}
	inline int GetFreeSpot(Numbers * numarray, int iSize)
	{
		return GetIndex(numarray, iSize, 0);
	}
	inline int GetNum () 
	{
		return iValue;
	}
	Numbers(void): iValue(0), iCount(0) {}
	~Numbers(void) {};
};
int main()
{
	Numbers * pNums;
	int iSize(0);
	int iNum(0);
	cout << "Enter array size: ";
	cin >> iSize;
	pNums = new Numbers [iSize];
	//load in the values
	for (int i(0); i < iSize; i++)
	{
		cout << "\nValue " << i + 1 << ": ";
		cin >> iNum;
		pNums[i].AddNum(pNums, iSize, iNum);
	}
	for (int i(0); i < iSize; i++)
	{
		if (pNums[i].GetNum())
			cout << "Number: " << pNums[i].GetNum() << " " << "Count: " << pNums[i].GetCount(pNums, iSize, pNums[i].GetNum()) << endl; 
	}
	cin.ignore(cin.rdbuf()->in_avail());
	cin.get();
                delete [] pNums;
	return 0;
}

Note that this won't count zeros, considering them null values

This article has been dead for over six months. Start a new discussion instead.