In: Computer Science
Use psuedo-code for the following:
1) Assign a new student into the correct desk in a room of students seated in alphabetical order. (Sort by just last name)
Here is the pseudocode for doing the above task:
Ask for the number of students in the class, let it be n
string array studs[n] to hold n students
for i=0 to n:
input studs[i] in format lastname<space>firstname
Now apply bubble sort to the list of students
for i=0 to n-1:
for j=0 to n-i-1:
if studs[j]>studs[j+1] alphabetically:
swap(studs[j],studs[j+1])
print students according to the seat numbers
for i=0 to n:
print: seat i - studs[i]
Here is a c++ program to do that:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int n, i, j;
cout << "Enter number of students: ";
cin >> n;
getchar();
string studs[n];
cout << "Enter " << n << " students name (lastname, firstname)\n";
for (i = 0; i < n; i++)
getline(cin, studs[i]);
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (studs[j] > studs[j + 1])
{
string temp;
temp = studs[j];
studs[j] = studs[j + 1];
studs[j + 1] = temp;
}
for (i = 0; i < n; i++)
{
cout << "Seat " << i + 1 << " - " << studs[i] << "\n";
}
return 0;
}
Here is the output of above code: