Jake2018
Jake2018

Reputation: 93

Weird Array Outputs

Okay what am I doing wrong here?

This program is supposed to read 20 integers and then output an array of the integers that are not duplicates (Output each integer only once).

//Program to read 20 integers and return each integer only once (no duplicates).
#include <stdio.h>

int main()
{
    int a, b, count=0, temp, array1[20];

    printf("Enter 20 array elements between 1 and 10 inclusive\n");
    for (a=0; a<20; a++) //Loop to enter 20 elements
    {
        scanf("%d", &temp);
        for (b=0; b<=20; b++) //Loop to test each new element against all previous entered elements
    {
        if (array1[b] == temp) //If duplicate increment count
        {
            count++;
        }
        else if (count == 0 && b == 20) //If there have been no duplicates and 20 numbers have been tested... add entered number to the array
        {
            array1[a] = temp;
        }
    }
}
for (a=0; a<20; a++)
{
    printf("%d\t", array1[a]);
}
return 0;

}

Upvotes: 0

Views: 65

Answers (2)

Rishikesh Raje
Rishikesh Raje

Reputation: 8614

There are the following things wrong here.

  1. In the inner loop, during the first check, you are comparing against 20 elements. On receiving the first element you do not have any elements to compare against. I have added a variable size to indicate the size of the array. size is initialized to 0.

  2. The if (count == 0 && b == 20) should be moved outside the for loop and can be simplified to if (count == 0)

  3. When an element is added to the array it is added at array1[size] and size is incremented.

  4. You need to reinitialize count at every outer for loop as shown below.

  5. The printing will print size elements that are non duplicate.

Code is below.

//Program to read 20 integers and return each integer only once (no duplicates).
#include <stdio.h>

int main()
{
    int a, b, count=0, temp, array1[20];

    int size = 0;
    printf("Enter 20 array elements between 1 and 10 inclusive\n");
    for (a=0; a<20; a++) //Loop to enter 20 elements
    {
        scanf("%d", &temp);
        count = 0;
        for (b=0; b<size; b++) //Loop to test each new element against all previous entered elements
        {
            if (array1[b] == temp) //If duplicate increment count
            {
                count++;
            }
        }
        if (count == 0) //If there have been no duplicates and 20 numbers have been tested... add entered number to the array
        {
            array1[size] = temp;
            size++;
        }
    }
    for (a=0; a<size; a++)
    {
        printf("%d ", array1[a]);
    }
    return 0;
}

This code will accept 20 elements and store and display as many as were non duplicate (which can be 1-20). If you want to store 20 non duplicate elements (entering possibly many more than 20) it can be easily modified.

Upvotes: 2

4386427
4386427

Reputation: 44274

You have multiple reads of uninitialized variables which is undefined behavior. You also access the array out of range.

for (b=0; b<=20; b++)
           ^^
           This will result in b in the range [0..20]
{
    if (array1[b] == temp) //If duplicate increment count
        ^^^^^^^^^
        array1[b] is uninitialized
        and when b is 20 you access out of range

Further you only write to the array when count is 0 and b is 20

    else if (count == 0 && b == 20)
    {
        array1[a] = temp;
    }

Notice that you never reset count so after the first match you'll never write the array again

BTW - you print:

 Enter 20 array elements between 1 and 10 inclusive

but you never perform any check of the input value to be in that range.

Upvotes: 1

Related Questions