Çözüldü C++ nasıl Max element gösterilir?

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

RSK06

Hectopat
Katılım
29 Aralık 2020
Mesajlar
2.285
Çözümler
4
Daha fazla  
Cinsiyet
Erkek
[CODE lang="cpp" title="C++"]#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
char selection{};
vector<double> list { 1, 2, 3, 4, 5 };
double new_num{};
double average{};
double total {};
double max = list[0];

do {
cout << "Menu options: \n" << endl;
cout << "P - Print numbers\nA - Add a number\nM - Display mean of the numbers\nS - Display the smallest number\nL - Display the largest number\nQ - Quit\n" << endl;
cout << "Enter your choice: ";
cin >> selection;
if (selection == 'P' || selection == 'p') {
cout << "displaying all the elements in list: [ ";
for (unsigned i = 0 ; i < list.size(); i++)
cout << list << " ";

cout << " ]" << endl;
}
else if(selection == 'A' || selection == 'a') {
cout << "Add integer: ";
cin >> new_num;
cout << "Adding new number to the list: [ ";
list.push_back(new_num);
cout<< "Showing the updated list: ";
for (unsigned i = 0 ; i < list.size(); i++)
cout<<list << " ";
cout << " ]" << endl;

}
else if (selection == 'M' || selection == 'm') {
cout << "\nCalculating average of the list... " << endl;
for (auto ttl: list) {
total += ttl;
}
average = total / list.size();
cout << "\nThe Average is: " << average << "\n" << endl;
}

else if (selection == 'L' || selection == 'l') {
for (int i = 0; i < 5; i++) {
if (list > max) {
max = list;
cout << "\nThe largest number in the list is: " << max << "\n" << endl;
}
}
}

} while (selection != 'q' && selection != 'Q');
cout << endl;

return 0;
}[/CODE]

Burada largest ve smallest number'ı nasıl belirtebilirim?
 
Son düzenleyen: Moderatör:
Kod:
int min = list[0];
for(i = 0; i < 5; i++){
        if(list[i] < min){
            min = list[i];
        }
    }

    cout  << "Minimum Element\n" << min;
Maximum için yaptığın şeyin aynısını minimum için de yapman gerekiyor. Senin kodunda for döngüsünün içinde hiçbir şey yok şu an.
O zaman buradaki 5'i listenin eleman sayısıyla değiştirmek gerekiyor. Çünkü şu anki durumda en son eklenen elemanları karşılaştırmaya katmıyor. Eleman eklediğin yere kaç eleman eklendiğini saymak için bir sayaç değişkeni koyup burayı da for(i=0 i<5+sayac;i++) olarak düzenleyebilirsin. Böylece listenin uzunluğu kadar karşılaştırma olacaktır.
 

Yeni konular

Geri
Yukarı