i've been given task to make a program that will print out numbers in reverse with visible 0s and negative numbers and i'm given these restrictions:
i can only use do-while i can only use one line of code to reverse the number and using division i can only use long as a variable i can't use if-else i can't use string
i already made it and it works but i broke some of the restrictions and they wont accept it.
this is exactly what i did, but when i enter negative numbers the calculation fucks up
Chase Roberts
Will basic work?
101 CLS PRINT " (Q)uit" PRINT " Your credit: "; credit PRINT: PRINT INPUT " (B)uy for 10 or (S)ell for 10"; bs$ bs$ = LCASE$(bs$) IF bs$ = "b" THEN GOTO 2 IF bs$ = "s" THEN GOTO 3 IF bs$ = "q" THEN END ELSE GOTO 101
2 credit = buy GOTO 101
3 credit = sell GOTO 101
This should work with the Apple iigs, commodore 64 etc
William Allen
Sounds like uni? I'm assuming it's not a company, they don't give this much of a fuck in the real world.
Jose Hall
Learning how to use declare functions? Good Lord I don't miss college.
I actually had to fucking think. Op you need to slap you professor with your dick because this bullshit creates buffer overflows.
Adrian Young
>i can only use one line of code >c/c++
You do understand how much you can put onto a single line of C code, don't you? Theoretically, the entire program can be one line.
Jayden Lewis
i already did, but the out is not "right" somehow.
Isaiah Reed
testVar=input ("Foo")|Print(number[::-1])
Here's the code in one line
Learn to do your own homework. This will create a memory hole. It's bad form and dirty code.
Jose Robinson
What is the required behaviour on negative numbers?
#include
int main() { long in; std::cout > in; while (in!=0) { std::cout
Aaron Gonzalez
This is python. I don't code in c all that often.
Robert Moore
If it's not "right", that means you didn't do your pointless homework.
Blake Collins
cin is string
Austin Evans
Holy shit I ran this and while it worked, it overheated my processor...
Op must be in college, only a Democrat professor would ask for hot garbage like this.
Isaac Price
cout and cin are string variables.
Noah Gray
if you entered -14120 it should output -02141
Asher Lewis
this is already quite close user thank you so much
Nathaniel Taylor
cin and cout are basic stream objects and when operating on the console the stream object is bound to operate on char (note: not char[], which would be a string in c)
Juan Watson
you're welcome
Jack Phillips
thank you for the explanation as well!
Zachary Diaz
fixed it: replace in /= 10; with in = abs(in / 10); and your are good to go
Jaxson Parker
If child appears in thread. >deepnude.exe
Jordan Garcia
what? :/
Bentley Hall
>abs what is this for?
Thomas Walker
why you asking us to do your first assignment of the semester?
Daniel Hill
As long as it works, you comment, and push all your commits.
Kayden Myers
to get the behaviour on negative numbers right
Aaron Lopez
oh this needs right?
Jaxon Long
shouldn't... works for me with gcc / MinGW... used g++ to compile, didn't use -lm to link math
Liam Moore
also didn't #include at all
Elijah Wright
oh okay, well thank you anyways!
Julian Fisher
C (GCC):
#include #include
int main() { long num;
scanf("%ld", &num);
do { printf("%ld", num % 10); num = labs(num / 10); } while (num);
printf("\n"); }
gcc num.c && chmod +x a.out && ./a.out (tried on macos with brew)
Chase Price
it won't print stuff like -00321
I'm not sure how to get around it without using if else. But since you didn't say you can't use the if else analogue... :)
#include #include
int main() { long num;
scanf("%ld", &num);
// print sign printf("%s", num < 0 ? "-" : "");
do { printf("%ld", num % 10); num = labs(num / 10); } while (num);
printf("\n"); }
William Edwards
ok this one is with any ifs and analogues looks great, doesn't it? it basically extracts the sign (which is just a bit set to either 1 or 0) then it multiplies it by '-', meaning that printf will only print either the sign or nothing still won't work if you enter numbers like -00123 (but I assume such numbers are invalid); will work for -12300
do { printf("%ld", num % 10); num /= 10; } while (num);
printf("\n"); }
Anthony Nguyen
>i can only use one line of code to reverse the number and using division not sure i understand that restriction. one line or one instruction? cause you dont nee dlines in C, that's not python.
Jeremiah Phillips
Don't do this faggots homework anyone.
Michael Clark
but anyway the basic idea is int inputnegative = input < 0; if (inputnegative) input = -input;
int x = 0; int cnt = 0; while (input > 0) { x *= 10; x += input % 10; input /= 10; ++cnt; }
the reason we have cnt is because if input was, say, 100 then your output would be 1, but you want to print 001. cnt - how many digits you actually have is how many 0 you need to output.
Jace Cox
btw it's possible to do shit like y = 1234 x = y / 1000 + // 1 y % 1000 / 100 * 10 + // 234 / 100 * 10 == 2 * 10 == 20 y % 100 / 10 * 100 + // 34 / 10 * 100 = 3 * 100 = 300 y % 10 * 1000 // 4 * 1000 = 4000 so you end up with 4321
but that depends on your initial length.
Adam Martinez
it's basically sum of y % 10^n / 10^(n-1) for n in 1..number-of-digits
Hudson Young
er, * 10^(number-of-digits - n), forgot about that