Read integers into an array and count the repeating integers

Write a program that reads a list of integers into an array with base type int. Provide the
capability to either read the list of integers into the array from the keyboard, or from a file, at
the user’s option. If the user chooses file input, the program should request a file name. You
may assume that there are fewer than 50 entries in the array. Your program determines how
many entries there are. The output is to be a two column list. The first column is a list of the
distinct array element; the second column is the count of the number of times each element
occurs in the array.
For example, for the input:
-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
The output should be:
N Count
4 2
3 3
2 2
1 4
-1 1
-12

so far I have

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

void sort(int array[], int size);

int main()
{
//Declare Variables
ifstream fin;
int count = 1;
int list[50];
int x = 0;
char ans;
string fileName;

//Ask what form they want to input
cout << "Enter f to enter from a file or Enter m for manual key in" << endl;
cin >> ans;

//Calculate if by file
if ((ans=='f') || (ans=='F'))
{
cout << "Please enter Filename (less than 20 characters)" << endl;
cin >> fileName;

//Open file check for failings
fin.open(fileName.c_str());

if(fin.fail())

{
cout << "Input File Error" << endl;
exit (1);
}

//Read in the file into array

for (!fin.eof(); x++;)
{
fin >> list[x];
}
}
sort(list, x);
int last = list[0];


int i;

for(i=1; i < x; i++)
{
if (list[i] == last)
count++;

else
{
cout << " " << list[i-1] << " " << count << endl;
last = list[i];
count = 1;
}

}
cout << " " << list[i-1] << " " << count << endl;
return 0;
}
void sort(int array[], int size)
{
for (int x=1; x < size; x++)
{
for (int y = x-1; y>=0 && array[y+1]<array[y]; y++)
{
int temp = array[y+1];
array[y+1] = array[y];
array[y] = temp;
}
}
}


This is what I have and it is not outputting anything correctly. Any help would be great!
That sorting is not the way to do it, it unnecessarily complicates code and takes longer.

Before I started to analyze your code, I tried to write my own first and found a working solution (and more optimal).
http://ideone.com/kMGbjX

Just in case you were looking for a solution without trailing 0's:

http://ideone.com/v4hm6G

it's not as clean though.

Basically, a problem like this would need to be broken down into steps. I agree with Luntri that sorting won't do much good.

First you'll need to find which numbers are the duplicates and store them. Then you'll need to find out how many times each of show up and store those values. At the end you'll be able to display the vectors/lists/arrays together to get all the values.

@unsensible your code doesn't display a count for -1 so it looks like you have a bug somwhere.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <iomanip>
#include <functional>
using namespace std;

int main()
{
	string input = "-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12";
	stringstream ss(input);
	
	const int SPACING = 3;

	const size_t SIZE = 50;
	int myArray[SIZE] = { 0 };
	
	int count = -1;
	
	//fills the array with integers
	while (ss >> myArray[++count]);//NULL statement

	//create pointers to the beginning and end of the array
	int *beg = myArray;
	int *end = myArray + count;
	
	
	//Sorts the array
	sort(beg, end, greater<int>());

	int current = myArray[0];
	count = 1;

	
	//counts the number of each value in the array
	while (++beg != end)
	{
		
		if (current != *beg)
		{
			cout << setw(SPACING) << current << setw(SPACING) << count << endl;
			current = *beg;
			count = 0;
		}

		++count;
	}
	
	//outputs the final value
	cout << setw(SPACING) << current << setw(SPACING) << count << endl;

	cin.ignore();
	return 0;
}
Last edited on
Oh I removed it because it wasn't a duplicated value. I see now that they wanted a count for all values and not just duplicated ones.

Oh well, there's two other solutions he can use.
Thank you all for your help!! I really appreciate it!
Topic archived. No new replies allowed.