Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

rotate number function (help me please)

justme

Coder
hello everyone ..
i would really appriciate it if someone would help me with this question..
i upload here the code i wrote, its not really solving anything (i get an really odd outputs),but i would like that someone will explain to me what am i doing wrong ( even if its probably everything 🙁 )..
so the question is to write an efficiant function that turn the digites from right to left (the user choose the amount of spins he would want to do on a function )
C:
#include<stdio.h>
#include<math.h>
int rotateNumber(int number, int spins)
{
    int reminder, answer = 0, num =0;
    for (int i = spins;i >0; i--)
    {
            reminder = number % 10;
            answer = reminder * (10 ^ (num-1)) + answer;
            num--;
    }
    return answer;
}

int numberLength(int num)
{
    int number = 0, spins = 0;
    while (number != 0)
    {
        {
            num = number / 10;
            num++;
        }
    }
    if (spins % num == 0)
    {
        printf("%d", number);
    }
    return number;
}
inputIntNonNegative()
{
    int number = 0,spins=0;
    scanf_s("%d", &number);
    while (number < 0)
    {
        printf("Enter a positive number: ");
        scanf_s("%d", &number);
    }
    return number;
    return spins;
}
void main()
{
    int number = 0, spins = 0, answer = 0;
    printf("\nplease enter the number: ");
    number = inputIntNonNegative();
    printf("please enter the amount of spins: ");
    spins = inputIntNonNegative();
    answer = rotateNumber(number, spins);
    printf("The number %d after %d spins is: %d\n", number, spins, answer);
}
 
I don't understand the assignment as it's written:
write an efficiant function that turn the digites from right to left (the user choose the amount of spins he would want to do on a function )

I assume "turn the digites from right to left" means to reverse them ? Then where do "spins" come in, and what are these anyway ?
As for your code, I like that it's neat and tidy, and it looks very plausible even though there's plenty wrong with it. I'll point that out but
first I'd like to correctly understand what they want. You might also want to show us these "really odd outputs".
 
I don't understand the assignment as it's written:


I assume "turn the digites from right to left" means to reverse them ? Then where do "spins" come in, and what are these anyway ?
As for your code, I like that it's neat and tidy, and it looks very plausible even though there's plenty wrong with it. I'll point that out but
first I'd like to correctly understand what they want. You might also want to show us these "really odd outputs".
Yes to reverse them .. (sorry my english aint so good 😅) .
And the professor called the reverse action as "spin", so the user can choose a number and the amount of reverses he want to do to the number .
Ill try to be more understandable , the question is to write a code that rotate a positive number with a function called "rotateNumber" that reverse each digit of the number as the amount of "spins" the user choose for example 2 spins in 1234 the output would be 4312 .
And the function lengthNumber caculate the amount of the digits of a number.
And the function need to be efficient so if the user choose to spin 1234 4000 it will know that he will get the same number without doing this amount of spins .
 
Thanks for the explanations. It could be just me, but I still don't get the concept of "spin". If the professor calls the action of reversing the digits a "spin", then why should the number of spins be a parameter at all ? In my current understanding, an even number of spins returns the original number, an odd number of spins returns the reversed number. So I don't see how two spins on 1234 would result in 4312 ? Please explain that in more detail.
In your 'efficiency' example, do you mean doing 4000 spins on 1234 ? Sure, that can be really efficient.... just do nothing at all. Surely nobody could be so stupid to call the reverse function 4000 times in a row ? Or even more than once ?
But, I am probably still missing something here...
 
Thanks for the explanations. It could be just me, but I still don't get the concept of "spin". If the professor calls the action of reversing the digits a "spin", then why should the number of spins be a parameter at all ? In my current understanding, an even number of spins returns the original number, an odd number of spins returns the reversed number. So I don't see how two spins on 1234 would result in 4312 ? Please explain that in more detail.
In your 'efficiency' example, do you mean doing 4000 spins on 1234 ? Sure, that can be really efficient.... just do nothing at all. Surely nobody could be so stupid to call the reverse function 4000 times in a row ? Or even more than once ?
But, I am probably still missing something here...
1 "spin" is reversing one digit like 123 will turn to 312 ( the unity digit "3" turned into the hundreds unit ) .
And obviously its kinda stupid to do 4000 spins for a number of 4 digits , but i guess he just want that ill define a code that will also know when the amount of reverses will get me the original number ,and at the beggining wont do all those reverses .
I hope you understood what i said..😅😅
 
