i will post a link for bonus point ..u jus tneed to comment on that link #includ
ID: 3555128 • Letter: I
Question
i will post a link for bonus point ..u jus tneed to comment on that link
#include<iostream>
#include<fstream>
#include<algorithm>
#include<climits>
#include <string>
using namespace std;
int readArray(ifstream& ifile, long arr[])
{
//this is how you print out you array
for(int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
}
void sortArray(long arr[],int numberInTheArray)
{
// changes length to numberInTheArray
}
int main()
{
string fname,line;
long i, ithVal, min, max;
long arr[1000];
cout<<"Input File Name: ";
cin >> fname;
ifstream myfile;
myfile.open(fname.c_str());
if (!myfile)
{
cout<<"Unable to open "<<fname<<endl;
return 1;
}
cout<<"Which number do you want to return? ";
cin>>i;
//read array
cout<<"Before sort: "<<endl ;
cout<<"Min is {"<<min<<"}. ";
cout<<"Max is {"<<max<<"}. ";
if(ret!=1000)
cout<<"Warning: Only "<<ret<<" numbers were read into the array"<<endl;
if(i>1000)
{
cout<<i<<" is bigger than 1000!"<<endl;
}
else if(i<ret)
cout<<"Value{"<<i<<"} is "<<ithVal<<". ";
else
cout<<"There aren't that many numbers in the file! ";
sortArray(arr,ret);
cout<<" After Sort"<<endl;
cout<<"Min is {"<<min<<"}. ";
cout<<"Max is {"<<max<<"}. ";
if(ret!=1000)
cout<<"Warning: Only "<<ret<<" numbers were read into the array"<<endl;
if(i>1000)
{
cout<<i<<" is bigger than 1000!"<<endl;
}
else if(i<ret)
cout<<"Value{"<<i<<"} is "<<arr[i]<<". ";
else
cout<<"There aren't that many numbers in the file! ";
myfile.close();
return 0;
}
To get started, copy the whole project for Homework 7! If you don't know how to do this, then create a new project and just copy and paste the code in the *.cpp file. As for the previous assignment, the code must prompt the user for an input filename and a value that is used as the position of the number in the array to display (It is the position in the array thus time instead of "in the file."). For the user enters 100, then you will print the 100th number in the array as part of the output. If the user enters a number greater that the number of values in the array, then you print an error message(see below).Explanation / Answer
#include<iostream>
#include<fstream>
#include<algorithm>
#include<climits>
#include <string>
using namespace std;
int readArray(ifstream& ifile, long arr[])
{
long n;
int i = 0;
while(ifile>>n)
{
arr[i]=n;
i++;
if(i==1000)
break;
}
return i;
}
void sortArray(long arr[],int numberInTheArray)
{
sort(arr,arr+numberInTheArray);
}
int main()
{
string fname,line;
long i, ithVal, min, max;
long arr[1000];
cout<<"Input File Name: ";
cin >> fname;
ifstream myfile;
myfile.open(fname.c_str());
if (!myfile)
{
cout<<"Unable to open "<<fname<<endl;
return 1;
}
cout<<"Which number do you want to return? ";
cin>>i;
int ret = readArray(myfile,arr);
max = LONG_MIN;
min = LONG_MAX;
for(int j=0;j<ret;j++)
{
if(max<arr[j])
{
max=arr[j];
}
if(min>arr[j])
{
min=arr[j];
}
}
cout<<" ";
cout<<"Before Sort:"<<endl ;
cout<<" Min is {"<<min<<"}. ";
cout<<" Max is {"<<max<<"}. ";
if(ret<1000 && i<=1000)
cout<<" WARNING: Only "<<ret<<" numbers were read into the array!"<<endl;
if(i>1000)
{
cout<<" "<<i<<" is bigger than 1000!"<<endl;
}
else if(i<ret)
cout<<" Value{"<<i<<"} is "<<arr[i-1]<<". ";
else
cout<<" There aren't that many numbers in the array! ";
sortArray(arr,ret);
cout<<" After Sort:"<<endl;
cout<<" Min is {"<<min<<"}. ";
cout<<" Max is {"<<max<<"}. ";
if(ret<1000 && i<=1000)
cout<<" WARNING: Only "<<ret<<" numbers were read into the array!"<<endl;
if(i>1000)
{
cout<<" "<<i<<" is bigger than 1000!"<<endl;
}
else if(i<ret)
cout<<" Value{"<<i<<"} is "<<arr[i-1]<<". ";
else
cout<<" There aren't that many numbers in the array! ";
myfile.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.