Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads in a list of integers in to an array with base type i

ID: 3880435 • Letter: W

Question

Write a program that reads in a list of integers in to an array with base type int . Read this array from the input file, called infile.txt . (The program should read in the data into an array calle d, numbers [ ], which can store all data from the input file.) You may as sume that there are fewer than 20 entries in the array. Your program de termines how many entries there are. The output is to be a two-column list. The first column is a list of the distinct array elements; the secon d column is the count of the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest. USE only function calls in the main program. (Only minimum implementation will be allowed in the main program.) c++

Explanation / Answer

#include<iostream>
#include <fstream>
#include<iomanip>
#include<string>
using namespace std;

//function prototypes
void initialize (int numbers[], int listSize);
void readNum (int numbers[], int& listSize);
void selectSort (int numbers[], int listSize);
void read_file(int numbers[],int&listSize);

int main()
{
int Numbers [100];
int count= 0;
int listsize = 100;
int temp;
int choise=0;


initialize (Numbers, listsize);

cout<<"would you like to enter the array your self or from a file"<<endl;
cout<<"(1)yourself (2)file"<<endl;
cin>>choise;
if(choise == 1)
{
cout<<"you can enter as maximum 100 number -999 to finish"<<endl;
readNum (Numbers, listsize);
cout <<endl;
cout <<fixed<<showpoint<<setw(7)<<"Number"<<setw(9)<<"Count"<<endl;

selectSort(Numbers, listsize);

temp = Numbers[0];
count = 1;
for (int t=1; t< listsize; t++)
{
if(Numbers[t] == temp)
count++;
else
{
cout << "Number = " << temp << " total = " << count<<endl;
temp = Numbers[t];
count = 1;
}
}
// display count of final number
cout << "Number = " << temp << " total = " << count<<endl;
}
else
{
read_file(Numbers,listsize);
cout <<endl;
cout <<fixed<<showpoint<<setw(7)<<"Number"<<setw(9)<<"Count"<<endl;

selectSort(Numbers, listsize);

temp = Numbers[0];
count = 1;
for (int t=1; t< listsize; t++)
{
if(Numbers[t] == temp)
count++;
else
{
cout << "Number = " << temp << " total = " << count<<endl;
temp = Numbers[t];
count = 1;
}
}
// display count of final number
cout << "Number = " << temp << " total = " << count<<endl;
}
return 0;
}

void initialize (int numbers[], int listSize)
{
int index;
int Num = 0;
for (index=0; index < 100; index++)
numbers[index] = 0;
}

void readNum (int numbers[], int&listSize)
{
int index =0;
int num =0;
listSize=0;
while (num != -999)
{
cin >>num;
numbers[index] = num;
index++;
listSize++;
}
}
void read_file(int numbers[],int&listSize)
{
int index =0;
int num =0;
listSize=0;
ifstream ifile;
string filename;

cout<<"Please enter the full file path"<<endl;
cin>>filename;
ifile.open(filename.c_str());

if(!ifile)
cout<<"file not found"<<endl;

else
while (num != -999)
{
ifile >>num;
numbers[index] = num;
index++;
listSize++;
}
ifile.close();
}

void selectSort (int numbers[], int listSize)
{
int index, minIndex, smallestIndex, temp;

for (index=0; index < listSize - 1;index++)
{
smallestIndex = index;
for (minIndex = index + 1; minIndex < listSize; minIndex++)
if (numbers[minIndex] < numbers[smallestIndex])
smallestIndex = minIndex;

temp = numbers[smallestIndex];
numbers[smallestIndex] = numbers[index];
numbers[index] = temp;

}
}   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote