简体   繁体   中英

Basic Value Swap function

I'm trying to design a piece of code that works like this. The user enters a 3 digit number, let's say they chose 653, they also input which numbers in that integer they wish to swap around. For example:

Enter a number and values you wish to swap: "653 2 3"

This then returns the following value:

635 is the new number. 

I am trying to do this in a function I called digit_swap. Im not really sure how I to approach this as I'm very new to coding and even newer to coding. I think I have to seperate the integer into the units, tens and hundred components and to do that I did the following:

third = (number % 10);
second = ((number % 100)/10);
first = ((number % 1000)/100);

The only thing is, would I use a bunch of if statements to determine the swapping of the numbers or would it be a loop. I really have no idea how to go about this. As for my code I have the following.

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int digit_swap(int number, int InputOne, int InputTwo) {

int first, second, third;

    if (number < 100) {
        cout << "Please enter a 3 digit integer\n";
        exit(0);
    }
    else if (number >= 1000) {
        cout << "Please enter a 3 digit integer\n";
        exit(0);
    }
    else {

        third = (number % 10);
        second = ((number % 100)/10);
        first = ((number % 1000)/100);

    }   
}

using namespace std;
int main() {
    int option_one, option_two;
    int number;
    cin >> number;
    cin >> option_one >> option_two;
    digit_swap(number, option_one, option_two);
    cout << "New number = " << number;

}

Even when I test to see if it working by adding a return first in the else segment of the if statement it returns nothing. Any help is appreciated, I'm not asking you to do the code for me either.

int digit_swap(int number, int InputOne, int InputTwo) {

    int first, second, third;

    if (number < 100) {
        // DO Something as you are doing 
    }
    else {
        third = (number % 10);
        number /= 10;
        second = (number % 10);
        number /= 10;
        first = (number % 10);
        number /= 10;
    }
    if(InputOne == 1) {
        if(InputTwo == 2) {
            number += second*100 + first*10 + third;
        }
        else if(InputTwo == 3) {
            number += third*100 + second*10 + first;
        }
        else{;}
    }
    else if(InputOne == 2) {
        if(InputTwo == 3) {
            number += first*100 + third*10 + second;
        }
    }
    else{;}
    return number;
}

I didn't test your code but I think there is an issue with the way you want to procede.

you want to modify "number" by passing it to your function

int digit_swap(int number, int InputOne, int InputTwo) {

int first, second, third;

if (number < 100) {
    cout << "Please enter a 3 digit integer\n";
    exit(0);
}
else if (number >= 1000) {
    cout << "Please enter a 3 digit integer\n";
    exit(0);
}
else {

    third = (number % 10);
    second = ((number % 100)/10);
    first = ((number % 1000)/100);

}   

}

if you want to modify a variable inside a function and the change can be see outside you will need to use pointer. If you are new to programming I suggest you to do something like this in your main code. The way function works, it will create copy of all your parameter, the change you made on them are not on the originals one.

int main() {
    int option_one, option_two;
    int number;
    cin >> number;
    cin >> option_one >> option_two;
    int result = digit_swap(number, option_one, option_two);
    cout << "New number = " << result;

}

you store in the new result variable the "return of your function"

First you either need to pass number by reference otherwise number in digit_swap is just a copy of number in main(). Your other option is to just call the function like this:

number = digit_swap(number, option_one, option_two);

or by reference

void digit_swap(int & number, int InputOne, int InputTwo);

To help you with swaping i would suggest an int array.

int arr[3];
arr[0] = number / 100;
arr[1] = number / 10;
arr[2] = number % 10;
int temp = arr[InputOne-1];
arr[InputOne-1] = arr[InputTwo-1];
arr[InputTwo-1] = temp;

I hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM