Sorting and Swapping with Pointers

These are just the reference instructions for this project(question is after subscript) :
Create a program that creates a main function and two functions Sort and ReplaceIfGreater.

The Sort function should be a void function that has (takes) three parameters: two of the parameters should
be pointers to a double and the third should be a Boolean variable named ascending. The function should sort
the two values the pointers point to from lower to higher when the ascending parameter is true.
You may make ascending a default parameter set to true. If ascending is false then sort the values pointed
to in descending order from higher to lower.
For this function you must write your own Swap function.


In the main function ask the user for two doubles and then call Sort to put the doubles in ascending order
and output the variables and then call Sort again and put the doubles in descending order and output the
doubles again as shown in the sample input and output below.

Next create the ReplaceIfGreater function with the following function header: double ReplaceIfGreater(double* pDouble, double input).
This function should replace the value to which the pointer points to with the value of the input parameter if the input parameter is greater.
The function should return the old value the pointer to which pDouble pointed to if the value was changed or if there wasn't a change then return the input value.
The value returned will always be the lowest value and the value pointed to by the pointer will be the highest value.
In main, get two more doubles from the user and call ReplaceIfGreater with the user’s new inputs.
Then output the return value from the function and the value pointed to by pDouble as shown below.


Using any of the following will disqualify your build.
global variables
using the swap or sort library functions
cin/cout in any function other than main
goto, break (other than in a switch), or continue statements


I have the code built, no errors, however, it seems to be finicky with how it progresses through the code. I am close, I can taste it, but something is either wrong in my math or logic and I can't tell what.
Here is the code, and I believe the issues are or begin at lines: 35 and 58
in "Sort" It works exactly as it should IF the user's first number always is greater. It doesn't seem to do any evaluating. for some reason, it seems the if statement is concrete, instead of evaluating if they are greater or not.
Same issue in the "ReturnIfGreater" function. If it follows the logic, it works and gets the output I want, but the use may not always do that. What can I do to get those functions to actually evaluate the values the pointers are giving them?

I originally began this build not even knowing where to begin and wanted to come here, but now that I have worked through it and gotten to the "end" I might need extra eyes to see something I am missing. I don't know where to start looking for my errors as the compiler isnt giving me any.

Thanks again for any assistance, help or insight you may provide.

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

using namespace std;



void Sort(double* pNum1, double* pNum2, bool ascending);
double ReplaceIfGreater(double* pDouble, double input);

int main ()

{
	double num1,num2,num3,num4;
cout << "please enter two doubles : " << endl;
	cin >> num1;
	cin >> num2;
		Sort(&num1,&num2,true);
cout << " The doubles in ascending order are:" << num1 <<" ,  "<< num2<< endl;
		Sort(&num1,&num2,true);
cout << "The doubles in descending order are:" << num1 <<" ,  "<< num2<< endl;
cout << "Please enter two more doubles:" << endl; 
	cin >> num3;
	cin >> num4;
ReplaceIfGreater(&num3, num4);
cout << "The return value of ReplaceIfGreater is " << num3 <<" and the pointer value is: "<< num4 << endl;
	return 0;
}

void Sort(double* pNum1, double* pNum2, bool ascending)
{
	
	if(pNum1>pNum2)	
	{	
	double temp;    
    temp = *pNum1;    
    *pNum1 = *pNum2;
    *pNum2 = temp;
	}
	else	
	{
	return;

	}




}





double ReplaceIfGreater(double* pDouble, double input)
{
	double* pInput= &input;
	if(pInput<pDouble)
	{
	double temp;    
    temp = *pInput;    
    *pInput = *pDouble;
    *pDouble = temp;
   return temp;

	}
	else
	{
	return input;
	}
}
Last edited on
bumping in hopes of finding help

changed some things in the past 24 hrs.

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

using namespace std;



void Sort(double* pNum1, double* pNum2, bool ascending);
double ReplaceIfGreater(double* pDouble, double input);

int main ()

{
	double num1,num2,num3,num4;
cout << "please enter two doubles : " << endl;
	cin >> num1;
	cin >> num2;
		Sort(&num1,&num2,true);
cout << " The doubles in ascending order are:" << num1 <<" ,  "<< num2<< endl;
		Sort(&num1,&num2,true);
cout << "The doubles in descending order are:" << num1 <<" ,  "<< num2<< endl;
cout << "Please enter two more doubles:" << endl; 
	cin >> num3;
	cin >> num4;
ReplaceIfGreater(&num3, num4);
cout << "The return value of ReplaceIfGreater is " << num3 <<" and the pointer value is: "<< num4 << endl;
	return 0;
}

void Sort(double* pNum1, double* pNum2, bool ascending)
{
	
	if(true)	
	{	
	(pNum1< pNum2);
	double temp;    
    temp = *pNum1;    
    *pNum1 = *pNum2;
    *pNum2 = temp;
	}
	else if (false)	
	{

	return;

	}


	return;

}





double ReplaceIfGreater(double* pDouble, double input)
{
	double* pInput= &input;
	if(pInput<pDouble)
	{
	double temp;    
    temp = *pInput;    
    *pInput = *pDouble;
    *pDouble = temp;
   return temp;

	}
	else
	{
	return input;
	}
}
Last edited on
bumping again...:\

Have continued to try and clean up this problem. still not sure why i dont get the correct outputs

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

using namespace std;



void Sort(double* pNum1, double* pNum2, bool ascending);
double ReplaceIfGreater(double* pDouble, double input);
void Swap (double  &a, double & b);
int main ()

{
	double num1,num2,num3,num4;
cout << "please enter two doubles : " << endl;
	cin >> num1;
	cin >> num2;
		Sort(&num1,&num2,true);
cout << " The doubles in ascending order are:" << num1 <<" ,  "<< num2<< endl;
		Sort(&num1,&num2,false);
cout << "The doubles in descending order are:" << num1 <<" ,  "<< num2<< endl;
cout << "Please enter two more doubles:" << endl; 
	cin >> num3;
	cin >> num4;
double value=ReplaceIfGreater(&num3, num4);
cout << "The return value of ReplaceIfGreater is " << value <<" and the pointer value is: "<< num4 << endl;
	return 0;
}

void Sort(double* pNum1, double* pNum2, bool ascending)
{
	double number1=*pNum1;
	double number2=*pNum2;

	if(ascending)	
	{	
	(number1 > number2);
	Swap(number1, number2);
	ascending=true;
	}
	else
	{
	(number1 > number2);
	Swap(number1, number2);
	ascending=false;
	}
	return;

}


double ReplaceIfGreater(double* pDouble, double input)
{

	double FirstDouble= *pDouble;


	if(FirstDouble>input)
	{
	Swap (FirstDouble, input);
	return FirstDouble;

	}
	else
	{
	return input;
	}
}
void Swap (double & a, double & b)

{

double x;
x = a;
a = b;
b = x;

}

Topic archived. No new replies allowed.