[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];
double min = 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 == 'S' || selection == 's') {
for(int i = 0; i < list; i++){
if(list < min){
min = list;
}
}
cout << "\nThe smallest number in the list is: " << min << "\n" << endl;
}
else if (selection == 'L' || selection == 'l') {
for (int i = 0; i < list; 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]
Kod bu şekilde yapmam gereken şey list boş ise "cout << "empty list" demem fakat nasıl boş liste oluşturacağım bilmiyorum. Mesela p seçince list boş ise "empty list" demesi lazım. Daha sonra push back ile sayı eklediğim zaman artık sadece sayıyı göstermeli.
#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];
double min = 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 == 'S' || selection == 's') {
for(int i = 0; i < list; i++){
if(list < min){
min = list;
}
}
cout << "\nThe smallest number in the list is: " << min << "\n" << endl;
}
else if (selection == 'L' || selection == 'l') {
for (int i = 0; i < list; 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]
Kod bu şekilde yapmam gereken şey list boş ise "cout << "empty list" demem fakat nasıl boş liste oluşturacağım bilmiyorum. Mesela p seçince list boş ise "empty list" demesi lazım. Daha sonra push back ile sayı eklediğim zaman artık sadece sayıyı göstermeli.
Son düzenleyen: Moderatör: