0

Can someone find the problem? This is a simple C program. It is supposed to print 11, 12, 13 but it does not. Why? Thanks!

#include<stdio.h>
#include<conio.h>

void main()
{
    int no[4]={11,12,13,14}; /* An array containing 4 numbers */
    int num[4]; 
    int count=0;
    int p;
    int s=1; 
    char c='y';

    for(int j=0;j<=3;j++)
    {
       p=no[j];

       for(int n=s;n<=3;n++)
       {
         if(p!=no[n]) c='n'; /* If not equal*/
       }
       if(c=='n')
       {
         num[count]=p;
         s=s+1;
         count=count+1;
        }
     }
     printf("%d %d %d",num[2],num[1],num[3]);
     getche();
}
eddie
  • 1,252
  • 3
  • 15
  • 20

3 Answers3

0

Your output might be buffered and will only output to the terminal when encountering a newline: c stdout print without new line?

Either call fflush(stdout); after your printf, or print an entire line:

printf("%d %d %d\n",num[2],num[1],num[3]);
Lanting
  • 3,060
  • 12
  • 28
0

The main problem that I see is with

    if(c=='n')
     {
         num[count]=p;
         s=s+1;
         count=count+1;
    }

block. You're assigning num[i] conditionally. In case that condition is not met, you'll access uninitialized members which contains indeterminate values. This leads to undefined behavior. You should at least initialize the local array which has automatic storage.

Alo, one more issue, as already mentioned in @CKT's answer, is, you're using the same variable j for both outer and inner loop control. What happens here, is for the first iteration of the outer loop, inner loop executes and increments j and after the iteration of the inner loop is done, the control jumps to outer loop control and the condition check fails as the control variable is already altered by the inner loop increment statement.

Thus, your target array members remains uninitialized. Use separate loop control variables for inner and outer loops.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

In your inner for loop you are incrementing j, so outer for loop runs only one time and you get junk values in your num array.

Use code:

#include<stdio.h>
#include<conio.h>
void main()
{
    int no[4]={11,12,13,14}; /*An array containing 4 number*/
    int num[4]; int count=0;
    int p;int s=1; char c='y';
    for(int j=0;j<=3;j++)
    {
       p=no[j];

       for(int n=s; n<=3; n++)
       {
         if(p!=no[n]) c='n'; /* If not equal*/
        }
        if(c=='n')
        {
           num[count]=p;
           s=s+1;
           count=count+1;
        }
    }
    printf("%d %d %d",num[2],num[1],num[3]);
    getche();
}

Also for printing fourth number use should use

 printf("%d %d %d %d",num[2],num[1],num[3], num[0]);
Chandra
  • 471
  • 2
  • 10
  • It will print 3 no. It is ok. But after the completion inner loop (j), control should return to outer loop. But it is not happening; – Muhammad Al Mamun Jun 13 '17 at 07:26
  • When you use "for(int n=s; n<=3; n++)" for your inner loop it will. Run the code i have provided it returns "13 12 14" as output – Chandra Jun 13 '17 at 07:28
  • Thanks CKT and everybody for assistance. As i am new to c so i unintentionally put wrong variable in inner loop. I was working to remove duplicate number from array. And it was my first step to apply my knowledge on this matter. Thanks everybody again. Hope to get help again in future. – Muhammad Al Mamun Jun 13 '17 at 07:37