Exam 2 Review

Here are a few sample questions that are similar to those you might see on the next exam! Then we will present a sample programming problem.

Part 1 - Review of the tools we have learned

Think about all the tools you have at your disposal now to develop a computer program to solve some problem. You have studied a number of basic concepts you need to understand before you can apply them to solve problems with a computer. The questions that follow are typical of the kinds of problems you might encounter (on the exam, or in real life) where a computer would make a good tool to use in solving them.

Encoding Data

  • QUESTION: Suppose you discovered that sound is just air particles vibrating at a certain rate (frequency). You also discovered a device that can measure the vibrations and produce an electrical voltage between 0 and 5 volts - corresponding to the full range of sounds you can hear. 0 volts means no sound, and 5 volts means a very high pitched sound - so high you can barely make it out! If we want to teach the computer to store and analyze sound, how would we encode that data? We want to come up with an encoding that will give us good accuracy in recording the frequency.

Standard Data Types

  • QUESTION: What does the term Data Type mean. (That is, what does the computer know about a data type?)

  • QUESTION: What are the fundamental data types we have learned so far, and give an example of a literal value for each type.

Declaring and Initializing Variables

  • QUESTION: Show a declaration of a variable suitable for storing your age in years.

  • QUESTION: Show a declaration suitable for storing your precise height in inches.

  • QUESTION: How do we initialize the variables we defined above?

Fundamental programming structures

  • QUESTION: Draw diagrams for the three fundamental programming structures.

Input/Output Statements

  • QUESTION: Show a sample input statement that loads the variable you created to store your height.

  • QUESTION: Show an output statement that displays your age with a label.

Assignment Statements

  • QUESTION: An assignment statement is written like this: A = expression;. Explain what the computer will do when it processes this statement.

Expressions

  • QUESTION: What is an expression? Give examples of places where you will need to write expressions in your programs.

  • QUESTION: How are parentheses used in expressions? How does the computer process an expression containing parentheses?

Math Operators and Precedence

  • QUESTION: List the four fundamental math operators.

  • QUESTION: in an expression using all of the operators above and no parentheses, how will the computer process the expression?

Logical Operators

  • QUESTION: What is the difference between an expression that performs math, and a logical expression?

  • QUESTION: Show an expression that determines if the number num is between big and little.

Decision Statements

If-Then-Else Statements

  • QUESTION: Show an if-then-else statement that displays the text “Hello World” if the value of the variable mood is greater than 5, and displays the text “go away” otherwise.

While-Loop

  • QUESTION: Show how to write a while-loop that displays the even numbers from 0 to 10.

Do-While-Loop

  • QUESTION: Show how to write a do-while-loop that displays the even numbers from 0 to 10.

For-Loop

  • QUESTION: Show how to write a for-loop that displays the even numbers from 0 to 10.

Part 2 - Solving a Sample Problem

We need to demonstrate that we are learning how to solve realistic problems. In this exercise, remember to take small steps as you work out a solution. You can take these steps and write code at the same time. You should practice this in all of your programming projects!

Note

This exercise will demonstrate something useful, but it will not be part of the exam. I want to show you how to “feed” a simple program with data we place in a file. The program will not know it is reading data from a file, it will run exactly as if a human is typing things in. This is kind of neat!

Let’s start off by building a simple “Hello, World” program manually. Name this project test2 and save it in a folder named Exam2Lab..

#include <iostream>
using namespace std;

int main(int argc, char * argv[]) {
    cout << "Hello, World" << endl;
    return 0;
}

Place this code in main.cpp in your project folder.

Now run the program and make sure it works correctly.

The problem

Here is a basic statement of the problem we are asked to solve:

Suppose we have collected data from exactly 12 sample temperatures produced by a recording thermometer on a mountain top (yes, there are such things!). The data is recorded in a simple text file with one integer number per line. We would like to produce an output listing of the recorded temperature and the difference between each reading and the average for the entire data set.

Sounds hard, but we will work through it slowly and see if we can come up with a solution.

Since we cannot figure out the average value of the data unless we process all the data, we seem to need to process the data twice. On the first pass, we calculate the average temperature, and on the second pass we produce the output. We can make this happen if we create a new data file with the recorded data listed twice. We will do this with a simple text editor.

Now, you know how to use cin to read data that the user types in on the keyboard. There is an interesting way to cause a program that reads input using cin to read data from a file instead. It uses something called redirection on the command line to do the job. If our program ended up in a file called test2.exe in our project folder, we could open a command prompt window and type the following command to run the program:

Note

You can open a command prompt window by clicking on the Start button at the bottom left of your screen on a PC. Type cmd into the search box that appears near this icon, or navigate to All programs ‣ Accessories ‣ Command Prompt.

You can type help at the command prompt to see the commands that are available. Most of them are short, and you can type help command to show you details on how to use each basic command. We only need to worry about cd (case does not matter in these commands) and dir.

C:\COSC1315\Exam2Lab> test2

And you should see your output.

Note

We run programs at the command prompt by typing the name of the program file (you can add the .exe extension, or leave it off) and the system will load the program and run it. The difference here is that you are issuing commands by typing them in, not clicking on a file name or a button in the IDE. If you have trouble doing this, ask me for help, or check with a tutor in an open lab on campus.

Getting started

Here is the project code I will test:

#include <cstdlib>
#include <iostream>
    
using namespace std;
    
int main(int argc, char * argv[])
{
    int MyData;
    cin >> MyData;
    cout << "You entered " << MyData << endl;
    return 0;
}

Now, this program does not print anything out when it starts, it will wait until you type a number in, then display the result. Try it manually, first.

c:\COSC1315\Exam2Lab>test2
12
You entered 12

This is basically what you would see if you ran the program from inside CLion. Now, let’s try the redirection trick. This assumes you have a file with one number in it. Create a data file by starting gVim (or notepad), and entering a single line with 12 on it. Be sure to press the enter key at the end of this line. Save the file as mydata.txt.

Now, go back the your command prompt window and try this:

c:\COSC1315\Exam2Lab>dir
 Volume in drive C has no label.
 Volume Serial Number is 5440-3100

 Directory of c:\COSC1315\Exam2Lab

10/27/2018  11:45 PM    <DIR>          .
10/27/2018  11:45 PM    <DIR>          ..
10/27/2018  11:45 PM               355 main.cpp
10/27/2018  11:03 PM           475,166 test2.exe
10/27/2018  11:10 PM                 8 mydata.txt
               5 File(s)        950,516 bytes
               2 Dir(s)  356,464,300,032 bytes free

Note

MAC users should type ls -l to see something similar.

The actual output will be different from this, but similar.

Here, we see the program file, and the data file. We will run the program manually now:

c:\COSC1315\Exam2Lab>test2 < mydata.txt
You entered 12

We do not see the number being entered because the user did not type the keys directly, but the program read the data just as though the user had entered it. This is enough to get you through the lab part.

The “less than” symbol tells the operating system to redirect normal input to the program from the file instead of the keyboard. This is a handy way to set up a test for your program where the same data will be used every time you run the program. The output will appear on the console as usual. If you want to capture the output in a second file, say myresults.txt, you could do the following:

c:\COSC1315\Exam2Lab>test2 < mydata.txt > myresults.txt

You can see what is in the file by opening it with something like Notepad, or by typing this:

c:\COSC1315\Exam2Lab>type myresults.txt
You entered 12

OK, now on to the problem:

  • We are to write a program that reads in the first 12 numbers and calculates the average temperature seen during the recording period.

  • The program then reads the next 12 numbers and prints out 12 lines with the following information:

    • The recorded temperature, and the difference between this temperature and the average temperature.

This will not be hard if you work on it using the Baby Steps approach. You should try to solve this problem for practice before the test, at least before the lab part of the test!

Here are the steps I took to get the final code running:

  • build the sample data file with 12 random numbers. Then copy the 12 numbers and paste them after the first set to get 24 lines of sample data in mydata.txt.

  • Add code to read 12 numbers from the input. I printed out the data to make sure it worked.

Here is what I got after this step:

c:\COSC1315\Exam2Lab>test2 < mydata.txt
  line 0 contained 12
  line 1 contained 14
  line 2 contained 16
  line 3 contained 18
  line 4 contained 20
  line 5 contained 22
  line 6 contained 24
  line 7 contained 10
  line 8 contained 9
  line 9 contained 17
  line 10 contained 2
  line 11 contained 5
  • I duplicated the above code and added it again to make sure I could read all 24 values from the file. (You should see two sets of the above output.)

  • Add code to calculate the average value from the first set of numbers. Print out this result after you complete the first loop.

All we need to do to calculate an average is to sum up all the numbers we see and divide by the number of data items. We will use integer math and produce an integer value.

Here is what I produced:

Average value was 14
  • The last step added code to the second loop to do the required calculations to produce the final output.

Here is the final output:

Average value was 14
  0: 12 (diff from avg is -2)
  1: 14 (diff from avg is 0)
  2: 16 (diff from avg is 2)
  3: 18 (diff from avg is 4)
  4: 20 (diff from avg is 6)
  5: 22 (diff from avg is 8)
  6: 24 (diff from avg is 10)
  7: 10 (diff from avg is -4)
  8: 9 (diff from avg is -5)
  9: 17 (diff from avg is 3)
  10: 2 (diff from avg is -12)
  11: 5 (diff from avg is -9)

OK, so the output is a bit ugly, but we will learn how to fix that later. For now, this is fine. Can you build this program. Try it!

One final note:

When you take my test, and you see a question that wants you to write something, I am not looking for an extremely short answer. I am interested in you telling me enough to show that you know what is going on, and are learning the concepts well enough to be able to use them in real problem solving.

On the other hand, I do not subscribe to the old student tactic:

Write down everything you know, because surely the answer must be is there somewhere. (I once had a professor who gave you a fixed amount of space to answer a question and stopped reading when the space was filled up!)

Since you have as much time to work the test as you need, spend more time thinking about the answer than you do writing it down. You should do much better this time - assuming you are studying, of course.

Good luck!