Using C++ Sample files: http://www.mediafire.com/download/aaa8kfq3ob3y0gq/sample
ID: 3675115 • Letter: U
Question
Using C++
Sample files: http://www.mediafire.com/download/aaa8kfq3ob3y0gq/samplefiles7.zip
Write the following code: Main program: declare an array of 100 integers call openFile call getData call getExtremes call output Functions: openFile: ask the user for the name of an input file and read their response try to open the specified file if the file did not open successfully: write out an error message ask for a new name and try to open that file repeat asking for file names until one opens successfully getData: read up to 100 integer values in from the open input file, storing all of the values into the array. Send back the array with the new values and the count of how many values were stored into the array (the data size). getExtremes: go through all of the data in the array and bring back both the largest and the smallest values in the array data. output: write all of the array data to the screen, with 10 values per line write out the extreme values (largest and smallest) that were previously found in main. 3 sample text files have been provided for testingExplanation / Answer
assignment.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void openFile(ifstream& fl);
void getData(ifstream& fl,int arr[],int& count);
void getExtremes(int arr[],int count,int& min,int& max);
void output(int arr[],int count,int min,int max);
int main()
{
int arr[100];
int num=0;
int min=0,max=0;
ifstream fl;
openFile(fl);
getData(fl,arr,num);
getExtremes(arr,num,min,max);
output(arr,num,min,max);
}
void openFile(ifstream& fl)
{
string fileName;
cout<<"Enter the file name"<<endl;
cin>>fileName;
fl.open(fileName);
int i=0;
if(!fl.is_open())
{
cout<<"Enter a valid filename"<<endl;
openFile(fl);
}
}
void getData(ifstream& fl,int arr[],int& count)
{
int i=0;
while(fl>>arr[i] && i<100)
{
i++;
}
count=i;
}
void getExtremes(int arr[],int count,int& min,int& max)
{
min=arr[0];
max=arr[0];
for(int i=1;i<count;++i)
{
if(arr[i]<min)
{
min=arr[i];
}
if(arr[i]>max)
{
max=arr[i];
}
}
}
void output(int arr[],int count,int min,int max)
{
for(int i=0;i<count;++i)
{
if(i%10==0 && i!=0)
{
cout<<endl;
}
cout<<arr[i]<<" ";
}
cout<<endl;
cout<<"Extremes:"<<endl;
cout<<"Maximum: "<<max<<endl;
cout<<"Minimum: "<<min<<endl;
}
sample output:
onetwelve.txt
Enter the file name
/Users/ankit2204/Documents/onetwelve.txt
160 58 125 142 101 123 56 30 34 158
151 154 55 66 77 160 97 33 148 44
132 27 118 111 16 57 113 83 69 29
143 43 69 147 152 89 10 97 50 25
142 63 25 31 39 158 133 17 93 111
68 130 32 26 55 89 150 120 21 146
129 101 92 132 103 32 143 124 23 25
149 126 42 126 72 15 75 30 143 29
44 133 131 159 33 83 103 91 34 104
77 40 35 58 143 20 120 14 139 12
Extremes:
Maximum: 160
Minimum: 10
thirtytwo.txt
80 -74 89 75 -99 29 -34 40 -88 60
-33 -77 -91 50 -15 -14 93 3 -27 -19
93 15 -12 31 -30 -37 58 -69 7 36
6 -84
Extremes:
Maximum: 93
Minimum: -99
seventyfive.txt
98 -22 49 52 36 -29 33 -83 -84 53
-94 56 10 -31 39 80 91 -33 1 54
12 -83 27 1 -48 -100 14 -59 -96 37
39 14 59 84 62 -53 -3 100 29 36
41 -54 14 -10 56 11 -46 17 -36 -95
-24 74 -26 -73 -48 11 -33 59 -38 40
-55 -5 24 15 -77 -81 -58 57 -90 16
-65 -22 67 66 39
Extremes:
Maximum: 100
Minimum: -100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.