Array File - Sorting/Ith Number/Max/Min

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  //these are my includes
#include <iostream>
#include <string>
#include <fstream>

using namespace 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;
		}
		else if (returnvalue==1000)
		{
			cout << " Value " << readIthVal << " is {" << ith << "}." << endl;
		}
		else if (returnvalue!=1000 && readIthVal<returnvalue)
		{
			cout << " WARNING: Only " << returnvalue << " numbers were read into the array!" << endl;
			cout << " Value " << readIthVal << " is {" << ith << "}." << endl;
		}
		else if (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;
		}
		else if (returnvalue==1000)
		{
			cout << " Value " << readIthVal << " is {" << ith << "}." << endl;
		}
		else if (returnvalue!=1000 && readIthVal<returnvalue)
		{
			cout << " WARNING: Only " << returnvalue << " numbers were read into the array!" << endl;
			cout << " Value " << readIthVal << " is {" << ith << "}." << endl;
		}
		else if (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;
			}
		}
	}
}
Last edited on
Have you considered using std::sort instead of writing and maintaining your own (potentially buggy) sorting code?

The <algorithm> header is your best friend in the world:
http://www.cplusplus.com/reference/algorithm/
http://en.cppreference.com/w/cpp/header/algorithm
Look at the name and purpose of each function.
My professor doesn't allow it, he said use the bubble sort that he provide for us, that's why I'm using it :(.

I also submitted a version with algorithm, and that gave me the same response from the submission.
vextria wrote:
I also submitted a version with algorithm, and that gave me the same response from the submission.
It's not magic. You need to pick up the tools and use them, not just put the toolbox on top of the hood of the car and hope it fixes itself.
Topic archived. No new replies allowed.