Searching Benchmarks program problems

Pages: 123
change the statement in line 124
 
 cout<<"\nOriginal order: ";


this will resolve your \n problem, and for the ??? part your insertion sort doesn't use the swap function...
That's weird. We weren't taught how to add the swap function concerning the ??? part.

I only know how to do so for the first two functions.
The counter for insertion sort should be inside the while loop of line 136-140
1
2
3
4
5
6
7
8
while(scan>0&&numbers[scan-1]>unsortedValue)
        {
            numbers[scan]=numbers[scan-1];
            scan--;
            
        swap(numbers[miniIndex], numbers[startScan]);
        counter++;
        }


Like this?

Also, I fixed \n with endl; a bit ago sorry I didn't tell you. : )
if that works then yes!
http://prntscr.com/bx6x22

Nope it gave me these errors. u.u
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
void insertionSort(int numbers[], int SIZE)
{  cout<<"Original order: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';

    int unsortedValue;
    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--;

        swap(numbers[miniIndex], numbers[startScan]);
        counter++;
        }
        numbers[scan]=unsortedValue;
    }
cout<<"\nInsertion Sorted: " << endl;
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    cout <<"\nThe number of location swaps is:" <<<< endl;
}
Last edited on
@FBHSIE
What assignment are you trying to solve?
Don't waste other people's time. You have already got one for yourself.
The one you helped me with is the second one. This is the first one.

There is a sorting benchmarks one and a search benchmarks one.

This is the search benchmarks one. We solved the sorting benchmarks one just now.
Last edited on
As your errors state, you haven't declared miniIndex, startScan and counter
Closed account, just know I wasn't the one that reported your account this time. I'm not sure who is at this point.

Shadder, but am I even using the swap function correctly for the insertion sort or no?
no, thats somewhat incorrect, you don't necessary need to evoke the swap function
Alright, what should be done.
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 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;
}

This should work IMO
Thanks for the the code! Sorry I couldn't figure it out.

It says the number of swaps is 0 when I run it each time. What's wrong?



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
144
145
146
147
148
#include <iostream>

using namespace std;

const int SIZE=20;

void bubbleSort(int numbers[], int SIZE);
void selectionSort(int numbers[], int SIZE);
void insertionSort(int numbers[], int SIZE);



int main()
{
    int numbers[SIZE]= {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};
    int value=0;
    bool found;

    cout << "Today we are going to be searching for values." << endl;
    cout << "These are the values you have to choose from" << endl;

    for (int i=0; i<SIZE; i++)
        cout << numbers[i]<<"; ";

    do
    {
        cout << "Make sure to enter a value that's in the list." << endl;
        cin >> value;
        found=false;
        for (int i=0; i<SIZE; i++)
        {
            if (value==numbers[i])
            {
                found=true;
                break;
            }
        }
        if (!found)
            cout << "Enter a valid value !" << endl;
    }
    while (!found);

    bubbleSort(numbers, SIZE);
    selectionSort(numbers, SIZE);
   insertionSort(numbers, SIZE);





    return 0;
}

void bubbleSort (int numbers[], int SIZE)
{
    cout<<"\nOriginal order: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    int maxElement;
    int index,counter=0;

    for(maxElement=SIZE-1; maxElement>=0; maxElement--)
    {
        for(index=0;index<=maxElement-1;index++)
        {
            if(numbers[index]>numbers[index+1])
            {
                swap(numbers[index], numbers[index+1]);
                counter++;//increments counter everytime swap occurs
            }
        }
    }
cout<<"\nBubble Sorted: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    cout<<"\nNumbers of location swap: "<<counter<<endl;
}

int swap(int &a, int &b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
}

void selectionSort(int numbers[], int SIZE)
{  cout<<"\nOriginal order: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    int startScan;
    int index;
    int miniIndex;
    int miniValue;
    int counter=0;

    for(startScan=0;startScan<(SIZE-1);startScan++)
    {
        miniIndex=startScan;
        miniValue=numbers[startScan];

        for(index=startScan+1;index<SIZE;index++)
        {
            if(numbers[index]<miniValue)
            {
                miniValue=numbers[index];
                miniIndex=index;
            }

        }
        swap(numbers[miniIndex], numbers[startScan]);
        counter++;
    }
cout<<"\nSelection Sorted: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    cout<<"\nNumbers of location swap: "<<counter<<endl;
    cout << endl;
}




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;
}
Last edited on
Hi,
Try this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void insertionSort (int arr[], int SIZE)
  {
	 int j, temp, swap = 0;
	 cout<<"Original order: ";
    for(int i = 0; i < SIZE; i++)
    cout<< arr[i] << ' ';
	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 <<"\nThe number of location swaps is: "<< swap << endl;
		return;
 }
