sorting and swaping

closed account (zN3vqMoL)
I am learning c++ and have learned and understood just the VERY basics, but now I am in need of some help.
I compiler I use is microsoft visual studio 2012

I need some help with the following. I just cant seem to get a good starting off point.

Using the practice principles of Function Prototypes, Function Declaration, Function Definition, Function Calls, Value Parameters, Reference Parameters and Decision Statements, I need to
Create 3 functions (areSorted, mySwap and mySort) and test that they work by creating a demonstration program (as shown in the Output Layout section below).

The areSorted function should accept in the values of 2 integers and test if they are in ascending order.

The mySwap function should accept in and swap the values of 2 integer parameters.

The mySort function should accept in and sort the values of 2 integer parameters in ascending order.

The output layout being: Please enter a number (0 to 99): 73
Please enter a number (0 to 99): 42
Those 2 numbers are already in ascending order: 42 73
or
Those 2 numbers sorted in ascending order are: 42 73

I begin with
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
using namespace std;
int main(void)
{
int a;
int b;

cout<< "please enter a number (0-99): ";
cin>> a;

if(aresorted (a,b));


return EXIT_SUCCESS;

}


Not very far, but like I said I am stumped. Any help to lead me in the right direction would be appreciated.
Thank you!
just go through this code you have written so far and think about what each line does
well think about it logically first, don't think about the code.
for example: areSorted(a, b):
1. take in 2 integers (a and b)
2. if a is greater than, or equal to b, then return false.
3. otherwise return true.

that kinda thing.

For mySwap() you'll need to pass in by reference (as passing by copy won't retain the swap back in your main() function) and you'll need a temporary variable inside the function to keep one of the values while you swap.

mySort() will have the same kind of logic as areSorted(). And you'll have to pass by ref again.
Last edited on
closed account (zN3vqMoL)
With some help, this is what I have so far. There is a problem with mySwap(a, b); at the bottom however, as it says it is an undeclared identifier, and I am stumped at the output.

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>

using namespace std;
int main()
{
	int a;
	int b;

	cout << "Please enter a number (0-99): ";
		cin >> a;
		cout << "Please enter a number (0-99): ";
		cin >> b;

	bool areSorted(int a, int b);
	return a <= b;
	{

		void mySwap(int& a, int& b);
		int temp = a;
		a = b;
		b = temp;
	}
	void mySort(int& a, int&b);
	if (!areSorted(a, b))mySwap(a, b);                        //why does it say myswap is undefined?

	cout << "Those 2 numbers are already in ascending order: " << a << ',' << b << ;    //I know there is something wrong with this line, and 
                                                                                        //should a cin go below it?
	else
    cout << "those 2 numbers sorted in ascending order are: " //not sure what to put here?
        cin >>                                                      //or here?


	
        

	return EXIT_SUCCESS;
}
}
Last edited on
You cannot define functions within functions (unless we count lambdas.)

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
#include <iostream>

using namespace std;

bool areSorted(int, int);
void mySwap(int&, int&) ;

int main()
{
    int a, b ;
    cout << "Please enter a number (0-99): " ;
    cin >> a ;
    cout << "Please enter a number (0-99):" ;
    cin >> b ;

    if ( !areSorted(a,b) )
        mySwap(a,b) ;

    // ...

}

bool areSorted(int a, int b)
{
    return a <= b;
}

void mySwap(int& a, int& b)
{
    int temp = a ;
    a = b ;
    b = temp ;
}
Topic archived. No new replies allowed.