c - The output of a certain part of my for loop -
#include <stdio.h> int num, i, k, a[5]; int main() { a[0]=2; a[1]=11; a[2]=12; a[3]=16; a[4]=28; num=a[1+3] i=4; while(num>0){ a[i]=num%4; num=num/3; printf("%d ",num); i--; } printf("8\n"); for(k=0;k<5;k++){ printf("%c ",65+a[k]); } printf("\n); } the output of program is:
9 3 1 0 8
c b d b a
i understand how output first line rather confused 2nd part.
for(k=0;k<5;k++){ printf("%c ",65+a[k]); this bit here confused me loop first time understanding should go k=0 print %c comes 65+a[k] k 0 65+a[0]. earlier part of setting see a[0]=2 , 65+2 67 character "c". correct on output if follow same logic 2nd loop 65+a[k] k=1 65+a[1] , a[1] 11 , 65+11 76 equal character "k" that's wrong should character "b".
i feel line of code im missing something:
a[i]=num%4 but doesn't set number still confused.
any appreciated
a[i]=num%4 set number. how:
in loop:
while(num>0){ a[i]=num%4; num=num/3; printf("%d ",num); i--; } num varies in first line of output.
a[i]=num%4; actually sets values in array follows:
initially, i=4 , num=28. therefore,
a[i]=num%4; sets a[4] 28%4=0. therefore, last character a+0=a.
then i=3, , num=9. therefore,
a[i]=num%4; sets a[3] 9%4=1. therefore, second last character a+1=b.
then i=2, , num=3. therefore,
a[i]=num%4; sets a[2] 3%4=3. therefore, third last character a+3=d.
then i=1, , num=1. therefore,
a[i]=num%4; sets a[1] 1%4=1. therefore, fourth last character a+1=b.
then i=0, , num=0. therefore,
we not enter loop. a[0]=c, initial value.
hence get: c b d b a
Comments
Post a Comment