Module 4: Algorithms and Data Structures Flashcards
What is pseudocode?
English like syntax; close to actual programming code; used to design algorithms prior to writing actual code.
What are the main reasons (4) for using pseudocode?
- Ensures you understand the problem that you are trying to solve.
- Helps you visualize your solution without getting lost the process of writing proper code syntax and structure.
- Helps you write multiple variations of an algorithm that can be used for evaluation purposes.
- You might want to present the algorithm to the customer or to the management for evaluation against intended outcomes with burdening them with technical code that they may not be able to read.
What are the 4 simple rules to remember when writing pseudocode?
- Use good English in code.
- Avoid unnecessary detail (no punctuation or braces).
- Do not state the obvious. Data types do not need to be declared if it is clear from the text.
- Use programming short hand. EX. if-the-else
What is an algorithm?
A step- by- step procedure for solving a problem. Used creatively to ensure considerations for simplicity and performance are taken.
What should the pseudocode look like?
PROBLEM STATEMENT:
The instructors would like to be able to enter a finite number of grades into the system and have it calculate the grade average. The number of grades to be input is not known ahead of time so there must be a way for the instructor to signal the end of input. Average is calculated by totaling all the grades entered and dividing that total by the number or entered grades. Grades may or may not be whole numbers.
PSEUDOCODE: Initialize total to zero Initialize average to zero Initialize counter to zero Input the first grade while the user has not as yet entered the sentinel add this grade into the running total add one to the grade counter input the next grade (possibly the sentinel) if the counter is not equal to zero set the average to the total divided by the counter print the average else print 'no grades were entered'
What should the code implementation look like?
PSEUDOCODE: Initialize total to zero Initialize average to zero Initialize counter to zero Input the first grade while the user has not as yet entered the sentinel add this grade into the running total add one to the grade counter input the next grade (possibly the sentinel) if the counter is not equal to zero set the average to the total divided by the counter print the average else print 'no grades were entered'
double grade = 0;
double total = 0;
double average = 0;
int gradeCounter = 0;
Console.Out.WriteLine(“Enter first grade”);
grade = Double.Parse(Console.ReadLine());
while(grade != 200)
{
total = total + grade;
gradeCounter++;
Console.Out.WriteLine(“Enter another grade or enter 200 to end.”);
grade = Double.Parse(Console.ReadLine());
}
if(gradeCounter != 0)
{
average = total / gradeCounter;
Console.Out.WriteLine(“Grade average is: “ + average);
}
else
{
Console.Out.WriteLine(“No grades were entered.”);
}
What is a bubble sort?
Finding a lower value on a list and bubbling it up to the top of the list.
What is the logic for a bubble sort?
- Get the size of the list.
- Create a pointer to the first value.
- Get the value referenced by the pointer.
- Get the value one greater than the pointer.
- Compare the two values.
- If the next value is smaller, swap the values.
- Increment the value pointer.
- Repeat steps 3 to 7 until you have reached the end of the list.
Logic represented in code: int temp = 0; for(int i = 1; i <= (myArray.Length - 1); i++) { for(int j = 0; j < (myArray.Length - 1); j++) { if(myArray[j + 1] < myArray[j]) {
temp = myArray[j]; myArray[j] = myArray[j + 1]; myArray[j + 1] = temp; } } }
What is Fibonacci Sequence?
The next number is the sum of the previous numbers.
EX 0, 1, 1, 2, 3, 5, 8, 13
Code for above example:
static void Fibonacci(int numberOfValues) { int a = 0; int b = 1; int temp = 0; for(int counter = 0; counter < numberOfValues; counter++) { temp = a; a = b; b = temp + a; Console.WriteLine(a); } }
What is an array?
A collection of elements of similar data types; accessed by index starting at 0; can be accessed at random(not required to start at beginning and step through).
Example:
int[] myArray;
What is a stack?
A stack is considered to be a data structure known as a last-in, first-out (LIFO) collection of objects. The standard form of a Stack object in Microsoft .NET can store different data types.
What are the three important methods when using stacks?
1. To place an object on the stack: you push it onto the stack. EX: Stack myStack = new Stack(); myStack.Push("Hello"); myStack.Push(2); myStack.Push(newArray); myStack.Push("This is on top"); Console.WriteLine(myStack.Peek())
- To remove an object: you pop it off the stack.
EX:
myStack.Pop();
Console.WriteLine(myStack.Peek()); - To see what is on the stack: you peel at the stack.
Console.WriteLine(myStack.Peek());
What is boxing?
Converting an intrinsic data type to an object.
What is a list?
A collection of items.
What is an ArrayList?
Allows you to create a list of items similar to an array, where you not only can access the elements through an index, but also insert and delete items that are in the list, something which is more complex in an array, whenever you want.