Unexplained error

Write a program that reads in a list of integers into an array with base type of int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines 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 second 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.

For example, for the input
-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12

the output should be
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4


Here is my code so far, I'm just trying to get it to sort so far. I haven't tested the file input method. When I run the program I get an error, but it's not in the error list. I get a pop up that says,

"A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available."

HELP PLEASE!!


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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
void sort(int a[], int& used);
void swap(int&  v1, int& v2);
int big(int a[], int start, int& used);
using namespace std;
int main()
{
	ifstream fin;
	string name;
	char ans;
	int count[50];
	int num[50];
	int j,k=0,used=0,a=0;

	cout<< "How would you like to input data \n 'F' for file, 'K' for keyboard.";
	cin>> ans;

	if ((ans == 'f') || (ans == 'F'))
	{
		cout<<"Enter file name. \n";
		cin>> name;

		fin.open(name);
		
		if (fin.fail())
		{
			cout<<"File open failed!";
			exit(1);
		}

		while(fin >> j)
		{
			num[k] = j;
			k++;
			used++;
		}
	}

	else
	{
		cout<<"Enter up to 50 integers. Type '.1' to signify you're done. \n";

	while((num[k] != '.1') || (num[k] != '.1'))
	{
		cin>> num[k];
		k++;
		used++;
	}
	}

	sort(num, used);


	while(num[a]<used)
	{
		cout<< num[a];
		a++;
	}


	return 0;
}

void sort(int a[], int& used)
{
	int bigger;
	for (int ind= 0; ind < used-1; ind++)
	{
		bigger=big(a,ind,used);
		swap(a[ind],a[bigger]);
	}
}

void swap(int&  v1, int& v2)
	{
		int temp;
		temp=v1;
		v1= v2;
		v2 = temp;
	}	


int big(int a[], int start, int& used)
{
	int max= a[start],imax=start;

	for(int inde= start+1; inde < used; inde++)
	{
		if (a[inde]>max)
		{
			max= a[inde];
			imax=inde;
		}
	}

		return imax;
}
Usually that kind of error has to do with the code using an array over its size. Say for example, you declare an array as having 50 fields and (unnoticedly) try to write/read from its 51th position (or 60th, or 100th).
In your case, on line 69 you declare a value (bigger) that is not initialized (thus, contains garbage). I guess you assume it has a value of zero.
On line 73, the program explotes: a[bigger] may signify a[890243214323]. You try to acces a field in the array far away of its borders... The program tries to please you, starts serching a place in memory that's not allowed to use and the operating system says NO WAY YOU' RE TOUCHING THAT PLACE IN MEMORY. And sends a signal to kill the process.

EDITED: I'm sorry. I see you DO assign a value to bigger before using it as an index... My mistake. Anyway, I would try to catch the error doing some bound-checking for every time you try to acces your array's values. Surely theres some part in the code where you go over 50th place.

try this every time:

1
2
3
4
5
6
7
8
9
10
11
const int array_size = 50; // on line 9 (you may want to change it later

//...

if (bigger < array_size){
   // do something with num[bigger]
} else {
   cout << "ERROR: Trying to reach " << bigger << "th position of the array" << endl;
   return 0;
}
Last edited on
I still get the same error after I initialize bigger as 0, would there be another place where the same error occurs?
I edited my previous answer, I'm sorry I made a mistake interpreting your code.
This is my modified code. I still get the same error

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
void sort(int a[], int used);
void swap(int&  v1, int& v2);
int big(int a[], int start, int used);
const int ARRAYN = 50;
using namespace std;
int main()
{
	ifstream fin;
	string name;
	char ans;
	int count[ARRAYN];
	int num[ARRAYN];
	int j,k=0,used=0,a=0;

	cout<< "How would you like to input data \n 'F' for file, 'K' for keyboard.";
	cin>> ans;

	if ((ans == 'f') || (ans == 'F'))
	{
		cout<<"Enter file name. \n";
		cin>> name;

		fin.open(name);
		
		if (fin.fail())
		{
			cout<<"File open failed!";
			exit(1);
		}

		while(fin >> j)
		{
			num[k] = j;
			k++;
			used++;
		}
	}

	else
	{
		cout<<"Enter up to 50 integers. Type '.1' to signify you're done. \n";

	while((num[k] != '.1') || (num[k] != '.1'))
	{
		cin>> num[k];
		k++;
		used++;
	}
	}

	sort(num, used);


	while(num[a]<used)
	{
		cout<< num[a];
		a++;
	}


	return 0;
}

void sort(int a[], int used)
{
	int bigger=0;
	for (int ind= 0; ind < used-1; ind++)
	{
		bigger=big(a,ind,used);
		


		if (bigger < ARRAYN)
		{
			swap(a[ind],a[bigger]);			
		} 
		else
		{
			cout << "ERROR: Trying to reach " << bigger << "th position of the array" << endl;
		}
	}
}



