Modification program

@shadder


It worked! Well, kinda. Got rid of 10 errors. : )
http://prntscr.com/bxdwcx


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
//Ashton Dreiling
//Sorting Benchmarks exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

//module prototypes
int binarySearch(int t, int arr[], int n);
int bubbleSort(int num[], int n);
void printArray(int arr[], int SIZE);


//global constants
const int SIZE=20;
const int NUM_FOR_CAL1=1;
const int NUM_FOR_CAL2=2;

int main()
{
    //some variables
   int swap;
   int arr[SIZE] = {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};

   //Showing original order, bubble sort, and the number of swaps
   cout << "Original Order : ";
   printArray(arr, SIZE, cout);
   swap = bubbleSort(arr, SIZE);
   cout << "Bubble Sorted : ";
   printArray(arr, SIZE, cout);
   cout << "Number of location swaps: " << swap << endl;
   int num, pos, total=NUM_FOR_CAL2;
   char YesNo;
   do
   {
      cout << "Select a number in the Array to search for: ";
      cin >> num;
      pos = binarySearch(num, arr, SIZE);
      cout << "Sequential Search comparisons: " << pos + NUM_FOR_CAL1<< endl;
      cout << "The position of the number is " << pos + NUM_FOR_CAL1 << endl;
      if(pos != -NUM_FOR_CAL1) total++;
      cout << "Binary Search comparisons: " << total << endl;
      cout << "The position of the number is " << pos + NUM_FOR_CAL1 << endl;
      cout << "Do you want to search again (Y = Yes) : ";
      YesNo = NUM_FOR_CAL2;
      cin >> YesNo;
   }//end of do while loop to search for array and display comparisons
    while(YesNo == 'Y' || YesNo == 'y');

   system("Pause");
   return 0;
}//end main

//searching array using binarySearch
int binarySearch(int t, int arr[], int n)

{
   for(int i = NUM_FOR_CAL2; i < n; ++i)
     if(arr[i] == t) return i;
   return -NUM_FOR_CAL1;
}//end of binarySearch

//searching array using bubbleSort
int bubbleSort(int num[], int n)
{
int i, j, flag = NUM_FOR_CAL1;
int temp, swap = NUM_FOR_CAL2;

for(i = NUM_FOR_CAL1; (i <= n) && flag; i++)
{
    flag = NUM_FOR_CAL2;
    for (j = NUM_FOR_CAL2; j < (n-NUM_FOR_CAL1); j++)
    {
        if (num[j+NUM_FOR_CAL1] < num[j])
        {
            temp = num[j];
            num[j] = num[j+NUM_FOR_CAL1];
            num[j+NUM_FOR_CAL1] = temp;
            flag = NUM_FOR_CAL1;
            swap++;
        }//end of if statement
    }//end of for loop
}//end of for loop
return swap;
}//end bubbleSort

void insertionSort(int numbers[], int SIZE)
{  cout<<"Original order: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';

    int unsortedValue,counter=0;
    int scan;

    for(int index=1;index<SIZE;index++)
    {
        unsortedValue=numbers[index];
        scan=index;

        while(scan>0&&numbers[scan-1]>unsortedValue)
        {
            numbers[scan]=numbers[scan-1];
            scan--;counter++;
        }
        numbers[scan]=unsortedValue;
    }
cout<<"\nInsertion Sorted: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    cout <<"\nThe number of location swaps is: "<<counter << endl;
}
First: Do not pass cout (line 27/30).
Second: Where is printArray(...) defined?
http://prntscr.com/bxet8y

Thanks coder! I fixed my issues.

Is the output correct?

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
//Ashton Dreiling
//Sorting Benchmarks exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

//module prototypes
int binarySearch(int t, int arr[], int n);
int bubbleSort(int num[], int n);
void printArray(int arr[], int SIZE);


//global constants
const int SIZE=20;
const int NUM_FOR_CAL1=1;
const int NUM_FOR_CAL2=2;

