.. _exam-2-review: 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``.. .. literalinclude:: code/test0.cpp :language: c++ 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 :menuselection:`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`. .. code-block:: text 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: .. literalinclude:: code/test1.c :language: c 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. .. code-block:: bash 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: .. code-block:: bash 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