1 "spin" is reversing one digit like 123 will turn to 312
So, as per the above example, is one "spin" the action of putting the last digit in front ? If so, N spins on a number of N digits will return the original number again. Also, it's not possible to reverse the digits this way. So I still think there's more to this magical "spin" than is mentioned here. And you still have not explained how two spins in 1234 gives 4312.
Damn, I must be getting old, understanding things is so hard sometimes 😮
 
I think what is probably meant is that when doing a number of spins in a loop, the 1st spin puts the last char in position 1, 2nd spin puts the (new) last char in position 2, and so forth. That way you reverse a number of N digits with N-1 spins, and then the next N-1 spins return the original number again.
 
So, as per the above example, is one "spin" the action of putting the last digit in front ? If so, N spins on a number of N digits will return the original number again. Also, it's not possible to reverse the digits this way. So I still think there's more to this magical "spin" than is mentioned here. And you still have not explained how two spins in 1234 gives 4312.
Damn, I must be getting old, understanding things is so hard sometimes 😮
Yes exactly.
aw thats my mistake 2 spins will get you 3412 sorry.. 🙄
well i guess its probably me not explaining so well haha..
so how do i write it as a code cus i couldnt do it as you see..?? ..🙁
 
aw thats my mistake 2 spins will get you 3412 sorry.. 🙄
Sloppy....
So apparently my last conjecture was wrong, and a "spin" is indeed just moving the last digit to the front ?
Then you'll get
1234
4123
3412
2341
1234
and after 4 spins we're back to start. But you'll never reverse the digits that way ! They have been in the same order all the time.
I think you'll need to get back to your professor because it's not clear what the assignment is. Or is it ? Perhaps you are not supposed to put them in reverse order as I assumed at first (although you confirmed that...) ? "Rotate a number", as you put it in the title, could mean just cycle the digits around like shown above. I will only help you with your code once were are totally clear on that. Makes sense ?
 
Sloppy....
So apparently my last conjecture was wrong, and a "spin" is indeed just moving the last digit to the front ?
Then you'll get

and after 4 spins we're back to start. But you'll never reverse the digits that way ! They have been in the same order all the time.
I think you'll need to get back to your professor because it's not clear what the assignment is. Or is it ? Perhaps you are not supposed to put them in reverse order as I assumed at first (although you confirmed that...) ? "Rotate a number", as you put it in the title, could mean just cycle the digits around like shown above. I will only help you with your code once were are totally clear on that. Makes sense ?
Yesss just like you said the function is moving each digit to the front every loop ..
And yea its kinda silly to do it after the amount of spins is bigger than the amount of dight becuase you are coming back to the original number , so maybe you can to also program that when spins>digits the amount of spins ill do will start number%spins ?
Ah i really need to take some english lessons if im still trying to explain to you the assignment haha ..
And i am really appreciate your tolerancea and your willing to help me 🙂
 
It's not really your English (which is well enough) which frustrates my understanding. It's the terminology. Rotate, reverse, spin.... my head is spinning.
So far as I understand it now, we can formulate the assignment like this:

Write a function that will do a specified number of "spins" on a specified number.
A "spin" is defined as the action of moving the rightmost digit of the number to the front.
Given that this process is cyclic (eventually results in the original number again), make sure you do not do any unnecessary spins.

If you can confirm without any doubt that this is the requirement, we can get to work on it and correct your code. And then we have to hope that this is indeed what the professor wanted. In the unlucky case that it isn't, I will not spend any more time on it. Fair enough ?
 
It's not really your English (which is well enough) which frustrates my understanding. It's the terminology. Rotate, reverse, spin.... my head is spinning.
So far as I understand it now, we can formulate the assignment like this:



If you can confirm without any doubt that this is the requirement, we can get to work on it and correct your code. And then we have to hope that this is indeed what the professor wanted. In the unlucky case that it isn't, I will not spend any more time on it. Fair enough ?
Eventually i got help from the uni , so thank you so much anyway 🙂
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom