+ 1
How to put for loop iteration results into a variable in C?
#include <stdio.h> int binary(int num, int x); int main(){ int i = 13, n; n = binary(i, n); printf("%d", n); return 0; } int binary(num, x){ for (; num; num/=2) { x = num%2; } return(x); } The result should be "1011" and instead of this, only "1" is printed on the screen. Why? And how should I modify the code to make it print "1011"?
17 Answers
+ 3
what you need is an array
+ 5
as ✳AsterisK✳ said an array is the best solution because int or even long will not be able to store a big number in binary system
+ 3
Miika
i could've write it with pointers to save some memory.
but i wouldn't want to give a complex solution to a beginner which they wouldn't understand in the end.
and the code would get unnecessarily complex for a simple solution. which will serve no purpose in having it made in the first place, to help Alessandro Palazzolo on this post.
reason i allocated 1000000 memory of int is to be able to put big numbers in there.
not neccessarily mean that it's wasteful if you can cut off the zeroes yourself.
if you cared so much about premature optimizations on memory managements that gives little to no effect on the computer.
Then you're free to put your implementation here while knowing that nobody really cares about it in the first place.
+ 2
Alessandro Palazzolo
Converting a decimal (base 10) to other base usually uses string. This is because there is a possibility for a number represented in other bases (but base 10) where number of digits exceeds the basic type ranges (e.g. int).
+ 2
No problem, curiosity is 👍
But I'm afraid I didn't get what you mean by "store the total range of results of any loop" here. Can you pass me an example?
+ 2
Alessandro Palazzolo
that binary is just wrong,
first of all the binary of 13
is 1101 since it's 8+4+1 = 13
not 1011 where 8+2+1 = 11
the codes you were given on that previous post is just wrong and i have no idea why people haven't realized it yet.
you should check whether the codes given by somebody gives the correct results or not before saying that the solution works.
here is a solution i created that actually works, and doesn't need the use of arrays.
edit:
since some of you guys requested the binary program with arrays, i have added them for further reference.
https://code.sololearn.com/c48BJ9EF0k44/?ref=app
https://code.sololearn.com/cmvYUfe6X6n8/?ref=app
+ 2
Good observation lvl 1 crook 👍
Apology for being less observant.
+ 1
Is this question still in relation to your previous one? as I remember you have a valid solution there to work this out.
https://www.sololearn.com/Discuss/2035576/?ref=app
+ 1
Ipang yeah, it is, but the solution works only for a few numbers!
I would like to know if there's an universal solution to store a loop complete result (any type of result) into a variable. Thanks in advance!
+ 1
Ipang thanks! And...sorry if I ask but i'm a beginner...apart from this example, is there an usual way to store the total range of results of any loop in C into a variable?
+ 1
Ok! Thanks to both of you!!
+ 1
✳AsterisK✳ how could I store this example in an array?
+ 1
lvl 1 crook most might not read the code to correct the user because that's not what the user ask for, i don't even bother reading since i know what can help the user out, and for a beginner using an array will be cool, your first code you can just have x>1 in the for loop to remove the result/=10
+ 1
hmmmmm, Miika why do you say its wasteful???
+ 1
Miika i understand your point, but no one cares about memory management in small codes though which is not gonna be use for a project, am sure he just click on the zeros, just enough without minding how many they're
+ 1
Miika why do you do that, it fine to argue, that's how we learn and also grow, and i can't find anything rude, and your comments might even help the OP and others in the future
- 1
public class Program
{
public void binary(int n) {
if(n >0){
binary(n/2);
System.out.print(n % 2 + " ");
}
}
public static void main(String[] args) {
int n = 19;
Program p = new Program();
p.binary(n);
}
}