Temperatures puzzle discussion

I’ve managed to pass the first test case by simplifying the code. However, Testcase 4 had failed. Is there something that were amiss by that the time that I’ve written the code.

Here are the results of Test 4
Failure
Found: -5
Expected: 5

Were there any errors that were made?

The statement mentions:

If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5).

It looks like your code hasn’t implemented that part, or has implemented it incorrectly.

All my test cases run perfectly when I test them,
but when I submit the code, it shows that some test cases are incorrect.
Every time I submit it, I get different results.
Sometimes it passes up to 90%, and other times only 64%.

int main()
{
    int n,b;
    int x=0,y=0;
    int p[100],m[100];
    scanf("%d", &n);
    if(n==0){
        printf("0\n");
    }
    else{
        for (int i = 0; i < n; i++) {
            int t;
            scanf("%d", &t);
            if(t!=0){   
                if(t>0){
                    p[x]=t;
                    x++;
                }
                else{
                    m[y]=-t;
                    y++;
                }
            }
        }

        for(int i=0;i<x;i++){
            for (int j=i+1;j<x;j++){
                if(p[i]>p[j]){
                    b=p[i];
                    p[i]=p[j];
                    p[j]=b;
                }
            }
        }
        for(int i=0;i<y;i++){
            for (int j=i+1;j<y;j++){
                if(m[i]>m[j]){
                    b=m[i];
                    m[i]=m[j];
                    m[j]=b;
                }
            }
        }
        if(p[0]==m[0]){
            printf("%d\n",p[0]);
        }
        if(p[0]>m[0]){
            printf("-%d\n",m[0]);
        }
        if(p[0]<m[0]){
            printf("%d\n",p[0]);
        }
    }  
    

    return 0;
}

if any thing in my code is incorrect please correct me

Try a test case where there is only positive numbers e.g.

3
10 20 30

In that case I believe since nothing was added to m, m[0] would be a random number which would cause your random output issue.

Give that a go at debugging / fixing and if there is still any problems let me know!

1 Like

Its a good test but it doesn’t actually require an array. I was surprised to see it, actually.

[Mod edit: Please avoid posting code on the forum.]

bonjour,

j’ai essayé avec ce code en C++ :

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/

