J'essaie de créer un programme en classe qui trouvera les 2 entiers les plus grands et les plus petits dans une séquence donnée, puis les imprimera à l'utilisateur. Un exemple d'exécution ressemblerait à ceci:

Enter the sequence size: 8
Enter the sequence: 5 8 9 12 -6 4 -8 10

The two smallest values are: -8 -6
The two largest values are: 12 10

Je ne suis pas autorisé à utiliser le tri ou le tableau. J'y ai passé toute la nuit mais je n'arrive pas à comprendre, est-ce que quelqu'un pourrait m'indiquer la bonne direction? C'est là que je suis actuellement bloqué - Il ne se compilera pas car les variables 'small1' et 'large1' ne sont pas initialisées, mais si je les mets à zéro, elles restent nulles dans l'exemple.

int small1, small2, large1, large2, loopcount, sequencevalue;
// Ask the user to enter the first number
cout << "Sequence Size: ";
cin >> loopcount;

// Enter the sequence and start the loop
cout << "\nEnter the Sequence: ";

for (int i = 0; i < loopcount; i++)
{
    cin >> sequencevalue;

    if (sequencevalue < small1)
    {
        small2 = small1;
        small1 = sequencevalue;
    }

    if (sequencevalue > large1)
    {
        large2 = large1;
        large1 = sequencevalue;
    }
}

// Small variables
if (small1 == 0)
{
    small1 = sequencevalue;
}
else
{
    if (sequencevalue < small1)
    {
        small2 = small1;
        small1 = sequencevalue;
    }
}

// Large variables
if (large1 == 0)
{
    large1 = sequencevalue;
}
else
{
    if (sequencevalue < large1)
    {
        large2 = large1;
        large1 = sequencevalue;
    }
}

// Final Output
cout << "Two smallest values: " << small1 << " " << small2 << "\n";
cout << "Two largest values: " << large2<< " " << large1 << "\n";

J'apprécie grandement toute aide sur cette question, merci pour votre temps.

0
lana 26 janv. 2017 à 10:49

5 réponses

Meilleure réponse

J'ai inclus les climats et corrigé votre code.

Échantillon de code:

#include <iostream>
#include <climits>
using namespace std;

int main(){
    int count = 0;
    cout << "Sequence Size: ";
    cin >> count;

    int sequencevalue;

    int large1 = INT_MIN;
    int large2 = INT_MIN;
    int small1 = INT_MAX;
    int small2 = INT_MAX;

    for (int i = 0; i < count; i++){
        cin >> sequencevalue;
        if (sequencevalue >= large1) {
            large2 = large1;
            large1 = sequencevalue;
        }
        else if (sequencevalue > large2){
            large2 = sequencevalue;
        }
        if (sequencevalue <= small1) {
            small2 = small1;
            small1 = sequencevalue;
        }
        else if (sequencevalue < small2){
            small2 = sequencevalue;
        }
    }

    cout << "Sequence size is " << count << endl;
    if (0 == count) {}
    else if (1 == count){
        cout << "Smallest value: " << small1 << endl;
        cout << "Largest value: " << large1 << endl;
    }
    else{
        cout << "First and second smallest value: " << small1 << " " << small2 << endl;
        cout << "First and second largest value: " << large1 << " " << large2 << endl;
    }
    return 0;
}

L'exemple de code ci-dessus considère les mêmes nombres comme des éléments uniques (ce qui peut entraîner les mêmes premier et deuxième plus petits / plus grands).

Si vous devez ignorer les mêmes nombres que des éléments uniques, la boucle for devrait ressembler à:

for (int i = 0; i < count; i++){
    cin >> sequencevalue;
    if (sequencevalue > large1) {
        large2 = large1;
        large1 = sequencevalue;
    }
    else if (sequencevalue != large1 && sequencevalue > large2){
        large2 = sequencevalue;
    }
    if (sequencevalue < small1) {
        small2 = small1;
        small1 = sequencevalue;
    }
    else if (sequencevalue != small1 && sequencevalue < small2){
        small2 = sequencevalue;
    }
}
0
26 janv. 2017 à 09:52
int small1, small2, large1, large2, loopcount, sequencevalue;
// Ask the user to enter the first number
cout << "Sequence Size: ";
cin >> loopcount;

// Enter the sequence and start the loop
cout << "\nEnter the Sequence: ";

for (int i = 0; i < loopcount; i++)
{
cin >> sequencevalue;
if(i==0)
small1=sequencevalue;
small2=sequencevalue;
large2=sequencevalue;
large1=sequencevalue;}
if(i==1) 
{if(sequencevalue>small1)small2=sequencevalue;
else{
small2=small1;
small1=sequencevalue;
    }
large2=small1;
large1=small2;
}
if (sequencevalue < small1)
{
    small2 = small1;
    small1 = sequencevalue;
}

if (sequencevalue > large1)
{
    large2 = large1;
    large1 = sequencevalue;
}
}
// Final Output
cout << "Two smallest values: " << small1 << " " << small2 << "\n";
cout << "Two largest values: " << large2<< " " << large1 << "\n";
0
Robin Degerio 26 janv. 2017 à 09:45

Vous voilà

#include <iostream>

int main() 
{
    while ( true )
    {
        // Ask the user to enter the first number
        std::cout << "Sequence Size: ";

        unsigned int loopcount;

        if ( not ( std::cin >> loopcount ) || ( loopcount == 0 ) ) break; 

        int small1, small2, large1, large2;

        // Enter the sequence and start the loop
        std::cout << "\nEnter the Sequence: ";

        unsigned int i = 0;

        for ( ; i < loopcount; i++ )
        {
            int sequencevalue;

            std::cin >> sequencevalue;

            if ( i == 0 )
            {
                small1 = sequencevalue;
                large1 = sequencevalue;
            }
            else
            {
                if ( large1 < sequencevalue )
                {
                    large2 = large1;
                    large1 = sequencevalue;
                }
                else if ( i == 1 || large2 < sequencevalue )
                {
                    large2 = sequencevalue;
                }

                if ( sequencevalue < small1 )
                {
                    small2 = small1;
                    small1 = sequencevalue;
                }
                else if ( i == 1 || sequencevalue < small2 )
                {
                    small2 = sequencevalue;
                }
            }
        }

        if ( i == 1 )
        {
            printf( "There is only one largest value %d\n",
                large1 );
            printf( "And only one mallest value %d\n",
                small1 );
        }
        else
        {
            printf( "The first largest value is %d and the second largest value is %d\n",
                large1, large2 );

            printf( "And the first smallest value is %d and the second smallest value is %d\n",
                small1, small2 );
        }

    }

    return 0;
}

La sortie du programme peut ressembler à

Sequence Size: 10
Enter the Sequence: 1 9 8 0 7 6 5 4 3 2
The first largest value is 9 and the second largest value is 8
And the first smallest value is 0 and the second smallest value is 1
Sequence Size: 0
0
Vlad from Moscow 26 janv. 2017 à 10:12

Votre programme a un comportement indéfini depuis que vous utilisez small1, small2, large1, large2 avant leur initialisation.

La bonne façon d'initialiser small1 et small2 sera:

int small1 = INT_MAX;
int small2 = INT_MAX;

La bonne façon d'initialiser large1 et large2 sera:

int large1 = INT_MIN;
int large2 = INT_MIN;

Mettre à jour

Sans la présence d'un tableau trié, la logique pour mettre à jour correctement les nombres n'est pas simple. Vous trouverez ci-dessous un programme de travail qui calcule correctement small1 et small2. Je vous laisse le soin de l'étendre pour calculer large1 et large2.

#include <iostream>
#include <sstream>
#include <climits>
#include <algorithm>

int main()
{
   std::istringstream str("8 5 8 9 12 -6 4 -8 10");

   int small1 = INT_MAX;
   int small2 = INT_MAX;

   int loopcount;
   bool small1_found = false;

   str >> loopcount;
   for (int i = 0; i < loopcount; ++i )
   {
      int sequencevalue;
      str >> sequencevalue;

      if ( sequencevalue < small1 )
      {
         int temp = small1;
         small1 = sequencevalue;
         if ( small1_found )
         {
            small2 = std::max(sequencevalue, temp);
         }
         small1_found = true;
      }

   }

   std::cout << "Two smallest values: " << small1 << " " << small2 << "\n";
}

Production:

Two smallest values: -8 -6
2
R Sahu 26 janv. 2017 à 08:41

Vous recherchez <climits>

Notez la sortie de ce programme

#include <climits>
#include <iostream>

int main() {
    std::cout << INT_MAX << std::endl;
    std::cout << INT_MIN << std::endl;
    return 0;
}
0
OregonTrail 26 janv. 2017 à 07:58