void swap(int&  v1, int& v2)
	{
		int temp;
		temp=v1;
		v1= v2;
		v2 = temp;
	}	


int big(int a[], int start, int used)
{
	int max= a[start],imax=start;

	for(int inde= start+1; inde < used; inde++)
	{
		if (a[inde]>max)
		{
			max= a[inde];
			imax=inde;
		}
	}

		return imax;
}
Ok. I'm looking into it better now, and there are other problems:

on line 47:

Suppose num[k] = .1

while ((num[k] != '.1') || (num[k] != '.1'))

this will never be false. You should use:

while ((num[k] != '.1') && (num[k] != '.1'))

I know it's counterintuitive, but try to go through it case by case and you' ll see

Anyway, what kind of number is '.1'? You mean 0.1? It's OK to write it as .1, but without single quotes. Single quotes are used with characters, and with only one at a time:

1
2
3
4
5
6
char option = 'y';
char init = 'M';
char x ='$';

char wrong = '.1' // this is not right


I'll come back with more news later...

Keep debugging my friend. This is the best school
Last edited on
Anyway, you shouldn't try to assing a double to an int var. You should recognize the end of the series with -1 or something you know you won't use, but integer.

Another problem:

1
2
3
4
5
6
while((num[k-1] != -1) && (num[k-1] != -1))
	{
		cin>> num[k];
		k++;
		used++;
	}


Notice I'm using [k-1]. You should check that the last value entered was not -1. The next... you simple don't know yet

Oh wow that's a big boo boo! k is supposed to be an integer I used a double to be a sentinel value hoping it won't go into my integer array. It used to be a character 'x', but that didn't work but the double did.

[UPDATE]
still have the same error..
Last edited on
Try with -1 and you'll see that there are other problems as well. For example, after recieving input from the user, it sorts the array (supposedly) but doesn't display it on the screen.
Try to go part by part, including little messages for your self like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void sort(int a[], int used)
{
        cout << "I'm in sort function now" << endl;
	int bigger=0;
	for (int ind= 0; ind < used-1; ind++)
	{
                cout << "Entered the for loop, ind = " << ind << endl;
                cout << "Sending this to function 'big':" << a << "  " << ind << "  " << used << endl; 
		bigger=big(a,ind,used);
		


		if (bigger < ARRAYN)
		{
			swap(a[ind],a[bigger]);			
		} 
		else
		{
			cout << "ERROR: Trying to reach " << bigger << "th position of the array" << endl;
		}
	}
OK well I scrapped that trail of functions and started new.
Here is my new code. It enters into the function, when I tried to cout the array sorted nothing would show..

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
void sort(int num[], int used);
using namespace std;
int main()
{
	ifstream fin;
	string name;
	char ans;
	int num[50];
	int j,k=0,used=0,a=0;

	cout<< "How would you like to input data \n 'F' for file, 'K' for keyboard.";
	cin>> ans;

	if ((ans == 'f') || (ans == 'F'))
	{
		cout<<"Enter file name. \n";
		cin >> name;

		fin.open(name);
		
		if (fin.fail())
		{
			cout<<"File open failed!";
			exit(1);
		}

		while(fin >> j)
		{
			num[k] = j;
			k++;
			used++;
		}
	}

	else
	{
		cout<<"Enter up to 50 integers. Type '-7' to signify you're done. \n";

	while(used < 50)
	{
		cin>> num[k];
		if (num[k]==-7)
			break;

		k++;
		used++;
	}
	}

	sort(num,used);

	return 0;
}

void sort(int num[], int used)
{
	int sorted[50];
	int d=0,q=used,max=0;

	for (used; used > -1; used--)
	{
		while(q > -1)
		{
		if (num[q] > num[used])
			max= q;
		else if (num[used] == num[q])
			q--;
		else
			max= used;
		}

		sorted[d]=max;

		d++;
	}

}


This is not the solution, but try to use this sort function and see what happens

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
void sort(int num[], int used)
{
	int sorted[50];
	int d=0,q=used,maximum=0;

	for (used; used > -1; used--)
	{
        cout << "I'm in the for " << endl;
        cout << "   used = " << used << endl;
        cout << "      q = " << q << endl;
        cout << "maximum = " << maximum << endl;
 		while(q > -1)
		{
		cout << "Surprise, how am I going to get out?" << endl;
		if (num[q] > num[used])
			maximum = q;
		else if (num[used] == num[q])
			q--;
		else
			maximum= used;
		}

		sorted[d]=maximum;

		d++;
	}
   // Hint: have you thought about how to return sorted[] to main?

}
Last edited on
Couting isn't really helping me that much, I don't understand what error I'm really looking for. Is there a way to sort the array without any functions?
Rogge, I'm gonna take off. I hope you find the best solution.
As you'd have seen, the sort function needs some fixing. Don't give up, it's by debugging slowly and patiently that I've learned the most. Good Luck!
Topic archived. No new replies allowed.