Please help me with this problem!

Yo man
Last edited on
closed account (o3hC5Di1)
Hi there,

You have a decent start.

After getting all the input, it's time to "sort" it. You will need to:

- Create another loop to go through all the elements
- For every element in the array, check whether it is equal to, larger or smaller than n
- Depending on this, move the elements around in the array. Note that to switch two elements, you will need to create a temporary:

1
2
3
4
5
6
void switch_elements(int a, int b, int array[])
{
    int tmp = array[a];
    array[a] = array[b];
    array[b] = tmp;
}


There's a little bit more to it than this, but it should get you on your way.
Please do let us know if you need any further help.

All the best,
NwN
Last edited on
You probably shouldn't put switch as the function name :P considering that is already a keyword.
closed account (o3hC5Di1)
Woops, well spotted! I was too fast - my apologies.

All the best,
NwN
So, I should create another loop that goes through all elements first? Well, how is that loop supposed to look like?

At the end im supposed to use bubblesort to sort the numbers, right? And am I supposed to use an if-statement that looks kinda like this? if (n>=seq[i].... etc?
Last edited on
...
Basically you are doing a bubble sort function. To call his function would basically be something 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
32
33
34
35
36
37
38
#include <iostream>

void switch_elements( int , int , int * );
void bubble_sort( int *, const int );

int main()
{
	const int SIZE = 20;
	int array[SIZE] = { 
	1 , 2 , 6 , 3 , 8 , 10 , -3 , 0 , 0 , 8 , 0 ,
	1 , 1 , -2 , -3 , -4 , -5 , -6 , -7 , -7 
	};
	
	bubble_sort( array , SIZE );
	for( const auto &it : array )
		std::cout << it << ' ';
}

void switch_elements( int lhs , int rhs , int *array )
{
	int temp = array[rhs];
	array[rhs] = array[lhs];
	array[lhs] = temp;
}

void bubble_sort( int *array , const int SIZE )
{
	for( int i = 0; i < SIZE; ++i )
	{
		for( int j = 0; j < SIZE; ++j )
		{
			if( array[j] > array[i] )
			{
				switch_elements( i , j , array );
			}
		}
	}
}


Actually I misread. You are correct you would pretty much just loop through the array and if it is greater than n then move it to the left of the n value otherwise put it to the right.
Yeah. The thing is i know what to do, but I dont know how to do it...
At the end, i should use bubblesort without creating a temp. My bubblesort should look something like this.



Keep in mind that this is just a random example.

const int SIZE = 6;
int seq[SIZE] = {10, 2, 7, 6, 0, 2};

for ( int pass = 1; pass < SIZE - 1; pass++ )
{
for (int i = 0; i < SIZE - 1; i++ )
{
if ( seq[ i ] > seq[ i + 1 ] )
//swap seq[i] and seq[i+1]
}
}






This is my code so far.


#include<iostream>
using namespace std;

int main()


{
const int SIZE = 20;
int seq[SIZE];
int n;
cout<<"Type 20 integers"<<endl;

for(int i = 0; i<SIZE; i++)
{

cin>>seq[i];


}


int howMany = 0;

cout << "Enter n: ";
cin >> n; //read first value

for(int i = 0; i<howMany; n++)

{
if (seq[i] <=n) continue;

cout<<seq[i]<<endl;
}


return 0;
}

How are my if statements supposed to look like?
...
Umm..first you should be a little more patient. Secondly you kind of need a temp variable when doing bubble sort. Think about this.
a = 5
b = 6

a = b //a = 6

How do you set b equal to 5 now?

You cant do a = b = a because that would mean b = a( b == 5 ) a = b ( a == 5 )

You would have to do
a = 5
b = 6
temp = b; //6
b = a; //5
a = temp; //6

As for your if statement it looks correct but your for loop I'm not sure what you are trying to do. why would you increment n?

Basically just loop and move elements left or right depending on greater or less than. Until it loops through all elements.

OK. Thanks man
Ok this is my code so far.

#include<iostream>
using namespace std;


int main()


{
const int SIZE = 20;
int seq[SIZE];
int n;
int howMany = 0;

cout<<"Type 20 integers"<<endl;

for(int i = 0; i<SIZE; i++)
{

cin>>seq[i];


}
cout << "Enter n: ";
cin >> n; //read first value



for(int i = 0; i<howMany; i++)

{
if (seq[i] <=n) continue;

cout<<seq[i]<<endl;
}


for(int i = 0; i<howMany; i++)

{



if (seq[i]>n) continue;
cout<<seq[i]<<endl;

}

return 0;
}

How should i finish this without using temp to sort the sequence?
Topic archived. No new replies allowed.