Searching Benchmarks program problems

Pages: 123
This is what I have so far.

I only got one error surprisngly, but I'm not even sure if I'm following the program instructions correctly or printing (or couting) the correct things (thus why there is a ??? in insertion sort).

https://s32.postimg.org/arwpuzb91/image.png


These are the instructions.

The error I have is this

http://prntscr.com/bx2u4u

I'm also having trouble getting my array to print horizontally and fitting all the words and numbers on the program screen for some reason. Never had that problem before.


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
 #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]<<"; ";


    cout << "What value would you like to search for?" << endl;

    cin >> value;

    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)
{
    int maxElement;
    int index;

    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]);
            }
        }
    }
}

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

    cout << "Number of location swaps: "<< temp << endl;
}

void selectionSort(int numbers[], int SIZE)
{
    int startScan;
    int index;
    int miniIndex;
    int miniValue;

    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]);
    }

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

    cout << "Number of location swaps: " << temp << endl;
}


void insertionSort(int numbers[], int SIZE)
{
    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--;
        }
        numbers[scan]=unsortedValue;
    }

    cout << "The number of location swaps is: ???" << endl;
}
Last edited on
Hi,

line 76 and 111 you have declared the swap function twice
Isn't it supposed to be that way? Can you explain how I fix it?

Or can you explain if I'm doing the program correctly so far?
Just remove one of the swap functions and you are good to go
Hm, the output isn't coming out correctly.

http://prntscr.com/bx3l2z

This is what it's supposed to look like.

http://prntscr.com/bx3lty
get rid of line 26-28 and try again
But they have to search for a value.

I have to
- create an array with 20 numbers, lets say the numbers 1 to 20.
- ask the user what value you should search for.
- search the value using sequential search and store the number of steps that were required in a variable.
- search the value using binary search and store the number of steps that were required in another variable.

- run the program a number of times (for example 21 times) searching for a different value (1 to 21) every time (this could be a for-loop if you want to be fast).
- calculate the average number of steps required to find a number and formulate your conclusions.

And it has to match the screenshow shown.

So I think I keep that line xD.

Line 32-33 you are doing that thing again
26
27
28
29
30
31
32
33
  cout << "What value would you like to search for?" << endl;

    cin >> value;

    do
    {
        cout << "Make sure to enter a value that's in the list." << endl;
        cin >> value;


see? so get rid of one of them
Oh wow. That helped a ton. I'm surprised I even did this much right lol. Nested for loops have been death for me.

Alright, so, three things.

1, the output isn't showing the full array or the word "What" and I have no idea why.

http://prntscr.com/bx3wnx

2. While I actually did the number of swaps right, it's not supposed to repeat.

It should simply look like

Original order:
26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51
Bubble Sorted:
5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93
Number of location swaps: 89

3. Speaking of the number of location swaps, it should be showing for each one.

For example, it should show each one.

Original order:
26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51
Bubble Sorted:
5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93
Number of location swaps: 89



Original order:
26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51
Selection Sorted:
5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93
Number of location swaps: 19



Original order:
26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51
Insertion Sorted:
5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93
Number of location swaps: 19

It should show the insertion, selection, and bubble.

The original order should be printed first, then the name of the swap, the selection sorted, and the number of location swaps.

Please show us your new code....
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
#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)
{
    int maxElement;
    int index;

    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]);
            }
        }
    }
}

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

    cout << "Number of location swaps: "<< temp << endl;
}

void selectionSort(int numbers[], int SIZE)
{
    int startScan;
    int index;
    int miniIndex;
    int miniValue;

    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]);
    }

}


void insertionSort(int numbers[], int SIZE)
{
    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--;
        }
        numbers[scan]=unsortedValue;
    }

    cout << "The number of location swaps is: ???" << endl;
}
Here's some tweak to your code

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
#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;
}


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--;
        }
        numbers[scan]=unsortedValue;
    }
cout<<"\nInsertion Sorted: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    cout <<"\nThe number of location swaps is: ???" << endl;
}
Today we are going to be searching for values.
These are the values you have to choose from
26; 45; 56; 12; 78; 74; 39; 22; 5; 90; 87; 32; 28; 11; 93; 62; 79; 53; 22; 51; Make sure to enter a value that's in the list.
5

Original order: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 
Bubble Sorted: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93 
Numbers of location swap: 89

Original order: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93 
Selection Sorted: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93 
Numbers of location swap: 19
Original order: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93 
Insertion Sorted: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93 
The number of location swaps is: ???
 
Exit code: 0 (normal program termination)


Hope it helps :)
Thank you so much!

Well, everything's basically done now haha.

I guess all I have now is

1)

Why are Selection Sorted and Insertion Sorted so close together? How can I fix this?

2) For some reason the output sill won't show the full array and the word "make".

3) Is the number of location swaps correct? I can't tell myself.
Why are Selection Sorted and Insertion Sorted so close together? How can I fix this?

Use a endl or \n

For some reason the output sill won't show the full array and the word "make".

That totally depends on your console, works fine on cpp.sh

Is the number of location swaps correct? I can't tell myself.

I don't see why they must be incorrect, read the code carefully and also try to understand the sorts in wikipedia so that you can manually calculate them if you want to :)
@FBHSIE
Is your problem solved now?
I keep putting the /n in the wrong place so it's giving me a weird output lol.

As for the words not fitting in the program?

I'll just tell my professor to run it on cpp.sh.

As for the location swaps? I'm not sure if they're correct yet.
I keep putting the /n in the wrong place so it's giving me a weird output lol.

its \n and not /n

As for the words not fitting in the program?

I'll just tell my professor to run it on cpp.sh.

great :)

As for the location swaps? I'm not sure if they're correct yet.

Like I said before, check them
1
2
3
4
5
cout<<"\nInsertion Sorted: ";
    for(int i=0;i<SIZE;i++)
    cout<<numbers[i]<<' ';
    cout <<"\nThe number of location swaps is: ???" << endl;
}


I'm a bit confused what to put in the ??? to display the number of swaps. I just forgot that's the whole reason I put it there in the first place haha.

Still didn't fix \n. : (

I think my swaps are right however!
Last edited on
@FBHSIE123
Why do you even keep going? You already have a solution.

Looks like you still have a lot of time for yourself :>
Because this program is separate from the other one I did. : )

The one you helped me with was a modification of this one.

For this one, I still need to figure out the "???" and the \n.
Pages: 123