Last edited on
Does that help? :)
https://s32.postimg.org/5smrr2sr9/Screenshot_3.png

Got these two errors. Tried putting a } where it told me too and still got errors.

By the way system, the "I keep getting 0 swaps" was meant for this program. I got the two confused. Little note.

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>

using namespace std;

const int SIZE=20;

void bubbleSort(int numbers[], int SIZE);
void selectionSort(int numbers[], int SIZE);
void insertionSort(int numbers[], int SIZE);



int main()
{
    int numbers[SIZE]= {26, 45, 56, 12, 78, 74, 39, 22, 5, 90, 87, 32, 28, 11, 93, 62, 79, 53, 22, 51};
    int value=0;
    bool found;

    cout << "Today we are going to be searching for values." << endl;
    cout << "These are the values you have to choose from" << endl;

    for (int i=0; i<SIZE; i++)
        cout << numbers[i]<<"; ";

    do
    {
        cout << "Make sure to enter a value that's in the list." << endl;
        cin >> value;
        found=false;
        for (int i=0; i<SIZE; i++)
        {
            if (value==numbers[i])
            {
                found=true;
                break;
            }
        }
        if (!found)
            cout << "Enter a valid value !" << endl;
    }
    while (!found);

    bubbleSort(numbers, SIZE);
    selectionSort(numbers, SIZE);
   insertionSort(numbers, SIZE);





    return 0;
}

void bubbleSort (int numbers[], int SIZE)
{
    cout<<"\nOriginal order: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    int maxElement;
    int index,counter=0;

    for(maxElement=SIZE-1; maxElement>=0; maxElement--)
    {
        for(index=0;index<=maxElement-1;index++)
        {
            if(numbers[index]>numbers[index+1])
            {
                swap(numbers[index], numbers[index+1]);
                counter++;//increments counter everytime swap occurs
            }
        }
    }
cout<<"\nBubble Sorted: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    cout<<"\nNumbers of location swap: "<<counter<<endl;
}

int swap(int &a, int &b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
}

void selectionSort(int numbers[], int SIZE)
{  cout<<"\nOriginal order: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    int startScan;
    int index;
    int miniIndex;
    int miniValue;
    int counter=0;

    for(startScan=0;startScan<(SIZE-1);startScan++)
    {
        miniIndex=startScan;
        miniValue=numbers[startScan];

        for(index=startScan+1;index<SIZE;index++)
        {
            if(numbers[index]<miniValue)
            {
                miniValue=numbers[index];
                miniIndex=index;
            }

        }
        swap(numbers[miniIndex], numbers[startScan]);
        counter++;
    }
cout<<"\nSelection Sorted: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    cout<<"\nNumbers of location swap: "<<counter<<endl;
    cout << endl;
}




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;
    }
void insertionSort (int arr[], int SIZE)
  {
	 int j, temp, swap = 0;
	 cout<<"Original order: ";
    for(int i = 0; i < SIZE; i++)
    cout<< arr[i] << ' ';
	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 <<"\nThe number of location swaps is: "<< swap << endl;
		return;
 }


closed account (48T7M4Gy)
lines 124 and 144 indicate the dangers of cut and paste OP's work vs problem solving and learning something :)
Last edited on
That was a mistake on my part. It's not actually in my program.

I forgot to delete that part of my code when I put code tags around it.

Even the correct version gives me the same errors.

I don't have an extra insertionsort in my actual program.

I have a question Kermot, why don't you go around on all the programs here I've done myself without barely anyone's help and praise me?

Why do you always go on the ones I'm having extreme troubles with? I can point out several programs I've done on here where I didn't get much help, but you never want to comment on those.

Though, if we're being honest, nested for loops utterly confuse me. I've had to get a ton of help on every program involving nested for loops.
Last edited on
closed account (48T7M4Gy)
I think you protest too much FBHSIE. Instead of foisting the particular problem I provided you help with onto somebody else you have actually solved it yourself - no cause for you to complain there in my estimation. :)
Pages: 123