You are on page 1of 8

Alan Totten Design Document hw7.

cpp

Part 1.) Structure of Code in Main function

The variables I have in the int main function include:
string command;
string name[20];
int section[20];
int grade[20];
int drop;
int counter = 0;
string column;
int highGrade;
int lowGrade;
The string command variable stores the command that the user enters after
answering the statement, cout << "Enter a command (add, print, drop, sort, average,
quit):\n";
The array for string name [20] is used to store the names entered by the user when they
use the add command. The spreadsheet can only fit up to 20 student records, which is
why its size is 20.
The array for int section[20] stores the section numbers that is asked for the
user to input in the add function as well. Since, the spreadsheet can only fit up to 20
student records, the size of the array is 20.
The array for int grade[20] stores the grades for each individual student name
that is entered in the add function. The spreadsheet can only fit up to 20 student
records, which is why the size of the array is 20.
The integer drop variable represents the row that the user wishes to be dropped
from the spreadsheet table. The variable is an integer because the row(s) are integer
numbers and cannot be decimals.
The integer counter variable keeps track of how many student records the user
enters. The counter starts at 0 because 0 student records are entered at first and is
incremented in the add function or decreased in the drop function.
The string variable titled column is utilized when the program asks the user "By
which column (name, section, grade)?\n";. The column is then inputted and based off of
what was stated by the user that specific column will be sorted.
EXTRA CREDIT-- The integer variables for highGrade and lowGrade are used to
determine the range of the grades. The students with the highest and lowest grades have
there grade saved to the variable.
My program starts off asking the user to, Enter a command (add, print, drop,
sort, average, quit):\n;. The command is inputted by the user and saved by the program
as it enters a while loop.
Now, while (command != quit), the program will execute the specific
command that is entered by the user.
If (command == add), the program will do one of two things, if the counter that
indicates how many student records have been inputted equals 20 then the program will
output, Spreadsheet is full. Can not add.\n;. Else the program will ask the user to,
Enter name section grade:\n in that exact order. The name is a string while the section
and grades are integer numbers. The name[counter], section[counter], and grade[counter]
will be inputted so each student is matched with there particular section and grade. Also,
at the same time the variable for counter = 0 is being added by 1.
Else if the command the user enters is print then I will call the print function,
which will be explained in detail later. The function is called by (name, section, grade,
counter). This ensures that the program will print the name, section, and grades of each
individual student record that is recorded in the counter.
Else if the user enters the drop command, the program waits for the user to
enter which row they wish to drop which is represented in the int drop variable. Then,
the program will do one of two things. If, the row entered by the user is less than 0 or
greater than the counter variable, which represents how many student records have been,
inputted the program will output, No such row. Can not drop row << row << \n. As
stated earlier the row indicates the row number that is entered by the user which
determines which student record to delete. Else when the row that has been chosen to be
dropped can be dropped my program will enter a for loop. The point of the loop is to
make sure that the row that is being asked to drop, drops the name, section, and grade of
the students record. Then, the loop is closed and the counter that, indicates how many
student records are inputted is decreased by 1.
Else if, the command entered is average the program calls upon the average
command, which will be discussed in detail in Part 2. The command calls based on the
grades that have been entered of the students and all of the counters meaning it use each
student that has been entered and not dropped.
Else if, the command entered by the user is sort the program asks the user, By
which column (name, section, grade)?\n. The user enters the column, which is inputted
by the program which will do one of four options. If (column == name) the program
calls the stringBubbleSort function which sorts string variables such as names. If (column
== section) the program calls the numberBubbleSort function which is used to sort
integer variables such as the sections of the students. Ifcolumn == grade) the program
calls the numberBubbleSort function and sorts the grades of the students. If the column
the user enters is neither, name, section, or grade then the program outputs, No such
column. Can not sort.\n;
Else any other command is invalid and the program outputs, Invalid command.
The at the end of the while loop the program outputs Enter a command (add, print,
drop, sort, average, quit).
Now the while loop is closed due to the user entering the quit command and
the program outputs, Thanks for using spreadsheet. Byebye.

Part 2.) Functions

