Module 4: Algorithms and Data Structures Flashcards

1
Q

What is pseudocode?

A

English like syntax; close to actual programming code; used to design algorithms prior to writing actual code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the main reasons (4) for using pseudocode?

A
  1. Ensures you understand the problem that you are trying to solve.
  2. Helps you visualize your solution without getting lost the process of writing proper code syntax and structure.
  3. Helps you write multiple variations of an algorithm that can be used for evaluation purposes.
  4. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 4 simple rules to remember when writing pseudocode?

A
  1. Use good English in code.
  2. Avoid unnecessary detail (no punctuation or braces).
  3. Do not state the obvious. Data types do not need to be declared if it is clear from the text.
  4. Use programming short hand. EX. if-the-else
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an algorithm?

A

A step- by- step procedure for solving a problem. Used creatively to ensure considerations for simplicity and performance are taken.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A
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'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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'
A

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.”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a bubble sort?

A

Finding a lower value on a list and bubbling it up to the top of the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the logic for a bubble sort?

A
  1. Get the size of the list.
  2. Create a pointer to the first value.
  3. Get the value referenced by the pointer.
  4. Get the value one greater than the pointer.
  5. Compare the two values.
  6. If the next value is smaller, swap the values.
  7. Increment the value pointer.
  8. 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; 
      } 
   } 
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Fibonacci Sequence?

A

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); 
   } 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an array?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a stack?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the three important methods when using stacks?

A
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())
  1. To remove an object: you pop it off the stack.
    EX:
    myStack.Pop();
    Console.WriteLine(myStack.Peek());
  2. To see what is on the stack: you peel at the stack.
    Console.WriteLine(myStack.Peek());
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is boxing?

A

Converting an intrinsic data type to an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a list?

A

A collection of items.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is an ArrayList?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a SortedList?

A

Allows you to store objects in the list using a key and a value. The key serves a dual purpose. It allows for accessing the value associated with that key, and allows for sorting. defaults to sorting items in ascending order either alphabetically or numerically. You can still access an element in a SortedList using an index value as well. With a key/value pair, you would assign a simple, easy to recognize key that relates to the data stored in the value. Internally, the SortedList maintains two arrays, one for the keys and one for the values.

17
Q

What is a queue?

A

Used to represent a list of items in the form of a first-in, first-out (FIFO) data structure.
EX:
Queue myQueue = new Queue();
myQueue.Enqueue(“First Item”);
myQueue.Enqueue(“Second Item”);
myQueue.Enqueue(“Third Item”);
myQueue.Enqueue(“Last Item”);
Console.Write(“The item at the beginning of the queue is: “);
Console.WriteLine(myQueue.Peek());
// check to see if an element exists called Third Item
bool exists = myQueue.Contains(“Third Item”);
Console.Write(“Does the queue contain ‘Third Item’? “);
Console.WriteLine(exists);
// Remove the first item and print out the new first item in the queue

myQueue.Dequeue();
Console.WriteLine(“The item ‘First Item’ was removed using Dequeue and now the first
item is: “);
Console.WriteLine(myQueue.Peek());

18
Q

What is a dictionary?

A

Can represent any object in .NET; uses key/ value pairs; strongly typed as a generics type; can be sorted and unsorted; part of the System.Collection.Generics namespace.

19
Q

What is the SortedDictionary<>?

A

A way to maintain a sorted dictionary list of items. The sorting is based on the keys used. Gives the ability to locate words in the dictionary based on an alphabetical or other sorting mechanism where there is a defined sorting to the items in the dictionary.