Can anyone tell if there is anything wrong with this code? I've been submitting this in and the submission site told me there was something wrong?
This program is used to read in an array of 1000 numbers, before sort, print out max, min, and the ith value, and after sort, print out the same thing.
If the number is under 1000, the program will print otherwise.
//these are my includes
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int readArray(ifstream& ifile, long arr[]);
void sortArray(long arr[], int returnvalue);
int main()
{
long minimum, maximum, arr[1000], ith;
int returnvalue=1; //return value
int arraysize = 1000;
int readIthVal;
ifstream ifile; //variable for myfile
string strVar; //variable for textfile
cout << "Input File Name: "; //prompts for user file
cin >> strVar; //inputs textfile into string
ifile.open(strVar.c_str());
if (!ifile) // the ! means myInfile is non 0, indicating it is OK
{
cout << "\nThat file does not exist!\n"; //shows this if file is wrong
return 1;
}
//prompt asking for i value
cout << "Which number do you want to return? ";
cin >> readIthVal;
if (ifile)
{
returnvalue=readArray(ifile, arr);
minimum = arr[0], maximum = arr[0]; // just setting first element as default min and max
if (readIthVal < arraysize) // if i does not exceed number of elements
{
ith = arr[readIthVal-1]; // then setting corresponding element here
}
int j=0; // loop counter for max/min search
for (int j = 0; j < returnvalue; ++j) // looking up for max and min
{
if (arr[j] < minimum) minimum = arr[j]; // if arr[j] meets our criteria
if (arr[j] > maximum) maximum = arr[j]; // updating corresponding value
}
if (returnvalue==0)
{
cout << "\nThe file is empty!" << endl;
return 0;
}
cout << "\nBefore Sort:" << endl;
cout << " Min is {" << minimum << "}." << endl;
cout << " Max is {" << maximum << "}." << endl;
if (returnvalue>0 && readIthVal>1000)
{
cout << " " << readIthVal << " is bigger than 1000!" << endl;
}
elseif (returnvalue==1000)
{
cout << " Value " << readIthVal << " is {" << ith << "}." << endl;
}
elseif (returnvalue!=1000 && readIthVal<returnvalue)
{
cout << " WARNING: Only " << returnvalue << " numbers were read into the array!" << endl;
cout << " Value " << readIthVal << " is {" << ith << "}." << endl;
}
elseif (returnvalue!=1000 && readIthVal>returnvalue)
{
cout << " WARNING: Only " << returnvalue << " numbers were read into the array!" << endl;
cout << " There aren't that many numbers in the array!" << endl;
}
sortArray(arr, returnvalue);
cout << "\nAfter Sort:" << endl;
cout << " Min is {" << minimum << "}." << endl;
cout << " Max is {" << maximum << "}." << endl;
if (readIthVal < arraysize) // if i does not exceed number of elements
{
ith = arr[readIthVal-1]; // then setting corresponding element here
}
if (returnvalue>0 && readIthVal>1000)
{
cout << " " << readIthVal << " is bigger than 1000!" << endl;
}
elseif (returnvalue==1000)
{
cout << " Value " << readIthVal << " is {" << ith << "}." << endl;
}
elseif (returnvalue!=1000 && readIthVal<returnvalue)
{
cout << " WARNING: Only " << returnvalue << " numbers were read into the array!" << endl;
cout << " Value " << readIthVal << " is {" << ith << "}." << endl;
}
elseif (returnvalue!=1000 && readIthVal>returnvalue)
{
cout << " WARNING: Only " << returnvalue << " numbers were read into the array!" << endl;
cout << " There aren't that many numbers in the array!" << endl;
}
}
return 0;
}
int readArray(ifstream& ifile, long arr[]) // reading array from file here
{
int count=0; // loop counter
int arraysize = 1000;
int num;
ifile >> num;
while (ifile)// trying to read 1000 numbers here
{
arr[count]=num;
count++;
ifile >> num;
}
return count; // returning default array
}
void sortArray(long arr[], int returnvalue)
{
long temp;
int count=returnvalue;
for(int i=0; i<count-1; i++)
{
for(int j=0; j<count-1; j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}