int main()
{
    //some variables
   int swap;
   int arr[SIZE] = {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};

   //Showing original order, bubble sort, and the number of swaps
   cout << "Original Order : ";
   printArray(arr, SIZE);
   swap = bubbleSort(arr, SIZE);
   cout << "Bubble Sorted : ";
   printArray(arr, SIZE);
   cout << "Number of location swaps: " << swap << endl;
   int num, pos, total=NUM_FOR_CAL2;
   char YesNo;
   do
   {
      cout << "Select a number in the Array to search for: ";
      cin >> num;
      pos = binarySearch(num, arr, SIZE);
      cout << "Sequential Search comparisons: " << pos + NUM_FOR_CAL1<< endl;
      cout << "The position of the number is " << pos + NUM_FOR_CAL1 << endl;
      if(pos != -NUM_FOR_CAL1) total++;
      cout << "Binary Search comparisons: " << total << endl;
      cout << "The position of the number is " << pos + NUM_FOR_CAL1 << endl;
      cout << "Do you want to search again (Y = Yes) : ";
      YesNo = NUM_FOR_CAL2;
      cin >> YesNo;
   }//end of do while loop to search for array and display comparisons
    while(YesNo == 'Y' || YesNo == 'y');

   system("Pause");
   return 0;
}//end main

//searching array using binarySearch
int binarySearch(int t, int arr[], int n)

{
   for(int i = NUM_FOR_CAL2; i < n; ++i)
     if(arr[i] == t) return i;
   return -NUM_FOR_CAL1;
}//end of binarySearch

//searching array using bubbleSort
int bubbleSort(int num[], int n)
{
int i, j, flag = NUM_FOR_CAL1;
int temp, swap = NUM_FOR_CAL2;

for(i = NUM_FOR_CAL1; (i <= n) && flag; i++)
{
    flag = NUM_FOR_CAL2;
    for (j = NUM_FOR_CAL2; j < (n-NUM_FOR_CAL1); j++)
    {
        if (num[j+NUM_FOR_CAL1] < num[j])
        {
            temp = num[j];
            num[j] = num[j+NUM_FOR_CAL1];
            num[j+NUM_FOR_CAL1] = temp;
            flag = NUM_FOR_CAL1;
            swap++;
        }//end of if statement
    }//end of for loop
}//end of for loop
return swap;
}//end bubbleSort

void insertionSort(int numbers[], int SIZE)
{  cout<<"Original order: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';

    int unsortedValue,counter=0;
    int scan;

    for(int index=1;index<SIZE;index++)
    {
        unsortedValue=numbers[index];
        scan=index;

        while(scan>0&&numbers[scan-1]>unsortedValue)
        {
            numbers[scan]=numbers[scan-1];
            scan--;counter++;
        }
        numbers[scan]=unsortedValue;
    }
cout<<"\nInsertion Sorted: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    cout <<"\nThe number of location swaps is: "<<counter << endl;
}

void printArray(int arr[], int SIZE)
{

   for(int i = 0; i < SIZE; i++)
   {
      cout << arr[i];
      if(i < SIZE - 1) cout << ", ";
   }
   cout << endl;
}

Last edited on
Is the output correct?
No, you missed the first two elements:

Original Order : 26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51
Bubble Sorted : 26, 45, 5, 11, 12, 22, 22, 28, 32, 39, 51, 53, 56, 62, 74, 78, 79, 87, 90, 93


What's the meaning of NUM_FOR_CAL.?
Oh, we can't have any unnamed numeric constants outside of the initialization in a for loop or initializing a list in an array.

You said I missed the first two elements? How do I fix it?
> For this one, it still says 0 swaps.
What function does it still say 0 swaps?
Is it the InsertSort() function?
You need to add the following constant :
const int ZERO_FOR_CAL = 0;

And this line should be :
int num, pos, total = ZERO_FOR_CAL;
Yes. It's the InsertSort function.
What line are you referring to?
Hi,
Try 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
void insertionSort (int arr[], int SIZE)
  {
	 int j, temp, swap = 0;
	 cout<<"Original order: ";
    for(int i = 0; i < SIZE; i++)
    cout<< arr[i] << ' ';
    cout << endl;
	for (int i = 0; i < SIZE; i++){
		j = i;
		
		while (j > 0 && arr[j] < arr[j-1]){
			  temp = arr[j];
			  arr[j] = arr[j-1];
			  arr[j-1] = temp;
			  j--; swap++;
			  }
		}
		cout<<"\nInsertion Sorted: ";
 		for(int i=0;i<SIZE;i++)
 		cout << arr[i] << ' ';
                cout << endl;

		cout <<"\nThe number of location swaps is: "<< swap << endl;
		return;
 }
Last edited on
> What line are you referring to?
int num, pos, total = ZERO_FOR_CAL;

It is the line 32.
Does that help? :)
Yes. I think I fixed everything. And I think my output is correct.

