Swap positive void function

I need to create void function called "swapPositive" which takes two reference parameters and swaps the values in those parameters if they are both positive numbers.
Show how this function is called from a main() program.
This is what I have so far.

#include <iostream>
#include <stdio.h>
void Swap(int, int);

int main()
{
int a, b;
printf("\nPlease Enter the value of a and b\n");
scanf_s("%d %d", &a, &b);
printf("\nBefore Swap: a = %d and b = %d\n", a, b);

Swap(a, b);
return 0;
}

void Swap(int a, int b)
{
int Temp;

Temp = a;
a = b;
b = Temp;

printf("\nAfter Swapping: a = %d and b = %d\n", a, b);

system("PAUSE");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

void swap_if_positive( int& a, int& b ) // note: a and b are of type 'reference to int'
{
    if( a > 0 && b > 0 ) // if both are positive
    {
        // swap them
        const int temp = a ;
        a = b ;
        b = temp ;
    }
}

int main()
{
    int a, b ;
    std::cout << "\nPlease Enter the value of a and b\n" ;
    std::cin >> a >> b ;

    std::cout << "\nBefore Swap: a = " << a << " and b = " << b << '\n' ;

    swap_if_positive( a, b ) ; // pass references to a and b
    std::cout << "\nAfter Swap: a = " << a << " and b = " << b << '\n' ;
}
Topic archived. No new replies allowed.