Miscellaneous > Computers and Technology

New Hw, Language C :/

(1/1)

Ameer:
I need some serious help on this one if any of you lads knows some c language, i'll appreciate it
since i have to hand it over tomorrow :/

I've done the first part of the picture below


however i still need some serious help on the second part


what second part do is,
You enter a number ( for example 7 )
it prints the binary numbers from 0 to 7.
without using loops, everything has to be done using recursion.



my main should look something like this
#include <stdio.h>
int main ()
{
 $ /*defining variables*/
 printf("Please enter a string:\n");
 print_some_chars(*);
******* printf("Please enter an integer:\n");
 % /*get input*/
 print_binary_number(**);
******** This is the part that i need help with.
 return 0;
}


the amount of numbers to be shown is as big as the last binary number ( which is the one you entered )

for example if you enter 3
it prints :
00
01
10
11


however if you enter 255 for example,
it prints :
00000000
00000001
00000010
until
11111111

Thomy:
A lot of these problems have good solutions online and have already been solved by many people. It's no shame to just google the solution of a problem. I'd just convert the number to a string containing all bits, then print that with a loop or in your case just a function that prints char by char recursively: http://stackoverflow.com/questions/7911651/decimal-to-binary

Your recursion of the string printer could look like that: (untested, just typed that here in the forum)

--- Code: ---void print(char* str, int pos) {
    printf("%c", str[pos]);
    if (pos > 0)
        print(str, pos-1);
}
--- End code ---

This is also not a full solution, it just shows how it could be done.

Ameer:

--- Quote from: Thomy on May 17, 2017, 02:56:54 pm ---A lot of these problems have good solutions online and have already been solved by many people. It's no shame to just google the solution of a problem. I'd just convert the number to a string containing all bits, then print that with a loop or in your case just a function that prints char by char recursively: http://stackoverflow.com/questions/7911651/decimal-to-binary

Your recursion of the string printer could look like that: (untested, just typed that here in the forum)

--- Code: ---void print(char* str, int pos) {
    printf("%c", str[pos]);
    if (pos > 0)
        print(str, pos-1);
}
--- End code ---

This is also not a full solution, it just shows how it could be done.

--- End quote ---

I'll make sure to take a look at that site, thanks
I might have seen something similar however I forgot to mention, no arrays could be used as well

its more like a homework that its about 1 chapter of language c which is recursion, no loops, arrays can be used.

Thanks again for your time tho.

Navigation

[0] Message Index

There was an error while liking
Liking...
Go to full version