简体   繁体   中英

C++ array beginner

I have this code that I have been working on for fun, it is as basic as it gets, becuase I am a beginner, and it works fine, but I can't seem to be able to figure out how to make it show the least and the most amount of pancakes ate. Thank you a lot in advance.

#include <iostream>
using namespace std;

int main(){

   int pancakes[10];
   int x,i;

    cout << "Hello user!" << endl;
    cout << endl;
    cout << "Please enter how many pancakes did each of the 10 people eat:" << endl;
    cout << endl;

    for (i=0;i<10;i++ ){
        cin >> x;
        pancakes[i]=x;
    }

    cout  << "1st person ate" << "  " << pancakes[0] << " " << "pancakes" << endl;
    cout  << "2nd person ate" << "  " << pancakes[1] << " " << "pancakes" << endl;
    cout  << "3rd person ate" << "  " << pancakes[2] << " " << "pancakes" << endl;
    cout  << "4th person ate" << "  " << pancakes[3] << " " << "pancakes" << endl;
    cout  << "5th person ate" << "  " << pancakes[4] << " " << "pancakes" << endl;
    cout  << "6th person ate" << "  " << pancakes[5] << " " << "pancakes" << endl;
    cout  << "7th person ate" << "  " << pancakes[6] << " " << "pancakes" << endl;
    cout  << "8th person ate" << "  " << pancakes[7] << " " << "pancakes" << endl;
    cout  << "9th person ate" << "  " << pancakes[8] << " " << "pancakes" << endl;
    cout  << "10th person ate" << " " << pancakes[9] << " " << "pancakes" << endl;

    return 0;
}

You can use min_element and max_element from the standard library to do this:

#include <algorithm>

cout << "The smallest number of pancakes was " << *min_element(pancakes, pancakes + 10) << endl;
cout << "The largest number of pancakes was "  << *max_element(pancakes, pancakes + 10) << endl;

Since you are a beginner, I will put a simple solution using a loop.

int max = 0;
for(i = 0; i < 10; i++) {
    if(pancakes[i] > max) max = pancakes[i];
}
cout << "Most amount of pancakes eaten by a single person: " << max << endl;

Firstly, instead of having around 10 cout's , you can use a loop to print them :

for (i=0;i<10;i++ ){

    if(i==0)
        cout  <<i+1<< "st person ate" << "  " << pancakes[i] << " " << "pancakes" << endl;
    else if(i==1)
        cout  << i+1<<"nd person ate" << "  " << pancakes[i] << " " << "pancakes" << endl; 
    else if(i==2) 
        cout  << i+1<<"rd person ate" << "  " << pancakes[i] << " " << "pancakes" << endl;
    else 
        cout  <<i+1<< "th person ate" << "  " << pancakes[i] << " " << "pancakes" << endl;
 }

Secondly, you can directly enter values into your array, no need for an intermediate variable x :

for (i=0;i<10;i++ ){

    cin >> pancakes[i];
}

For your max and min problem, take two variables, say - max and min . Initialise them to any arbitrary smallest (say 0, if you are not dealing with negative numbers) and largest value (say, INT_MAX) respectively. Alternatively, you can initialise them to the first element of your array.

For finding max and min, you can traverse the entire array, while checking if the elements are greater or lesser than your max and min variables. If they are, then assign them to your variables:

for (i=0;i<10;i++ ){

if(pancakes[i]>max)
  max = pancakes[i];

if(pancakes[i]<min)
  min = pancakes[i];
}

You can add std::cout inside for loop like below,

for (int i = 0; i < 10; i++ )
{
   std::cin >> pancakes[i];
   std::cout << i+1 <<"st person ate" << "  " << pancakes[i] << " " << "pancakes" << std::endl;
}

int maxpancakes = pancakes[0];
int minpancakes = pancakes[0];

for(int i = 0; i < pancakes.length(); i++ )
{
    if( pancakes[i] < minpancakes ) 
       minpancakes = pancakes[i];
    if( pancakes[i] > maxpancakes ) 
       maxpancakes = pancakes[i];
} 

std::cout << "The smallest pan cake had is :" << minpancakes << std::endl;
std::cout << "The max pan cake had is :" << maxpancakes  << std::endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM