0
The exercise I’m solving asks to write a program in C(pointer, dynamic and recursive allocation) that prints a given integer on a binary basis. My result is generating a very large number, now I don’t know if this picking up junk from memory or if my code is even wrong.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <locale.h>
long long int binario(int *n)
{
long long int *numeroBinario = 0;
int *resto;
int i = 1;
int passo = 1;
numeroBinario = (long long int*) malloc(sizeof(long long int));
resto = (int *) malloc(sizeof(int));
while(*n != 0)
{
*resto = *n % 2;
printf("Passo %d: %d/2, Resto = %d, Quociente = %d\n", passo++, *n,*resto, *n / 2);
*n = *n / 2;
*numeroBinario = (*numeroBinario) + (*resto) * i;
i = i * 10;
}
return *numeroBinario;
}
int main()
{
int *n;
int *temp;
setlocale(LC_ALL, "Portuguese");
n = (int *) malloc(sizeof(int));
temp = (int *) malloc(sizeof(int));
puts("Digite um número: ");
scanf("%d", n);
*temp = *n;
printf("O número %d na base binária é: %lld.\n", *temp, binario(n));
return 0;
}
I think I have already answered this in other questions (https://answall.com/a/324267/101, https://answall.com/q/216284/101 and https://answall.com/q/216128/101 among others), although not in exactly the same algorithm. I already say that this code is absurdly complicated and uses a lot of things unnecessarily. See how it can be simpler in links above. Understand that even the concept of what you are trying to do is wrong, and it will hurt your understanding even more if you insist on this way of doing it. Number is number and has no decimal or binary notation, text is something else.
– Maniero
Thank you for replying @Maniero, but that’s how I understood it and explained it to me at university and exercise lists are like that, because if it was to make a nonrecursive code it would be easier, I do not understand why the keystroke in making recursive codes being that the memory space that a recursive function consumes for "draft" can be great, I will check the links and try to understand better the concept, because it is still very confusing for me.
– user124286
If they told you to do this, the course is bad, but I’d rather believe that’s a misinterpretation of text. And if they told you to do something that complicated, then the course is really bad, but I don’t think they said to do it this way, it wouldn’t even make sense. Nobody’s talking about recursion here, I’m talking about not using pointers, not doing dynamic allocation, and mainly using text where text should be text and not inventing fashion trying to put what should be text in a number, it makes zero sense. Ñ exists binary number, there exists binary textual representation, as there is the decimal that is standard
– Maniero
The course is not bad, this was lack of my own interpretation and another I just mentioned recursion because it is what the exercise asks the question of using pointer and dynamic allocation was my own initiative, already found the solution in the links thank you.
– user124286
I don’t see why I should use a pointer, but it could just be a lack of context, dynamic allocation. You should only use it when you really need it stack. Even to learn, avoid, and only use to learn where you should, then learn right..
– Maniero