int main()
{
    int n; // the number of temperatures to analyse
    int temp ; 
    cin >> n; cin.ignore();
    int t;
    if(n==0){
        cout << 0 << endl ; 
     }
    for (int i = 0; i < n; i++) {
         // a temperature expressed as an integer ranging from -273 to 5526
       
        cin >> t; cin.ignore();
        if(*t!=null){
        if(i==0){
          //si la variable i est égale à 0,soit, on est dnas le premier tour de la boucle , alors temp est égale à t
          temp=t ;
        }
    }
    if(t==null){
       // cout << t << endl ; 
     // cout << temp << endl ; 
         if (t < 0) {
        if (temp > 0 ) {

            //on teste si les deux valeurs , c'est à dire temp et t sont égales 
            if (abs(t) == abs(temp)) {
                // on affiche la valeur absolue de t 
                cout << abs(t) << endl;
            }
            else if (abs(t) - abs(temp) > 0) {
                cout <<  temp << endl;
            }
            else if (abs(t) - abs(temp) < 0) {
                temp = t;
                cout <<  temp << endl;

            }
            /*if (temp < t)
            {
                temp = t;
                cout << "si t est supérieur à 0 et que temp est supérieur à 0 et que temp est inférieur à t alors  " << temp << endl;
            }*/

            // il faut tester en plus si t!= temp 
            /*else if (temp < 0 && t != temp) {
                temp = t;
                cout << temp << endl;
            }*/

        }
        else if (temp < 0) {
            //on teste si les deux valeurs , c'est à dire temp et t sont égales 
            if (abs(t) == abs(temp)) {
                // on affiche la valeur absolue de t 
                cout <<  abs(t) << endl;
            }
            else if (abs(t) - abs(temp) > 0) {
                cout <<  temp << endl;
            }
            else if (abs(t) - abs(temp) < 0) {
                cout<< t << endl;
            }

        }
          else if (t == temp) {
            temp = abs(t);
            cout <<temp << endl;
        }
      /*  if (temp > t && t != temp) {
            temp = t;
            cout <<"si t est inférieur à 0 et que temp est supérieur t et que t est différent de temp" << temp << endl;
        }
        else {
            cout << "si t est inférieur à 0 et que temp n'est  pas supérieur t et que t n'est pas différent de temp" << t << endl;
        }*/
    }
    //on teste si t est supérieur à 0
    else if (t > 0) {
        // la variable t est supérieure et que temp est inférieur à 0
        if (temp < 0) {
            //on teste si les deux valeurs , c'est à dire temp et t sont égales 
            if (abs(t) == abs(temp)) {
                // on affiche la valeur absolue de t 
                cout << abs(t) << endl;
            }
            else if (abs(t) - abs(temp) > 0) {
                cout <<  temp << endl;
            }
            else if (abs(t) - abs(temp) < 0) {
                cout << t << endl;
            }
        }
        else if (temp > 0) {
            //on teste si les deux valeurs , c'est à dire temp et t sont égales 
            if (abs(t) == abs(temp)) {
                // on affiche la valeur absolue de t 
                cout << abs(t) << endl;
            }
            else if (abs(t) - abs(temp) > 0) {
                cout << temp << endl;
            }
            else if (abs(t) - abs(temp) < 0) {
                temp = t;
                cout  << temp<< endl;

            }
            /*if (temp < t)
            {
                temp = t;
                cout << "si t est supérieur à 0 et que temp est supérieur à 0 et que temp est inférieur à t alors  " << temp << endl;
            }*/

            // il faut tester en plus si t!= temp 
           /* else if (temp < 0 && t != temp) {
                temp = t;
                cout << temp << endl;
            }*/

        }
        else if (t == temp) {
            temp = abs(t);
            cout <<temp << endl;
        }
    }
}
    }
    
         
 }

    // Write an answer using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;
j'ai un problème avec un pointeur à cette ligne: 

   for (int i = 0; i < n; i++) {
         // a temperature expressed as an integer ranging from -273 to 5526
       
        cin >> t; cin.ignore();
        if(*t!=null){
        if(i==0){
          //si la variable i est égale à 0,soit, on est dnas le premier tour de la boucle , alors temp est égale à t
          temp=t ;
        }
    }

même avec ceci :

for (int i = 0; i < n; i++) {
         // a temperature expressed as an integer ranging from -273 to 5526
       
        cin >> t; cin.ignore();
        if(&t!=null){
        if(i==0){
          //si la variable i est égale à 0,soit, on est dnas le premier tour de la boucle , alors temp est égale à t
          temp=t ;
        }
    }

et ceci non plus ne fonctionne pas :

if(t==null){
       // cout << t << endl ; 
     // cout << temp << endl ; 
         if (t < 0) {
        if (temp > 0 ) {

            //on teste si les deux valeurs , c'est à dire temp et t sont égales 
            if (abs(t) == abs(temp)) {
                // on affiche la valeur absolue de t 
                cout << abs(t) << endl;
            }
            else if (abs(t) - abs(temp) > 0) {
                cout <<  temp << endl;
            }
            else if (abs(t) - abs(temp) < 0) {
                temp = t;
                cout <<  temp << endl;

            }
            /*if (temp < t)
            {
                temp = t;
                cout << "si t est supérieur à 0 et que temp est supérieur à 0 et que temp est inférieur à t alors  " << temp << endl;
            }*/

            // il faut tester en plus si t!= temp 
            /*else if (temp < 0 && t != temp) {
                temp = t;
                cout << temp << endl;
            }*/

        }