简体   繁体   中英

scanf is continuously taking values (input)

I have a homework problem where I have to input an array, and take out all the distinct elements from it.

For this I have made a new sub_array where all distinct values will be stored, Howsoever during input scanf is not taking values as supposed to?

Here is my code:-

# include <stdio.h>
# include <stdlib.h>

int main()
{
    int number; // Variable name "number" which will specify the size of dynamically allocated array.

    printf("Enter the size of your array\n");
    scanf("%d",&number);

    int *array;
    array = (int*)calloc(number, sizeof(int)); // Dynamically allocated memory.

    int i,j=0; // Counter variables for loop.

    printf("Enter the elements of arrays\n");

    for(i = 0; i < number; i++)
    {
        scanf("%d",(array + i)); // Code is asking for endless numbers and is not outputting the desired result. THIS IS THE PROBLEM SECTION.
    }


    for(i = 0; i < number; i++)
    {
        for( j = i + 1 ; j < number; j++)
        {
            if( *(array + i ) == *(array + j))
            {
                *(array + j) = 0; // My way of removing those numbers which are the repeated. (Assigning them value of zero).
            }
        }
    }

    i=0;j=0;

    int sub_number = 0;

    while(i < number)
    {
        if(*(array + i) != 0)
        {
            sub_number++; // Variable name "sub_number" which will specify the size of dynamically allocated array "sub_array".
        }
    }

    int *sub_array;
    sub_array = (int*)calloc(sub_number,sizeof(int));

    for(i = 0;i < sub_number;i++)
    {
        printf("%d\n",*(sub_array + i)); // Desired array which only contains distinct and unique variables.
    }
    return 0;
}

Edit:- I missed one loop where i forgot to fill the sub_array loop. Here is the code :-

for(i = 0;i < number ;i++) //New code inserted.
    {
        if( *(array + i ) != 0)
        {
            *(sub_array + j) = * (array + i );
            j++;
        }
    }

Howsoever my program is still not working.

In the following part of your code:

 while(i < number)
{
    if(*(array + i) != 0)
    {
        sub_number++; // Variable name "sub_number" which will specify the size of dynamically allocated array "sub_array".
    }
}

You are not incrementing i. You miss an i++; . Your scanf cycle is correct.

There are two errors in your program.

The first one is that inside the while loop the variable i is not incremented.

Write the loop like

while(i < number)
{
    if(*(array + i++) != 0)
    {
        sub_number++; // Variable name "sub_number" which will specify the size of dynamically allocated array "sub_array".
    }
}

The second one is that you forgot to copy elements from array to sub_array.

Include this loop

for ( i = 0, j = 0; i < number; i++ )
{

    if ( *( array + i ) != 0 ) *( sub_array + j++ ) = *( array + i );
}

before printing sub_array.

And you should free all allocated memory.

free( sub_array );
free( array );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM