0

I'm learning C++ currently and wanted to do the following exercise: Requires:

variables, data types, and numerical operators basic input/output logic (if statements, switch statements) loops (for, while, do-while) arrays

Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10) Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.

★ Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people. i.e. Person 4: ate 10 pancakes Person 3: ate 7 pancakes Person 8: ate 4 pancakes ... Person 5: ate 0 pancakes

Sofar I've gotten the "ate most" and "second most", but I get a problem with the "least" part...

Here is my code:

#include <iostream>
using namespace std;
int arrayPersonsPancakes[]={};
int arrayPersonsNr;

int createArrayforPersons();
void pancakesPerPerson(int);
void whoAteTheMost();


int main(){
int arrayPersonsNr = createArrayforPersons();
pancakesPerPerson(arrayPersonsNr);
whoAteTheMost();

/*for(int i=0; i < arrayPersonsNr; i++){
   cout << arrayPersonsPancakes[i] << endl;
}*/

}

int createArrayforPersons(){
cout << "Please enter the Nr. Of Persons who ate breakfast: " << endl;
int nrOfPersons;
cin >> nrOfPersons;
arrayPersonsPancakes[nrOfPersons];
return nrOfPersons;
}

void pancakesPerPerson(int nrOfPersons){
for (int i=0; i < nrOfPersons; i++){
    cout << "Please enter how many pancakes person Nr." << i+1 << " ate: " << endl;
    cin >> arrayPersonsPancakes[i];
    }

}
void whoAteTheMost(){
    int theMost= arrayPersonsPancakes[0];
    int theSecondMost;
    int theLeast;
for(int i = 0; i < arrayPersonsNr; i++){
    if (arrayPersonsPancakes[i] > theMost){
        theSecondMost = theMost;
        theMost = arrayPersonsPancakes[i];
                }
    }

theLeast = theMost;

 cout << theLeast << endl; //Here I get the normal output

for(int y = 0; y < arrayPersonsNr; y++){
    if (theLeast >  arrayPersonsPancakes[y]) {
        theLeast = arrayPersonsPancakes[y];
    }
}

cout << "after for " << theLeast << endl;  //I always get a zero here after the loop and have no idea why.
}

for(int j = 0; j < arrayPersonsNr; j++){
    if (arrayPersonsPancakes[j] == theMost){
        cout << "Person Nr." << j+1 << " ate the most Pancakes: " << theMost << endl;}
    else if (arrayPersonsPancakes[j] == theSecondMost){
        cout << "Person Nr." << j+1 << " ate the second most Pancakes: " << theSecondMost << endl;}
   else if (arrayPersonsPancakes[j] == theLeast){
        cout << "Person Nr." << j+1 << " ate the least Pancakes: " << theLeast << endl;}

    }
}

After the Loop for "theLeast" I always get a zero no matter what I do. I'd appreciate any comments to how I code and how I can improve that (regardless of missing comments currently, sorry about that)

Thank you!

Ars3mR
  • 3
  • 1
  • 3
    Probably better to start with one of [these C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Please take [the tour](https://stackoverflow.com/tour) and read [the help page](https://stackoverflow.com/help). Welcome to SO. – Ron Sep 25 '17 at 16:17
  • 1) What is your input? 2) Did you try stepping through your code with a debugger, to figure out, where your algorithm does something, that you didn't expect? – Algirdas Preidžius Sep 25 '17 at 16:18
  • @Ron Thank you for the welcome and the books recommendation. I've tried to use books, but I find it somewhat hard with c++ books, everything is a bit overcomplicated, so I use the tutorial at learncpp.com.... my plan is to go through the tutorials there and when I get a basic understanding I'll work with a book. – Ars3mR Sep 25 '17 at 19:26
  • @AlgirdasPreidžius I input 4 for the first function and follow it with 10 20 30 40 for each person, everything works properly and I get the "Most" and "Second most" perfectly, but always get a Zero for the "least" – Ars3mR Sep 25 '17 at 19:28
  • @Ars3mR So, pick the book from the linked list. The books are meant to explain everything along the way, and there are plenty of bad books, that don't do that. – Algirdas Preidžius Sep 25 '17 at 19:36
  • 1
    @Ars3mR In addition, on the second look through your code, your code has undefined behavior. `int arrayPersonsPancakes[]={};` Allocates 0-sized array (which is non-standard, but allowed by the use of some compiler extensions), and any subsequent reads/writes to/from it is undefined behavior, due to unallocated memory access (`arrayPersonsPancakes[nrOfPersons];` doesn't allocate the memory - it merely accesses the element at the `nrOfPersons` position). – Algirdas Preidžius Sep 25 '17 at 19:36
  • @AlgirdasPreidžius Thank you, I'll re-write the code again and work on that, I thought I could initialize a null-array and then change its size again, like python for example. – Ars3mR Sep 25 '17 at 20:34
  • @Ars3mR 1) Consider the suggestion of learning from a C++ book, from [this list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) - everything would have been explained in a structured fashion, and you wouldn't have made this mistake. 2) C++ is a different language from Python, why would you ever assume that concepts are exactly the same between different languages? – Algirdas Preidžius Sep 25 '17 at 20:42
  • @AlgirdasPreidžius sometime one just assumes thing based on experience, that's how adults normally learn. – Ars3mR Sep 26 '17 at 17:03
  • @Ars3mR It's a bad way to learn about programming languages based from experience, since it's easy to teach yourself to write bad code, just because "it works". C++ has plenty of cases with undefined behavior, where it might seem to "work" while using one compiler, or architecture, but break with others. It's far more efficient to learn in a structured manner, rather re-learning everything later. You can learn from experience in general day-to-day life (or if you already have some knowledge to build upon), you can't do it effectively in learning technical topics from the beginning. – Algirdas Preidžius Sep 26 '17 at 17:32

1 Answers1

0

You are creating local variable arrayPersonsNr in main function

int main(){    
        int arrayPersonsNr = createArrayforPersons(); // This is local variable
        pancakesPerPerson(arrayPersonsNr);
        whoAteTheMost();
    }

Modify your code to

int main(){
    arrayPersonsNr = createArrayforPersons(); // This is global variable
    pancakesPerPerson(arrayPersonsNr);
    whoAteTheMost();
}
Geek
  • 273
  • 5
  • 19