ciao sono uno studente delle superiori e mi servirebbe aiuto a trovare l'errore in un programma che sto sviluppando (il compiler non restituisce errori o avvisi).
in pratica l'utente fornisce 2 liste di 10 numeri ciascuna e il programma restituisce altre 2 liste con i numeri maggiori e minori in ciascuna posizione. Es. l'utente scrive:
lista 1:
1
2
50
66
lista 2:
4
1
49
70
il programma dovrebbe restituire:
lista 3 (i numeri maggiori):
4
2
50
70
lista 4 (i minori):
1
1
49
66
la prima parte del programma funziona ma quando il computer restituisce le due liste con i numeri maggiori e minori mi da come risultati dei numeri totalmente random e non riesco a capire perché. ho provato a vedere se c'era qualcosa di sbagliato con gli array ma non sono riuscito a trovare niente. Se qualcuno mi desse una mano anche solo indicandomi cosa sto sbagliando mi farebbe un grande favore. vi allego sotto il codice c++:
codice:
#include <iostream>
#include <cmath>
int main()
{
int i, a, b;
int list1[10];
int list2[10];
int list3[10];
int list4[10];
cout << "this program compares 2 lists of 10 positive numbers and gives you 2 other lists containing every bigger and lower number in each position";
cout << "write the INTEGER POSITIVE numbers of the first list\n";
for (i = 0; i < 10; i++)
{
cout << "number " << i+1 << " of 10" << endl;
cin >> a;
list1[i] = a;
}
cout << "write the INTEGER POSITIVE numbers of the second list\n";
for (i = 0; i < 10; i++)
{
cout << "number " << i+1 << " of 10" << endl;
cin >> b;
list1[i] = b;
}
cout << "compiling the list containing all the bigger numbers in each position.\n";
for (i = 0; i < 10; i++)
{
cout << "compiling " << i + 1 << " of 10\n";
if (list1[i] < list2[i])
list3 [i] = list2 [i];
else if (list1[i] > list2[i])
list3 [i] = list1 [i];
else if (list1[i] == list2 [i])
{
cout << "founded two equal values at the position" << i <<". the number will be present in each list.\n";
list3 [i] = list1 [i];
}
else
{
cout << "error: unvalid value\n";
}
}
cout << "completed.";
cout << "compiling the list containing all the lower numbers in each position\n";
for (i = 0; i < 10; i++)
{
cout << "compiling " << i + 1 << " of 10\n";
if (list1[i] < list2[i])
list4 [i] = list1 [i];
else if (list1[i] > list2[i])
list4 [i] = list2 [i];
else if (list1[i] == list2 [i])
{
cout << "founded two equal values at the position" << i <<". the number will be present in each list.\n";
list4 [i] = list1 [i];
}
else
{
cout << "error: unvalid value\n";
}
}
cout << "completed.\n";
cout << "list containing all bigger numbers\n";
for (i = 0; i < 10; i++)
{
cout << list3 [i] << endl;
}
cout << "list containing all lower numbers\n";
for (i = 0; i < 10; i++)
{
cout << list4 [i] << endl;
}
return 0;
}
grazie di ogni aiuto