Please any help for this code ?

Dec 30, 2016 at 11:23pm
Hi i am john from france.I am a student in IT class, and still a beginner. Could anyone explain this code for me in simple words, and is there any errors in it ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27


#include <stdio.h>
#include <stdlib.h>
#define mx 200
#define b 2
int i=-1, j;
int T [MX],n,q,r;

main()

{
      printf ("un nombre entier: ");
      scanf ("%s", &n);
      do {
              q=n/b;
              r=n-(b*q);
              i++;
              T[i]:=r;
              n=q;
      }while (q>0);
      for (j=i; j>=0; j--) printf ("%d",T[j]);
      printf ("\n");
      system ("pause");
}


thanx : )
Last edited on Dec 30, 2016 at 11:36pm
Dec 31, 2016 at 12:56am
Hello John
in simple words, this code changes integer numbers from Decimal digit to Binary digit,
like this:
12>>1100
12 = (2^3+2^2+2^0+2^0)
position: 3 2 1 0
1 1 0 0
-----------------------------------------------------------
errors:
Line 08 T[mx] is Correct. it dose not need space
07,08 Declaring 'i','g','T[mx]' before main function make them Global.it means they are constant e.g. in line 18 'i' wouldn't change however we see it change. so both Declaration must be moved to the main function before printf.
10 int main() is Correct.
14 scanf("%d",&n) is Correct form because in line 13 U ask a Number(digit), NOT String(%s).
19 ':' is extra.
24 instead of system ("pause"); write return 0;

here is the right one: cpp.sh/76r5e
Last edited on Dec 31, 2016 at 2:12pm
Jan 1, 2017 at 3:42pm
okay, i understand it all now, thank you very much arad1995 : )
Topic archived. No new replies allowed.