Devin Dahlberg

C++ : Assignment Four

// This program asks for the user to enter one to twenty-five names and determine which name is first and last alphabetically and lists the names in order.
//
//
//Program from Starting Out With C++ from Control Structures through Objects (Ninth Edition) by Tony Gaddis(2018) pg. 299, Problem #14.
//
// Programmed by Devin Dahlberg, CIS 251 Student
// February 5, 2024

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string roster[25];
    int roster_count = 0;

    //Get the number of students.
    cout << "How many students do you wish to enter? (1-25) " << endl;
    cin >> roster_count;
    while (roster_count < 1 || roster_count > 25) {
        cout << "Invalid amount of students. " << endl;
        cout << "How many students do you wish to enter? (1-25) " << endl;
        cin >> roster_count;
    }

    //Gets the appropriate amount of students required by the user.
    cout << "Enter the names of the " << roster_count << " students: " << endl;

    for (int count = 0; count < roster_count; count++) {
        cin >> roster[count];
    }

    //Sorts the students in alphabetical order.
    for (int count = 0; count < roster_count; count++) {
        for (int countTwo = count + 1; countTwo < roster_count; countTwo++) {
            if (roster[count] > roster[countTwo]) {
                string current = roster[count];
                roster[count] = roster[countTwo];
                roster[countTwo] = current;
            }
        }
    }

    //Lists the first and last student in line.
    cout << "The first student in line is " << roster[0] << "." << endl;
    cout << "The last student in line is " << roster[roster_count - 1] << "." << endl;

    //Lists the students in alphabetical order.
    cout << "The students in alphabetical order: " << endl;
    for (int count = 0; count < roster_count; count++) {
        cout << count + 1 << ". " << roster[count] << endl;
    }

    return 0;
}

image

image