Home > Software engineering >  no repeating numbers in an array using c language
no repeating numbers in an array using c language

Time:10-28

prompt the user to enter 10 integers and if there was a repeated number it will ask again 10 integers and if there are no repeated numbers it will end the loops.

this is the expected output :

if I enter 10 numbers the program will review the array if there was a repeated number and if there is, it will ask to user to input again 10 integer. the loops will end if there is no repeated numbers.

Enter 10 elements in the array :
Input 1: 1
Input 2: 2
Input 3: 3
Input 4: 4
Input 5: 5
Input 6: 5
Input 7: 5
Input 8: 6
Input 9: 7
Input 10: 8
5 is repeated, please enter numbers again // the 5 repeated in the array.

Enter 10 elements in the array :
Input 1: 1
Input 2: 2
Input 3: 3
Input 4: 4
Input 5: 5
Input 6: 6
Input 7: 7
Input 8: 8
Input 9: 9
Input 10: 0
There are no repeated numbers!
#include<stdio.h>
int main() 
{ 
    int array[10]; 
    int i,j ,num=10;
    int n = sizeof(array)/sizeof(array[0]); 

    int visited[n];
    
    do{
        printf("\n");
        printf("Enter %d elements in the array : \n", num);
        for(i=0;i<n;i  )
        {
            printf("Input %i: ",i 1);
            scanf("%d", &array[i]);
        }
    
        for(i=0; i < n; i  )
       
            if(visited[i] == 0){
                int count = 1;
        
                for(j = i 1; j < n; j  ) { 
                    // if appears again in the array 
                    if(array[i] == array[j]) 
                    { // increase count & mark index visited 
                        count  ; 
                        visited[j] = 1; 
                    } 
                } // 
            
                if(count >= 1){
                    printf("%d is repeated, please enter numbers again ",array[i]);
                    printf("\n");
                    break;
                }else{
                    printf("There are no repeated numbers!");
                    break;
                } 
            }
    }while(!array[i]); 
}

CodePudding user response:

The variable length array visited was not initialized

int visited[n];

So for example this if statement

if(visited[i] == 0){

invokes undefined behavior.

This if statement

        if(count >= 1){
            printf("%d is repeated, please enter numbers again ",array[i]);
            printf("\n");
            break;

does not make a sense because even if a value is not repeated in the source array nevertheless initially count is set to 1

  if(visited[i] == 0){
        int count = 1;

There is no need to define an auxiliary array to resolve the problem.

And the condition in the do-while statement

}while(!array[i]); 

also does not make sense.

The program can look for example the following way

#include <stdio.h>

int main( void ) 
{ 
    enum { N = 10 };
    int array[N];
    
    int repeated = N;

    do
    {
        printf("\n");
        printf( "Enter %d elements in the array :\n", N );

        for ( int i = 0; i < N; i   )
        {
            printf( "Input %i: ", i   1 );
            scanf( "%d", &array[i] );
        }
    
        repeated = N;
        
        for ( int i = 0; i < repeated; i  = repeated == N )
        {
            int j = i   1;
            while ( j < N && array[j] != array[i] ) j  ;
           
            if ( j != N ) repeated = i;
        }
            
        if ( repeated != N )
        {
            printf( "%d is repeated, please enter numbers again.\n", array[repeated] );
        }
        else
        {
            puts( "There are no repeated numbers!" );
        } 
    } while( repeated != N ); 
}
  • Related