Write a program that reads in a list of integers into an array with base type in
ID: 3767619 • Letter: W
Question
Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this 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 entires in the array. You program determines how many entires there are. The output is to be a two-column list. The first column is a list of the distinct array elements; the second column is the count of the number of occurrences of each element. The list should be sorted on entires in the first column, largest to smallest.
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 4
Explanation / Answer
Here is the code for you. If you have any further queries, just get back to me.
#include <iostream>
#include <fstream>
using namespace std;
int found(int num, int numbers[], int size)
{
for(int i = 0; i < size; i++)
if(num == numbers[i])
return i;
return -1;
}
void insert(int num, int numbers[], int count[], int size)
{
int i;
for(i = size; i>= 0; i--)
if(num > numbers[i])
{
numbers[i+1] = numbers[i];
count[i+1] = count[i];
}
else
break;
numbers[i+1] = num;
count[i+1] = 0;
}
void keyboardReading()
{
int num, numbers[50], count[50], arraySize = 0; //Initialize the array size to 0.
cout<<"Enter the number (999 to stop): ";
cin>>num; //Read a number.
for(int i = 0; i < 50; i++) //Initialize all counts to 0.
count[i] = 0;
while(num != 999) //Till the read number is not 999.
{
if(found(num, numbers, arraySize) != -1) //If the number is readily in the array.
count[found(num, numbers, arraySize)]++; //The relavent count array position is incremented.
else //If the number is not in the array.
{
insert(num, numbers, count, arraySize); //Add it to the array.
count[found(num, numbers, arraySize)]++; //The relavent count array position is incremented.
arraySize++; //Array size is incremented.
}
cout<<"Enter the number (999 to stop): "; //Read another number.
cin>>num;
}
for(int i = 0; i <= arraySize; i++)
cout<<numbers[i]<<" "<<count[i]<<endl;
}
void fileReading()
{
string fileName;
int num, numbers[50], count[50], arraySize = 0;
for(int i = 0; i < 50; i++)
count[i] = 0;
cout<<"Enter the name of the file: ";
cin>>fileName;
ifstream file;
file.open(fileName);
while(!file.eof())
{
file>>num;
if(found(num, numbers, arraySize) != -1)
count[found(num, numbers, arraySize)]++;
else
{
insert(num, numbers, count, arraySize);
count[found(num, numbers, arraySize)]++;
arraySize++;
}
}
for(int i = 0; i < arraySize; i++)
cout<<numbers[i]<<" "<<count[i]<<endl;
}
int main()
{
char keyboardOrFile;
cout<<"Do you want me to read from a (k)eyboard or a (f)ile: ";
cin>>keyboardOrFile;
if(keyboardOrFile == 'k')
keyboardReading();
else if(keyboardOrFile == 'f')
fileReading();
else
cout<<"Invalid option. You should choose with either keyboard or file."<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.