HEllo,
i just encountered a promblem.My code is doing all right but is printing an number(235689)…all by its own.
can u help me figure where is the error?
#include<stdio.h>
void main()
{ int ar[100];
int i,t=0;
while(ar[t]!=42)
{
scanf("%d",&ar[++t]);
}
for(i=0;i<t;i++)
{
printf("%d",ar[i]);
printf("\n");
}
} Here is the question
Brother, you should indent your code properly & the hyperlink for the problem is incorrect. Anyways, let me try to help you out.
Explanation for each line :-
#include<stdio.h>
void main()
{
int ar[100];/*initializing array containing elements in cells 0-100 which means 101 elements*/
int i,t=0;/*initializing counter i & position t of array 'ar' as 0*/
while(ar[t]!=42)/*loop will run only if the 1st element isn't 42(0th position)*/
{
scanf("%d",&ar[++t]);/*1st position goes empty as it's pre-incremented*/
}
/*You need to know the mechanism of 'for' loop properly*/
for(i=0;i<t;i++)/*Error: i starts from 0 & will run till i<t but t is always 0 as initialized*/
{/*Now study how the 'for' loop works and try to understand the error yourself*/
printf("%d",ar[i]);
printf("\n");
}
}
4 Likes