-1

I need a C program to compare 4 numbers. I need it to output YES if any 2 numbers or more are equal, otherwise it should output NO.

For example, if a=1, b=2, c=4, d=4 it should output YES because c and d are same. But if all are different from each other only then program should output NO.

I have tried this method. It's facing issues with online compilers. Saying 'uninitialized value usage' if((a!=b) && (a!=c) && (a!=d) && (b!=c) && (b!=d) && (c!=d) `

Test: #1, time: 30 ms., memory: 0 KB, exit code: 0, checker exit code: 1, verdict: WRONG_ANSWER

Input 1987 Answer 2013 Checker Log wrong answer Answer contains longer sequence [length = 1], but output contains 0 element . So I need another one. link : https://codeforces.com/problemset/problem/271/A

#include<stdio.h>

int main()
{
    int a, b, c, d, number , i , temp;

    scanf("%d",&number);

    int k;
    number+=1;
    
    for(number;number<9001;number++){
        temp=number;
        for(i=1;i<5;i++){
            if(i==1){
                a=temp%10;
                temp=temp/10;
            }
            else if (i==2){
                b=temp%10;
                temp=temp/10;
            }
            else if (i==3){
                c=temp%10;
                temp=temp/10;
            }
            else if (i==4){
                d=temp%10;
                temp=temp/10;
            }
            if((a!=b) && (a!=c) && (a!=d) && (b!=c) && (b!=d) && (c!=d) && (i==4)){
                printf("%d",number);
                k=0;
                break;
            }
            else{
                continue;
            }
        }
        if(k==0){
            break;
        }
    }
}```


[problem link][1]
[1]: https://codeforces.com/problemset/problem/271/A
  • 1
    Post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – alex01011 May 01 '21 at 01:01
  • Think a, b, c, d are 4 numbers. I need a program (without this method - if((a!=b) && (a!=c) && (a!=d) && (b!=c) && (b!=d) && (c!=d) ) which will tell me all are equal or not. – Pronay Sarker May 01 '21 at 01:07
  • `if (a == b && a == c && a == d) return 1;` ? – alex01011 May 01 '21 at 01:09
  • what if `c=d?` .. – Pronay Sarker May 01 '21 at 01:13
  • "uninitialized value usage" means you tried to read a variable without assigning a value to it first. – dbush May 01 '21 at 01:39
  • #include int main() { int a, b, c, d, number , i , temp; scanf("%d",&number); int k; number+=1; for(number;number<9001;number++){ temp=number; for(i=1;i<5;i++){ if(i==1){ a=temp%10; temp=temp/10; } else if (i==2){ b=temp%10; – Pronay Sarker May 01 '21 at 01:48
  • temp=temp/10; } else if (i==3){ c=temp%10; temp=temp/10; } else if (i==4){ d=temp%10; temp=temp/10; } if((a!=b) && (a!=c) && (a!=d) && (b!=c) && (b!=d) && (c!=d) && (i==4)){ printf("%d",number); k=0; break; } else{ continue; } } if(k==0){ break; } } } – Pronay Sarker May 01 '21 at 01:48
  • My code ^. Anything wrong with that? – Pronay Sarker May 01 '21 at 01:48
  • Code in comments is unreadable. Add your code to the question, and be sure to format / indent it properly. – dbush May 01 '21 at 01:48
  • done........... – Pronay Sarker May 01 '21 at 01:52
  • The `&&` operator guarantees left-to-right evaluation, so the `i==4` check should be *first*, otherwise you read uninitialized variables. Better yet, get rid of the inner loop entirely. – dbush May 01 '21 at 01:54
  • same as before. Checker Log- wrong answer Answer contains longer sequence [length = 1], but output contains 0 elements – Pronay Sarker May 01 '21 at 01:57
  • This code works fine on visual stdio. But not in codeforces. Link -- https://codeforces.com/problemset/problem/271/A – Pronay Sarker May 01 '21 at 01:59
  • try to sort four number, then check `(a==b)||(b==c)||(c==d)` can be more clear – newlife May 01 '21 at 06:17

1 Answers1

0

I'm not sure if I am missing something but doesn't something like this work?

#include <stdio.h>

char const* compareFour(int a, int b, int c, int d){
    static char const* YES = "YES", *NO = "NO";
    if(a==b || a==c || a==d || b==c || b==d || c==d) {
        return YES;
    } else {
        return NO;
    }
}


int main() {
    printf("%s\n", compareFour(1, 2, 3, 4));
    printf("%s\n", compareFour(1, 2, 3, 3));
    printf("%s\n", compareFour(1, 2, 1, 4));
}

EDIT: This is in response to the edited question

The core part of the answer remains similar as above, I've just added some driving code for the CodeForce question

#include <stdio.h>

int compareFour(int a, int b, int c, int d){
    if(a!=b && a!=c && a!=d && b!=c && b!=d && c!=d) {
        return 1;
    } else {
        return 0;
    }
}
 
int distinctDigits(int year) {
    int num[4] = {0}, idx = 0;
    while(year > 0) {
        num[idx++] = year%10;
        year/= 10;
    }
    return compareFour(num[0], num[1], num[2], num[3]);
}
 
int main() {
    int year;
    scanf("%d", &year);
    year++;
    for(; year < 9876; ++year) {
        if(distinctDigits(year)) {
            printf("%d", year );
            break;
        }
    }
}

There are a few problems with your answer (in the question):

  1. You limit the answer number to 9001. That's the limit of the input. The output can be anything.

  2. Your k is unassigned and is most probably set to 0, that's why your loop probably exits in the first condition.

  3. (Nitpick) Remove that

    else {
        continue;
    }
    

That's redundant. If the condition in if(...) doesn't match, the loop will continue. Fix these and your solution should work too.

Zoso
  • 2,286
  • 1
  • 12
  • 22