Felipe ZC
Felipe ZC

Reputation: 27

How to compare the value inside of an array

So Im writing a program that asks the user to input the number of pancakes a person(1-10) had for breakfast. The program must analyze the input and determine which person ate the most pancakes. Also the program must output a list in order of number of pancakes eaten of all 10 people. So far I have written the code to get the user input and the code to display the array, but not in order. Im completely lost when it comes to comparing the elements in the array:

int getPancakes();
void displayArray(int theArray[],int sizeOfArray);
void compareArray(int sizeOfArray);
int pancakes[10];
int z = 0;

int main()
{
}

int getPancakes(){
    int y;
    int x = 0;

    for(int y = 0; y < 10; y++){
        ++x;
        cout << "How many pancakes did person " << x << " eat?" << endl;
        cin >> pancakes[y];
    }
}

void displayArray(int theArray[],int sizeOfArray){
    for(int x = 0 ;x < sizeOfArray ; x++){
        ++z;
        cout << "Person " << z << " ate " << pancakes[x] << " pancakes" << endl;
    }
}

So how can I instruct my program to compare the elements inside the array? Also how can I instruct my program to print the list of number of pancakes eaten by each person in order?

Upvotes: 1

Views: 24417

Answers (5)

gretzki
gretzki

Reputation: 13

I just finished coming up with a solution to the same exercise, regarding the "who ate the most" part, even if there is more than one person who ate the most pancakes. It involves arrays and vectors. Tested and working:

#include <iostream>
#include <array>
#include <vector>
using namespace std;

int main()
{
    array <int, 10> PancakeEater;
    vector<int> Winners;
    int max;


    cout << "Type number of pancakes eaten, separated by spaces, for each of the ten contestants:" << endl;
    cout << "#1 #2 #3 #4 #5 #6 #7 #8 #9 #10" << endl;

    for (int i = 0; i < PancakeEater.size(); i++)
    {
        cin >> PancakeEater[i];
    }

    max = PancakeEater[0];
    Winners.push_back(1);
    for (int i = 0; i < PancakeEater.size()-1; i++)
    {

        if (max == PancakeEater[i + 1]) 
        {
            Winners.push_back(i + 2);
        }
        if (max < PancakeEater[i + 1])
        {            
            max = PancakeEater[i + 1];
            Winners.clear();
            Winners.shrink_to_fit();
            Winners.push_back(i + 2);
        }
    }
    if (Winners.size() > 1) 
    {
        cout << endl << "The contestants that ate the most pancakes, on a tie for first place, are contestant numbers " << endl;
        for (auto item : Winners) 
        {
            cout << item << " ";
        }
    }
    else
    {
        cout << endl << "The contestant that ate the most pancakes is contestant number " << endl;
        for (auto item : Winners)
        {
            cout << item << " ";
        }
    }
}

Upvotes: 0

Baldrickk
Baldrickk

Reputation: 4409

There is no need to run through the array looking for max then sorting to provide output. Instead, you can just keep track of the max value you have found during the input phase:

int getPancakes(){
    int max = 0;
    for(int y = 0; y < 10; y++){
        cout << "How many pancakes did person " << y << " eat?" << endl;
        cin >> pancakes[y];
        if (pancakes[y]>pancakes[max]){
            max = y;
        }
    }
    return max;
}

Note that I removed the redundant declaration of y (you are declaring it in the for loop) and x (was always going to be equal to y).
I also added a return to the function (index of the person who has eaten the most) as you had no return value. (Either return something or make the function return void (no return)).
If you only care about the max number eaten, then you don't even need to keep track of max. Instead, just read the largest value from the array after the sorting step.

Now all you need to do is implement void sortArray() and call it before calling the display function:

int main()
{
  int max = getPancakes();
  sortArray();             //left to you to implement
  displayArray(pancakes,10);
}

You may want to consider making pancakes local to main and passing it into your functions in the same way that you are doing displayArray.

Upvotes: 0

Paul92
Paul92

Reputation: 9062

In order to find who ate the most pancakes, you basically need to find the position of the maximum value in the array.

int findMaxPosition(int array[], int arraySize){
    int maxPosition = 0;     //assume the first element is maximum
    for(int i = 1; i < arraySize; i++)
        if(array[i] > array[maxPosition]) //compare the current element with the known max
            maxPosition = i;   //update maxPosition
    return maxPosition;
}

Note that this gives you the first occurence of the maximum value. If the elements are unique, that's enugh. Otherwise, you should find the maximum value, array[maxPosition], and iterate through the array and display every position on which it occurs.

Sorting is a little bit complicated. The sorting algorithms aren't so straightforward, and I'm afraid that if I write you an implementation, I wouldn't help you.

One of the simplest sorting algorithms is bubble sort. Wikipedia (http://en.wikipedia.org/wiki/Bubble_sort) has a detailed page about it, and you should be able to implement it using the pseudocode given there.

Upvotes: 1

Thomas Matthews
Thomas Matthews

Reputation: 57678

To be blunt, there are two methods for comparing an element of an array to another value, direct and through a copy.

// Make a copy:
  int count = pancakes[x];
  if (count == limit)
  {
    //...
  }

// Direct access
  if (pancakes[x] == limit)
  {
    //...
  }

Upvotes: 0

Jablonski
Jablonski

Reputation: 18504

If all numbers are unique

int max = 0;
for(int x = 0 ;x < 10 ; x++)
{
    if(pancakes[x] > max)
        max = pancakes[x];
}
for(int x = 0 ;x < 10 ; x++)
{
    if(pancakes[x] == max)
         cout << "Person " << x << " ate " << pancakes[x] << " pancakes - biggest number" << endl;
}

Upvotes: 0

Related Questions