sportsdirect
Cenjen
- Učlanjen(a)
- 18.11.2013
- Poruke
- 36
- Poena
- 159
Imam sledeci zadatak:
//Generalize the to_binary() function of Listing 9.8 to a to_base_n() function that
//takes a second argument in the range 2–10. It then should print the number that
//is its first argument to the number base given by the second argument. For
//example, to_base_n(129,8) would display 201, the base-8 equivalent of 129.
//Test the function in a complete program.
Gde gresim kada kucam 129 i base broj 8 pa mi ispisuje 0201 umesto 201 i da li sam trebao drugacije da napisem tj da li sam jos nesto pogresio? Hvala unapred
Sad sam probao tj uvideo gresku ja mislim da sam trebao da stavim u base funkciji
if (first>=second)
sa ovom izmenom mi daje pravi rezultat
//Generalize the to_binary() function of Listing 9.8 to a to_base_n() function that
//takes a second argument in the range 2–10. It then should print the number that
//is its first argument to the number base given by the second argument. For
//example, to_base_n(129,8) would display 201, the base-8 equivalent of 129.
//Test the function in a complete program.
Gde gresim kada kucam 129 i base broj 8 pa mi ispisuje 0201 umesto 201 i da li sam trebao drugacije da napisem tj da li sam jos nesto pogresio? Hvala unapred
Kod:
#include <stdio.h>
void to_base(int first,int second);
int main(void)
{
int first_num,second_num;
printf("Enter an integer and base number from 2 to 10 (q to quit):\n");
while (scanf("%d %d", &first_num, &second_num) == 2)
{
printf("Binary equivalent: ");
to_base(first_num,second_num);
putchar('\n');
printf("Enter an integer (q to quit):\n");
}
printf("Done.\n");
return 0;
}
void to_base(int first,int second)
{
int r;
/* recursive function */
r = first % second;
if (first >= 1)
to_base (first/second,second);
printf ("%d",r);
return;
}
Sad sam probao tj uvideo gresku ja mislim da sam trebao da stavim u base funkciji
if (first>=second)
sa ovom izmenom mi daje pravi rezultat
Poslednja izmena: