The world is currently online!


Welcome to Emps-World!

Register now to gain access to all of our forum features. Once registered and logged in, you will be able to create topics, post replies, send private messages, manage your profile, chat with other players in the shoutbox and much more. Once you sign in, this message will disappear.



Pages: 1
0 Members and 1 Guest are viewing this topic.

Offline Ameer

  • *
  • 488
  • Liked: 631 times
  • +0/-0
    • View Profile
New Hw, Language C :/
« on: May 17, 2017, 02:45:49 pm »
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



Emps-World Player Moderator Since July 18, 2015
Emps-World Game Moderator Since September 22, 2015
Emps-World Player Administrator Since  October 29, 2015
Emps-World Game Administrator since few years

Emps world player since the day I resigned, dunno when.

Offline Thomy

  • *
  • 3555
  • Liked: 3326 times
  • +5/-0
    • View Profile
Re: New Hw, Language C :/
« Reply #1 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: [Select]
void print(char* str, int pos) {
    printf("%c", str[pos]);
    if (pos > 0)
        print(str, pos-1);
}

This is also not a full solution, it just shows how it could be done.
« Last Edit: May 17, 2017, 03:03:28 pm by Thomy »
The following users liked this post: Ameer

Offline Ameer

  • *
  • 488
  • Liked: 631 times
  • +0/-0
    • View Profile
Re: New Hw, Language C :/
« Reply #2 on: May 17, 2017, 03:12:59 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: [Select]
void print(char* str, int pos) {
    printf("%c", str[pos]);
    if (pos > 0)
        print(str, pos-1);
}

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

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.



Emps-World Player Moderator Since July 18, 2015
Emps-World Game Moderator Since September 22, 2015
Emps-World Player Administrator Since  October 29, 2015
Emps-World Game Administrator since few years

Emps world player since the day I resigned, dunno when.
Pages: 1