My Pancake Program c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <cmath>
#include <string>

void getArray(int tempArr[], int arrSize);
void printArray(int tempArr[], int arrSize);
int calculateHighestArray(int tempArr[],int arrSize);
int calculateLowestArray(int tempArr[],int arrSize);

int main(){
    int people, min, max;
    std::cout << "Enter the number of people " << std::endl;
    std::cin >> people;
    int pancakes[people];
    std::cout << "Enter the number of pancakes for the " << people << " people." << std::endl;
    getArray(pancakes, people);
    printArray(pancakes, people);
    max = calculateHighestArray(pancakes, people);
    std::cout << "The highest number of pancakes ate was " << max << std::endl;
    min = calculateLowestArray(pancakes, people);
    std::cout << "The lowest number of pancakes ate was " << min << std::endl;

}
//The below functions deal with input and output from arrays. All output of array functions print to console. 
void getArray(int tempArr[], int arrSize){
    for(int i = 0;i < arrSize;i++){
        std::cin >> tempArr[i];
    }

}

void printArray(int tempArr[], int arrSize){
    for(int i = 0;i < arrSize;i++){
       std::cout << "Person " << i+1 << ": " << std::endl;
       std::cout << tempArr[i] << std::endl;
    }

}
int calculateHighestArray(int tempArr[],int arrSize){
    int maxValue = tempArr[0];
        for(int i=1;i<arrSize;i++){
            if(tempArr[i] > maxValue){
                maxValue = tempArr[i];

            }
        }
    return maxValue;
}
int calculateLowestArray(int tempArr[],int arrSize){
    int minValue = tempArr[0];
        for(int i=1;i<arrSize;i++){
            if(tempArr[i] < minValue){
                minValue = tempArr[i];

            }
        }
    return minValue;
}


Everything works great and it all runs ok I was just wondering if there is anything I'm doing that is bad practice.
If you wanted, you could add "using namespace std;" after the header inclusions. This way you don't have to use "std::" for the standard C++ functions.

Otherwise, it's a very good example of clean coding. (At least in my opinion.) The only other thing you could consider, which is optional, is to "prettify" your code.

This is how my instructor taught me:
1
2
3
4
5
6
7
8
9
10
11
12
13
//  ========================================================
    functionType FunctionName( identifierType identifier ) {

//      ======================
//      Variable Declarations.
//      ======================
        int someNum = 1234;
//      ===================

//      Your code and whatnot.

    } // function FunctionName()  You can also do this for: //if, //else, //switch, etc.
//  ============================  


Again, this is completely optional, as your code is already well laid out.
Just outta curiosity, how did your line 14 work? I thought C++ does not allow an array declaration with variable in it, and you have to use dynamic allocation?
Topic archived. No new replies allowed.