Cant get my program to print my Arrays

Hey thank you if you can help me. I cant seem to get my arrays to print or do anything. I'm trying to use bubble sort and selection sort. my problem is that visual studio says I have errors and fails when debugging. I'm not sure what to do.
Thank you for your help and time!
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


//includers
#include <iostream>
#include <iomanip>

//using name space
using namespace std;

int main()
{
	
	//variable in arrays
	const int ISIZE = 7;

	//Array A
	int A[ISIZE] = { 88,75,71,31,55,98,89 };
	selectionSort(A, ISIZE);

	//Array B
	const int B[ISIZE] = { 88,75,71,31,55,98,89 };
	bubbleSort(B, ISIZE);
	
	//print
	
	system("pause");
	return 0;
}

void selectionSort(int A[], int ISIZE) //Ascending
{
	int startScan, minIndex, minValue;

	for (startScan = 0; startScan < (ISIZE - 1); startScan++)
	{	
	
		minIndex = startScan;
		minValue = A[startScan];

		for (int index = startScan + 1; index < ISIZE; index++)
		{
			for (int i = 0; i < ISIZE; i++) {
				cout << A[i] << ' ';
			}
			cout << endl;

			if (A[index] < minValue)
			{
			
				minValue = A[index];
				minIndex = index;
			}//if
		}//for index

		A[minIndex] = A[startScan];
		A[startScan] = minValue;
	}//for start scan

	
}//selectionsort

void bubbleSort(int B[], int ISIZE)
{
	int temp;
	bool swap;

	do
	{
		swap = false;

		for (int count = 0; count < (ISIZE - 1); count++)
		{
			if (B[count] > B[count++])
			{
				temp = B[count++];
				B[count] = B[count++];
				B[count++] = temp;
				swap = true;
			}//if
		}//forcount
	} while (swap);
}//bubble sort 
Last edited on
Line 21: The array needs to be mutable (a.k.a not const) since you're changing it in bubble sort.

Lines 73-79: Do not use count++. This increase the value of count each time. So, by the time you're through the for loop, count has increased by 3. Use count +1 instead.

Also, your ordering is off. Use
1
2
3
temp = B[count + 1];
B[count +1] = B[count];
B[count] = temp;
Ok I did as you said but I'm getting a warning of my selectionsort identifiers are not found and same with my bubble sort..
thank you so much for the reply by the way much appreciated
You need a forward declaration/prototype for your functions. Main is calling them before it knows they exist.
Add these before your main.
1
2
void selectionSort(int A[], int ISIZE);
void bubbleSort(int B[], int ISIZE);
Last edited on
ok so like this
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
//includers
#include <iostream>
#include <iomanip>

//using name space
using namespace std;

void selectionSort(int A[], int ISIZE);
void bubbleSort(int B[], int ISIZE);

int main()
{
	
	//variable in arrays
	const int ISIZE = 7;

	//Array A Selection sort
	int A[ISIZE] = { 88,75,71,31,55,98,89 };
	cout << "Selction Sort Functon" << endl;
	selectionSort(A, ISIZE);

	//Array B bubble sort
	int B[ISIZE] = { 88,75,71,31,55,98,89 };
	cout << "Bubble Sort Function" << endl;
	sort(B, ISIZE);
	
	//print
	
	system("pause");
	return 0;
}
ok I ran it through the visual studio and it still say build errors it displays just the cout statements in the main
That should work as long as you still have the definitions below main. Line 25 just says sort instead of bubbleSort.
ok I got it to run but its not doing the selective sorting and its not running through bubble sort. so no errors just isn't doing the sorting and running bubble
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
//includers
#include <iostream>
#include <iomanip>

//using name space
using namespace std;

void selectionSort(int A[], int ISIZE);
void sort(int B[], int ISIZE);

int main()
{
	
	//variable in arrays
	const int ISIZE = 7;

	//Array A Selection sort
	int A[ISIZE] = { 88,75,71,31,55,98,89 };
	cout << "Selction Sort Functon" << endl;
	selectionSort(A, ISIZE);

	//Array B bubble sort
	int B[ISIZE] = { 88,75,71,31,55,98,89 };
	cout << "Bubble Sort Function" << endl;
	sort(B, ISIZE);
	
	//print
	
	system("pause");
	return 0;
}

void selectionSort(int A[], int ISIZE) //Ascending
{
	int startScan, minIndex, minValue;

	for (startScan = 0; startScan < (ISIZE - 1); startScan++)
	{	
	
		minIndex = startScan;
		minValue = A[startScan];

		for (int index = startScan + 1; index < ISIZE; index++)
		{


			if (A[index] < minValue)
			{
			
				minValue = A[index];
				minIndex = index;
			}//if			
			for (int i = 0; i < ISIZE; i++) {
				cout << A[i] << ' ';
			}
			cout << endl;
		}//for index

		A[minIndex] = A[startScan];
		A[startScan] = minValue;
	}//for start scan

	
}//selectionsort

void sort(int B[], int ISIZE)
{
	int temp;
	bool swap;

	do
	{
		swap = false;

		for (int count = 0; count < (ISIZE - 1); count++)
		{
			if (B[count] > B[count + 1])
			{
				temp = B[count + 1];
				B[count + 1] = B[count];
				B[count] = temp;
				swap = true;
			}//if
		}//forcount
	} while (swap);
}//bubble sort 
Can you post your code again so I can see what happened?
Last edited on
hmm I cant seem to get it to work if I even touch it
I'm not exactly sure what you're trying to accomplish. If you want to check the values after they have been sorted, I suggest removing lines 53-56, and adding output statements after each of the sorts. Doing that I got the correct output.
I needed to make it so it displays the sorted arrays
I need to display each pass it does in the sorts
the out put I'm trying to accomplish needs to look like that
selective sort
4321
1324
1234
Okay. I got something similar when I moved
1
2
3
4
for (int i = 0; i < ISIZE; i++) {
   cout << A[i] << ' ';
}
cout << endl;

to just after line 60, and added something similar to just after line 82.
Topic archived. No new replies allowed.