https://s32.postimg.org/l6m3yiwsl/Screenshot_2.png

You can point out if its not. Thanks!
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
//Ashton Dreiling
//Sorting Benchmarks exercise
#include <iostream>
#include <stdlib.h>

using namespace std;

//module prototypes
int binarySearch(int t, int arr[], int n);
int bubbleSort(int num[], int n);
void printArray(int arr[], int SIZE);


//global constants
const int SIZE=20;
const int NUM_FOR_CAL1=1;
const int NUM_FOR_CAL2=2;
const int ZERO_FOR_CAL=0;

int main()
{
    //some variables
   int swap;
   int arr[SIZE] = {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};

   //Showing original order, bubble sort, and the number of swaps
   cout << "Original Order : ";
   printArray(arr, SIZE);
   swap = bubbleSort(arr, SIZE);
   cout << "Bubble Sorted : ";
   printArray(arr, SIZE);
   cout << "Number of location swaps: " << swap << endl;
   int num, pos, total = ZERO_FOR_CAL;
   char YesNo;
   do
   {
      cout << "Select a number in the Array to search for: ";
      cin >> num;
      pos = binarySearch(num, arr, SIZE);
      cout << "Sequential Search comparisons: " << pos + NUM_FOR_CAL1<< endl;
      cout << "The position of the number is " << pos + NUM_FOR_CAL1 << endl;
      if(pos != -NUM_FOR_CAL1) total++;
      cout << "Binary Search comparisons: " << total << endl;
      cout << "The position of the number is " << pos + NUM_FOR_CAL1 << endl;
      cout << "Do you want to search again (Y = Yes) : ";
      YesNo = NUM_FOR_CAL2;
      cin >> YesNo;
   }//end of do while loop to search for array and display comparisons
    while(YesNo == 'Y' || YesNo == 'y');

   system("Pause");
   return 0;
}//end main

//searching array using binarySearch
int binarySearch(int t, int arr[], int n)

{
   for(int i = NUM_FOR_CAL2; i < n; ++i)
     if(arr[i] == t) return i;
   return -NUM_FOR_CAL1;
}//end of binarySearch

//searching array using bubbleSort
int bubbleSort(int num[], int n)
{
int i, j, flag = NUM_FOR_CAL1;
int temp, swap = NUM_FOR_CAL2;

for(i = NUM_FOR_CAL1; (i <= n) && flag; i++)
{
    flag = NUM_FOR_CAL2;
    for (j = NUM_FOR_CAL2; j < (n-NUM_FOR_CAL1); j++)
    {
        if (num[j+NUM_FOR_CAL1] < num[j])
        {
            temp = num[j];
            num[j] = num[j+NUM_FOR_CAL1];
            num[j+NUM_FOR_CAL1] = temp;
            flag = NUM_FOR_CAL1;
            swap++;
        }//end of if statement
    }//end of for loop
}//end of for loop
return swap;
}//end bubbleSort

void insertionSort(int numbers[], int SIZE)
{  cout<<"Original order: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';

    int unsortedValue,counter=0;
    int scan;

    for(int index=1;index<SIZE;index++)
    {
        unsortedValue=numbers[index];
        scan=index;

        while(scan>0&&numbers[scan-1]>unsortedValue)
        {
            numbers[scan]=numbers[scan-1];
            scan--;counter++;
        }
        numbers[scan]=unsortedValue;
    }
cout<<"\nInsertion Sorted: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    cout <<"\nThe number of location swaps is: "<<counter << endl;
}

void printArray(int arr[], int SIZE)
{

   for(int i = 0; i < SIZE; i++)
   {
      cout << arr[i];
      if(i < SIZE - 1) cout << ", ";
   }
   cout << endl;
}

Glad it helped :)
I think my output is correct.
Nope, it's still the same incorrect output for your bubbleSort(...). The problem is

1
2
NUM_FOR_CAL1
NUM_FOR_CAL2


As you use it they don't make sense. Just get rid of them.

flag is bool. It just shows that no more swaps have to be done.
swap starts with 0 (like all other that uses wrongfully NUM_FOR_CAL... now)

Further more: Your binarySearch(...) is not binary search. It's a linear search.

In the function binarySearch() :
for(int i = NUM_FOR_CAL2; i < n; ++i)

Should be :
for(int i = ZERO_FOR_CAL; i < n; ++i)
Topic archived. No new replies allowed.