The void print function consists of the (const stringto names[], const int
sections[], const int grades[], int size). The arrays are constant because the student record
the user enters cannot be manipulated or altered. It can only be sorted or dropped
(deleted). The function prints the number of records that the user has entered so far.
The void average function consists of the variables, (const int grade[], int
counter). The array is constant because the program has to keep the integrity of the
grades array. The counter variable indicates how many different grades have been
entered. Therefore, if (counter == 0) then the average is 0. The average and total
variables are doubles because they can possibly be decimals. Else the function enters a
for loop. The loop initializes int z = 0. As long as z is less than the counter, z = z + 1.
This ensures that each grade the user entered will be used in the average function. The
total = total which is zero + grade[z]. So the total is all of the grades added together. The
average is finally calculated by total / counter. Which is the sum of all of the grades
entered divided by the number of students entered in the spreadsheet. The program then
outputs, Average grade = << average. The function is a void function because it
doesnt return anything just the average.
The void swap function swaps integer values for my program. It works as any
other swap function our class has used. An int temp value is initialized to equal
source[i] and is swapped with source[j] and source[j] is then swapped with temp. This
function swaps the array values of source[i] to become source[j]. This is a void function
because it doesnt return anything it just swaps the integer values.
The void wordSwap function is the exact same as the swap function except it
swaps string values. The function is void because it doesnt return any values just swaps
the string variables. The main difference is the temp is now a string value and so is the
string source array.
The void stringBubbleSort function is used to sort string values such as the names
of that the user enters. The function uses variables such as, (string source[], int size, int
section[], int grade[]). The function ensures that the string variables are sorted. The
function calls the swap and wordSwap functions as mentioned previously to swap integer
and string values respectively. The function is a void function because it doesnt return
anything it just sorts the string variable name.
The void numberBubbleSort function is utilized to sort integer values such as the
sections and grades of the students. The function uses variables such as, (int source[], int
size, string name[], int section[]). The function sorts integer values and calls on the swap
function to move the functions around and orders them through a for loop to make sure
the sorting is done correctly.
EXTRA CREDITThe range function calculates the range by subtracting the
lowest grade from the highest grade as indicated by the student records. This lets the user
understand the parity between two separate students gradewise.

"#$% &'( !"#$%&'

Testing add and print commands:
Enter a command (add, print, drop, sort, average, quit):
add
Enter name section grade:
Alan 4 10
Enter a command (add, print, drop, sort, average, quit):
add
Enter name section grade:
Bob 3 20
Enter a command (add, print, drop, sort, average, quit):
add
Enter name section grade:
Cat 3 30
Enter a command (add, print, drop, sort, average, quit):
add
Enter name section grade:
Dan 2 40
Enter a command (add, print, drop, sort, average, quit):
add
Enter name section grade:
Eli 2 50
Enter a command (add, print, drop, sort, average, quit):
print
[0]: Alan 4 10
[1]: Bob 3 20
[2]: Cat 3 30
[3]: Dan 2 40
[4]: Eli 2 50

Testing print and drop commands:
Enter a command (add, print, drop, sort, average, quit):
drop
4
Enter a command (add, print, drop, sort, average, quit):
print
[0]: Alan 4 10
[1]: Bob 3 20
[2]: Cat 3 30
[3]: Dan 2 40

Testing average command:
[0]: Alan 4 10
[1]: Bob 3 20
[2]: Cat 3 30
[3]: Dan 2 40
Enter a command (add, print, drop, sort, average, quit):
average
Average grade = 25

Testing sort command:
Enter a command (add, print, drop, sort, average, quit):
sort
By which column (name, section, grade)?
name
Enter a command (add, print, drop, sort, average, quit):
print
[0]: Alan 4 10
[1]: Bob 3 20
[2]: Cat 3 30
[3]: Dan 2 40
Enter a command (add, print, drop, sort, average, quit):
sort
By which column (name, section, grade)?
section
Enter a command (add, print, drop, sort, average, quit):
print
[0]: Dan 2 40
[1]: Bob 3 20
[2]: Cat 3 30
[3]: Alan 4 10
Enter a command (add, print, drop, sort, average, quit):
sort
By which column (name, section, grade)?
grade
Enter a command (add, print, drop, sort, average, quit):
print
[0]: Alan 4 10
[1]: Bob 3 20
[2]: Cat 3 30
[3]:Dan 2 40

Testing Invalid command:
Enter a command (add, print, drop, sort, average, quit):
bob
Invalid command.

Testing quit command:
Enter a command (add, print, drop, sort, average, quit):
quit
Thanks for using spreadsheet. Byebye.
Alans-MacBook-Pro:Tottencmpsc101 alantotten$

Extra Credit-- Testing range command:
Enter a command (add, print, drop, sort, average, quit):
range
Enter the lowest and highest grades as indicated by the student
records:
10 25
Range equals 15

You might also like