Beginner Excercise Feedback and Help

Hi everyone, I'm doing the beginner excercises found here. http://www.cplusplus.com/articles/N6vU7k9E/
I've got the first 3 done and id like some feedback on them. However, I'd also like some help on the pancake glutton program ive got the first modification but i'm having a lot of toruble displaying the array in descending order. Here are my programs.

Grade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main() {
    int grade;
    std::cout << "What grade did you get in programming?" << "\n I got a ";
    std::cin >> grade;
    if(grade == 100) std::cout << "You got a perfect score!\n";
    if(grade <= 100 && grade >= 90) std::cout << "Letter grade: A (90 - 100)";
    if(grade <= 89 && grade >= 80) std::cout << "Letter grade: B (80 - 89)";
    if(grade <= 79 && grade >= 70) std::cout << "Letter grade: C (70 - 79)";
    if(grade <= 69 && grade >= 60) std::cout << "Letter grade: D (60 - 69)";
    if(grade <= 59 && grade >= 0) std::cout << "Letter grade: F (0 - 59)";



return 0;
}


Cola
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main() {
    std::cout << "What's your favorite beverage? (Coke, Pepsi, Sprite, Hi C, Big Red)";
    int bev;
    std::cin >> bev;
    switch(bev) {
        case 1: std::cout << "You're favorite is Coke" << std::endl;
            break;
        case 2: std::cout << "You're favorite is Pepsi" << std::endl;
            break;
        case 3: std::cout << "You're favorite is Sprite" << std::endl;
            break;
        case 4: std::cout << "You're favorite is Hi C" << std::endl;
            break;
        case 5: std::cout << "You're favorite is Big Red" << std::endl;
            break;
        default: std::cout << "That wasn't a valid choice" << std::endl;

    return 0;
    }


Gullible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main() {
    int num = 0;
    int times = 0;
    int ans = 0;
    do {
    std::cout << "Enter any number other than " << times << ": ";
    std::cin >> num;
    ans = times;
    times++;
    } while(num != ans && times != 10);

    if(num == ans) std::cout << "\nYou were not supposed to enter " << ans << "!\n";
    if(times == 10) std::cout << "\nWow you're really patient!\n";






    return 0;
}


Pancake
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
#include <iostream>
#include <string>
#include <vector>

int main() {
    int personNum = 1; //person number
    int pancakeNum = 0; // how many pancakes they eat
    std::vector <int> pancakesEaten; //array of the number of pancakes eaten
    while(personNum <= 10) {
        std::cout << "How many did person " << personNum << " eat: ";
        std::cin >> pancakeNum;
        pancakesEaten.push_back(pancakeNum);
        personNum++;
    }
    int maxPan = pancakesEaten[0];
    int leastPan = pancakesEaten[0];
    int maxIndex = 0;
    int leastIndex = 0;
    for(int i = 0; i < pancakesEaten.size(); i++) {
        //std::cout << pancakesEaten[i] << "\n";
        if(pancakesEaten[i] > maxPan) {
            maxPan = pancakesEaten[i];
            maxIndex = i;
        }
        if(pancakesEaten[i] < leastPan) {
            leastPan = pancakesEaten[i];
            leastIndex = i;
        }
    }
    std::cout << "Person " << maxIndex + 1 << " ate the most! They ate " << pancakesEaten[maxIndex] << ".\n";
    std::cout << "Person " << leastIndex + 1 << " ate the least! They ate " << pancakesEaten[leastIndex] << ".";




    return 0;
}


I tried to find the max number in the array, output who ate that many and then delete it from the array and do it again (EX. 1 2 34 5 6 9. fine 34 out put person 3 ate 34 then delete it and then it would find the max again which would be 9) but my indexes all screwed up and i know there has to be a batter way to do it.

Heres the screwed up pancake one
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
#include <iostream>
#include <string>
#include <vector>

int main() {
    int person = 1; // which person
    int numpan = 0; // how many pancakes that person ate
    std::vector <int> pancakes;
    while(person <= 10) {
        std::cout << "How many pancakes did person " << person << " eat? ";
        std::cin >> numpan;
        pancakes.push_back(numpan);
        std::cout << '\n';

        person++;
    }
    int times = 0;
    do{
        int mostpancakes = pancakes[0];
        int mostindex = 0;
        for(int i = 0; i < pancakes.size(); i++) {
            if(pancakes[i] > mostpancakes) {
                mostpancakes = pancakes[i];
                mostindex = i;
            }
        }
        std::cout << "Person " << mostindex + 1 << " ate " << pancakes[mostindex] << '\n';
        pancakes.erase(pancakes.begin()+ mostindex);
        times++;
    }while(times < 10);
    //std::cout << "Person " << mostindex << " ate the most pancakes!\n";
    //std::cout << "Person " << leastindex << " ate the least pancakes!";
    return 0;
}


Please help thanks!
Last edited on
Hi,

i'm having a lot of trouble displaying the array in descending order.


you need to know about selection sort, bubble sort and insertion sort

http://theoryapp.com/selection-insertion-and-bubble-sort/

firstly try to understand what exactly does the code does

it would be easier to start with bubble sort as its short and sweet and straightforward
Topic archived. No new replies allowed.