0

I am creating a simple "ascending" program. When I find smallest int in array of int I want to replace it with some other value so it won't come as smallest number again in the array. For that, I assigned that int NULL. But now the results are not as expected.

Please tell me if I am doing anything wrong. If so then what I should replace with the value of that int ?

#include<stdio.h>
#include<conio.h>
void main()
{

   clrscr();
   int a[10],b,c=0,d=0;
   printf("Enter number of values you want to enter \n");
   scanf("%d",&b);

   printf("Enter values \n");
   for(int i=0;i<b;i++)
      scanf("%d",&a[i]);

   while(c<b)
   { 
      for(int k=0;k<b;k++)
      {
         for(int j=0;j<b;j++)
         {
            if(a[k] > a[j])
            {
                d=1;    
            }
         }
         if(d!=1 && a[k]!=NULL) 
         {
            c++;
            printf("%d ",a[k]);
            a[k]='\0' ; //assigning it as NULL
         }
         if(c >= b)
            break; 
         d=0;
      }
   }
   getch();
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
parth_07
  • 1,322
  • 16
  • 22
  • Are you trying to arrange the array in ascending order? – Abhi Apr 18 '15 at 06:21
  • 2
    `NULL` is equal to `0` So you are trying to assign it `0`. – Arun A S Apr 18 '15 at 06:23
  • Yes i am trying to print the array in ascending order @Abhi – parth_07 Apr 18 '15 at 06:44
  • 1
    Well there are ways to sort your array. Linear sort, bubble sort, insertion sort, quick sort etc. I wouldn't say your way of doing it is completely wrong but you should google and learn these algorithms to get a good idea on how exactly to do it. – Abhi Apr 18 '15 at 06:48
  • And if you want to do it your way I would advice you to debug the code and at each step try to understand the way your code is working. Looking at the code it seems you are using turbo c. In which case use F7 for debugging purposes. – Abhi Apr 18 '15 at 06:52
  • `NULL` is a null pointer. `'\0'`is a null character. They're different things (though they can both be written as `0`. Since you're assigning to an `int`, you should just use `0`. Also, it's `int main(void)`, **not** `void main()`. – Keith Thompson Apr 18 '15 at 07:03
  • Thanks for the advices.... and yeah m using turbo C @Abhi – parth_07 Apr 18 '15 at 14:54

2 Answers2

1

In C and related languages ints are not "nullable" - you could use a special value instead, e.g. a value that is well outside the expected range of your input data, such as INT_MAX:

#include <limits.h>  // required header for INT_MAX et al

...

if(d!=1 && a[k]!=INT_MAX) 
{
    c++;
    printf("%d ",a[k]);
        a[k]=INT_MAX

}

However it would probably be a good idea to go back to the drawing board and see if you can come up with a better algorithm that doesn't require special values.

Paul R
  • 208,748
  • 37
  • 389
  • 560
1

Read the differences between NULL and 0 and '\0' here. There is a type mismatch when you're trying a[k]!=NULL.

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

int main()
{
    clrscr();
    int a[10], b, c = 0, d = 0;
    int k, j, i;

    printf("Enter number of values you want to enter \n");
    scanf("%d",&b);
    printf("Enter values \n");
    for(i = 0;i < b;i++)
        scanf("%d",&a[i]);

    while(c < b)
    {
            for(k = 0;k < b;k++)
            {
                    for(j = 0;j < b;j++)
                    {
                            if((a[k] > a[j]) && a[j] != 0)
                            {
                                    d=1;
                            }
                    }
                    if(d != 1 && a[k] != 0)
                    {
                            c++;
                            printf("%d ",a[k]);
                            a[k] = 0; //assigning it as NULL
                    }
                    if(c >= b)
                            break;
                    d=0;
            }
    }
    return 0;
    getch();
}

This code fixes the problem.

What you're missing is a[j] != 0 in if((a[k] > a[j]) && a[j] != 0). Also I don't suggest this, as it won't work if there are 0's in the array entered.

Community
  • 1
  • 1
Karthik Nayak
  • 731
  • 3
  • 14