# Thread: Ignore negative numbers in array

1. ## Ignore negative numbers in array

So i have this program that takes in user input and stores them into an array and then prints them, removes duplicates, and sorts in ascending order. The user can also stop by inputting a sentinel value (in this case -1). But i am also supposed to ignore any negative value besides -1. When i input any other negative value into the program it messes up. How would i go about ignoring the negative values?

Code:
```#include<stdio.h>

int main()
{
int input, nums[20], i, j, k, temp, count=0, count2=0;

for(i=0;i<20;i++)
{

printf("Enter any positive integer (Enter -1 to stop): ");
scanf("%i", &input);
nums[i]=input;
count++;
if(input==-1)
{
nums[i]=nums[i-1];
count--;
i=20;
}

else if(input<-1)
{
nums[i]=nums[i-1];
count--;
}

}

printf("\nOriginal list: ");

for(i=0;i<count;i++)
{
printf("%i ", nums[i]);
}

printf("\nDuplicates removed: ");
for(i=0;i<count;i++)
{
for(j=i+1;j<count;)
{
if(nums[i]==nums[j])
{
for(k=j;k<count;k++)
nums[k]=nums[k+1];
count--;
}
else
j++;
}
}

for (i=0;i<count;i++)
printf("%i ", nums[i]);

printf("\nAscending order with duplicates removed: ");
for(j=0;j<count-1;j++)
for(i=0;i<count-1;i++)
{
if(nums[i]>nums[i+1])
{
temp = nums[i];
nums[i] = nums[i+1];
nums[i+1] = temp;
}
}

for(i=0;i<count;i++)
printf("%i ",nums[i]);

return 0;
}```

2. I also have that random count2 variable i was trying something out with, but it didnt work out.

3. How do you want to ignore them, and when?

If I gave you a list of numbers with some negative numbers to be ignored, and you had no computer, how would you go about ignoring those negative numbers less than -1?

Forget the program for now.

4. i would just get rid of them and continue on with the list. So if i had 5 numbers like 1,2,3,-4,5 all that would show up is 1,2,3,5

5. Originally Posted by Ram999
i would just get rid of them and continue on with the list. So if i had 5 numbers like 1,2,3,-4,5 all that would show up is 1,2,3,5
So it's probably a really bad idea to do "nums[i]=input" before you've checked whether you want to store input.

6. ok so i made some progress. But now the ascending algorithm stops after like 6 inputs and i dont know why

Code:
```#include<stdio.h>

int count2=0;

int main()
{
int input, nums[20], i, j, k, temp, count=0;

for(i=0;i<20;i++)
{

printf("Enter any positive integer (Enter -1 to stop): ");
scanf("%i", &input);
if(input<-1)
{
i--;
count2++;
}
else if(input==-1)
{
i=20;
}
else
{
nums[i]=input;
count++;
}
}

printf("\nOriginal list: ");

for(i=0;i<count;i++)
{
printf("%i ", nums[i]);
}

printf("\nDuplicates removed: ");
for(i=0;i<count;i++)
{
for(j=i+1;j<count;)
{
if(nums[i]==nums[j])
{
for(k=j;k<count;k++)
nums[k]=nums[k+1];
count--;
}
else
j++;
}
}

for (i=0;i<count;i++)
printf("%i ", nums[i]);

printf("\nAscending order with duplicates removed: ");
for(j=0;j<count-count2;j++)
for(i=0;i<count-count2;i++)
{
if(nums[i]>nums[i+1])
{
temp = nums[i];
nums[i] = nums[i+1];
nums[i+1] = temp;
}
}

for(i=0;i<count;i++)
printf("%i ",nums[i]);

return 0;
}```

7. i recommend use the algorithm the selected sort
it is an algorithm very good for order the small array

this code:
Code:
```int min,aux;

for(int i=0;i<count-count2-1;i++){

min=i;

for(j=i+1;j<count-count2;j++)
if(nums[j]<nums[min])
min=j;

aux=nums[min];
nums[min]=nums[i];
nums[i]=aux;

}```

8. Originally Posted by Ram999
ok so i made some progress. But now the ascending algorithm stops after like 6 inputs and i dont know why
What are count and count2? They seem to represent num_elements and num_bad_inputs, respectively, so why not name them as such. Print them just before you sort the list, along with num_elements-num_bad_inputs. That is how many elements of your list you sort. Notice that, if you have any invalid inputs, you sort the whole list.

Also, why would you make count2 global? Globals are generally a bad idea, especially for newbies. And you really don't need globals here. You don't even really need count2 (or num_bad_inputs if you renamed it). Remove it all together.

You need some better variable names. It will make your code clearer and make it easy for you to figure out what is going on. Also, I would break this up into functions. One to get input, one to remove duplicates, one to sort the list, and one to print it.

You should avoid magic numbers. #define a constant MAX_ELEMENTS instead of using 20 everywhere.

Lastly, I think you over-complicate the input part. You could use a basic do-while loop -- which is great for user input since it always asks the user once for input (i.e. it runs once before checking the loop condition) -- and only add to the list if it's a non-negative*:
Code:
```#define MAX_ELEMENTS 20
int nums[MAX_ELEMENTS]
int num_elements = 0
do {
// informs the user how many elements they have entered and the max elements they can enter before the list is full
printf("Enter any non-negative integer, or -1 to stop (element %d of %d): ", num_elements+1, MAX_ELEMENTS);
scanf input
if input >= 0  // accept non-negative
nums[num_elements++] = input  // insert at end of array and increment element count
} while (num_elements < MAX_ELEMENTS && input != -1)```
The if there ensures only non-negative numbers go in the list. The while condition ensures 2 things: you don't go out of the array bounds, and you stop when the input is -1.

Also:
Code:
```            if(nums[i]==nums[j])
{
for(k=j;k<count;k++)
nums[k]=nums[k+1];
count--;
}```
count-- is not in the loop there. I don't know if you meant it to be or not, but the indentation does not match what the code actually does. Use curly brackets to include both statements in the for loop, or fix the alignment.

* Note, there is a technical difference between positive and non-negative. Positive integers are 1, 2, 3, 4..., there is no zero. Non-negative integers include zero: 0, 1, 2, 3, 4.... Be clear in what you ask of the user, and what you accept/ignore in your program.

9. Originally Posted by vittorio92
i recommend use the algorithm the selected sort
it is an algorithm very good for order the small array
Your code has the same bug as Ram999's. It has nothing to do with the sort method used (bubble vs selection) and everything to do with the fact that he was not sorting the full list. Suggesting a better method is good, but please double check your suggestions before suggesting a "fix". And here, selection is barely better than bubble. Selection sort's biggest advantage over bubble sort is that it reduces the number of swaps. That is only a big benefit when the cost of a swap is high, e.g. requiring a deep copy of a large, complicated object.

Popular pages Recent additions