-2

I have got this homework, Write a program that reads ten numbers entered by the user and indicates if there are any of them identical. I didn´t program anything for like a year so I forgot a lot of stuff, and I didn´t find anything releated to this topic. i tried using field and if..else , but it didnt work . Is there any faster way to compare these numbers? Or some other method for this problem?

My code is kinda shitty but I didn´t come up with anything better.The code is in my own language but it shouldn´t be a problem. If you would help me I would be grateful

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

   if (pole[0]==pole[2])
   printf("first and second number are identical");
   else if (pole[0]==pole[3])
   printf("prvé a tretie číslo sa rovnajú");
   else if (pole[0]==pole[4])
   printf("prvé a stvrte číslo sa rovnajú");
   else if (pole[0]==pole[5])
   printf("prvé a piate číslo sa rovnajú");
   else if (pole[0]==pole[6])
   printf("prvé a sieste číslo sa rovnajú");
   else if (pole[0]==pole[7])
   printf("prvé a siedme číslo sa rovnajú");
   else if (pole[0]==pole[8])
   printf("prvé a osme číslo sa rovnajú");
   else if (pole[0]==pole[9])
   printf("prvé a deviate číslo sa rovnajú");
   else if (pole[0]==pole[10])
   printf("prvé a desiate číslo sa rovnajú");

   else if (pole[2]==pole[3])
   printf("druhe a tretie číslo sa rovnajú");
   else if (pole[2]==pole[4])
   printf("druhe a stvrte číslo sa rovnajú");
   else if (pole[2]==pole[5])
   printf("druhe a piate číslo sa rovnajú");
   else if (pole[2]==pole[6])
   printf("druhe a sieste číslo sa rovnajú");
   else if (pole[2]==pole[7])
   printf("druhe a siedme číslo sa rovnajú");
   else if (pole[2]==pole[8])
   printf("druhe a osme číslo sa rovnajú");
   else if (pole[2]==pole[9])
   printf("druhe a deviate číslo sa rovnajú");
   else if (pole[2]==pole[10])
   printf("druhe a desiate číslo sa rovnajú");
   
   else if (pole[3]==pole[4])
   printf("tretie a stvrte číslo sa rovnajú");
   else if (pole[3]==pole[5])
   printf("tretie a piate číslo sa rovnajú");
   else if (pole[3]==pole[6])
   printf("tretie a sieste číslo sa rovnajú");
   else if (pole[3]==pole[7])
   printf("tretie a siedme číslo sa rovnajú");
   else if (pole[3]==pole[8])
   printf("tretie a osme číslo sa rovnajú");
   else if (pole[3]==pole[9])
   printf("tretie a deviate číslo sa rovnajú");
   else if (pole[3]==pole[10])
   printf("tretie a desiate číslo sa rovnajú");
   
   else if (pole[4]==pole[5])
   printf("stvrte a piate číslo sa rovnajú");
   else if (pole[4]==pole[6])
   printf("stvrte a sieste číslo sa rovnajú");
   else if (pole[4]==pole[7])
   printf("stvrte a siedme číslo sa rovnajú");
   else if (pole[4]==pole[8])
   printf("stvrte a osme číslo sa rovnajú");
   else if (pole[4]==pole[9])
   printf("stvrte a deviate číslo sa rovnajú");
   else if (pole[4]==pole[10])
   printf("stvrte a desiate číslo sa rovnajú");
   
   else if (pole[5]==pole[6])
   printf("piate a sieste číslo sa rovnajú");
   else if (pole[5]==pole[7])
   printf("piate a siedme číslo sa rovnajú");
   else if (pole[5]==pole[8])
   printf("piate a osme číslo sa rovnajú");
   else if (pole[5]==pole[9])
   printf("piate a deviate číslo sa rovnajú");
   else if (pole[5]==pole[10])
   printf("piate a desiate číslo sa rovnajú");
   
   else if (pole[6]==pole[7])
   printf("sieste a siedme číslo sa rovnajú");
   else if (pole[6]==pole[8])
   printf("sieste a osme číslo sa rovnajú");
   else if (pole[6]==pole[9])
   printf("sieste a deviate číslo sa rovnajú");
   else if (pole[6]==pole[10])
   printf("sieste a desiate číslo sa rovnajú");
   
   else if (pole[7]==pole[8])
   printf("siedme a osme číslo sa rovnajú");
   else if (pole[7]==pole[9])
   printf("siedme a deviate číslo sa rovnajú");
   else if (pole[7]==pole[10])
   printf("tretie a desiate číslo sa rovnajú");
   
   else if (pole[8]==pole[9])
   printf("osme a deviate číslo sa rovnajú");
   else if (pole[8]==pole[10])
   printf("osme a desiate číslo sa rovnajú");
   
   else if (pole[9]==pole[10])
   printf("deviate a desiate číslo sa rovnajú");
   else
   printf("there are no identical number");
   }
   
   
   
    return 0;
}

2
  • Welcome to SO. You mention C# in the title but it is entirely C. Regarding the code, you should read the chapters about loops again. Two nested loops should do the trick and make the code look much cleaner.
    – Gerhardh
    Nov 23, 2020 at 12:54
  • Please show some intelligence such as using loops! Nov 23, 2020 at 12:56

4 Answers 4

0

Nested loops.

for (int i = 0; i < n - 1; i++)
  for (int j = i + 1; j < n; j++)
    if (pole[i] == pole[j]) {...}
0

You have a for cycle at the beginning of yout code but you not using it, the cleanest soluction would be with 2 for cycles like so:

int f = 0;

for(int i=0;i<=n;i++){
   for(int j=0;j<i;j++){
       if(pole[i] == pole[j]){
            printf("number at position %d is equal to number at position %d",i,j);
            f = 1;
       }
   }
}
if (f == 0)
    printf("There are no identical numbers");

In situations like these loops should be used, we first loop to all elements with the i variable, and with the j variable, for each element that is before the i we will test it if they are equal, if they are we print out that they match.

The f stands for flag, so we can now if we found a match, if not we will print at the end that there are no identical numbers

0
int flag = 0;
for(int i=0;i<n;i++){
    for(int j=i; j<n; j++){
        if(poll[i] == poll[j]){
            printf("%d th and %d th numbers are identical",i,j);
            flag = 1;
        }    
    }
}
if(flag == 0){
    printf("No identical numbers are there")
}
0

Thanks for the response it helped me a lot and I learned something. I changed the code and no matter what I do it always shows that "There are no identical numbers". I don't know whats the isue and I am hopeless.

int pole[10];
int i,n,j;


for (i = 0; i <10; i++ ){
    printf ("\n Zadaj [%d] prvok pola: ",i);
    scanf ("%d",&pole[i]);
}

int f=0;

for( i=0;i<=n;i++){
 for( j=0;j<i;j++){
   if(pole[i] == pole[j]){  
        printf("number at position %d is equal to number at position %d",i,j);
        
   }
  }
}
if (f == 0)
printf("There are no identical numbers");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.