next up previous
Up: Return

Basic Programming

Group: Captain: Recorder: Reflector:

Motivation

In order to write a program that can ``remember'' properties of parts, cost of materials, etcetera, you need variables. Varables can be thought of as named storage locations.

In order to get the values of material cost, material content of parts, etcetera, into the storage locations, you need to know about input and output (IO).

Objectives

1.
Learn about variables.
2.
Learn about assignments.
3.
Learn about input and output.
4.
Learn about numerical expressions.
5.
Learn about program neatness.
6.
Strengthen previous knowledge.

Performance Criteria

1.
Success in writing a program that reads the cost of steel per unit weight as well as the steel weight of a part from the keyboard, computes the cost of the steel in that part, and outputs that cost. Storage locations to create: steel_cost, (containing the cost of steel per kg or lb), part_steel_content, (containing the amount of steel in the part being computed), and part_material_cost, (containing the total cost of material, just steel for now, for the part being computed.) (25%)
2.
Success in modifying the program for a part that also has, or may have, some aluminum content. Variable part_material_cost now should contain the total cost of material, both steel and aluminum, for the part being computed. You also will need to define additional variable names. (25%)
3.
Program neatness, including lots of clear and meaningful comments, clear and meaningful variable names, indentation, strategic blank lines, etcetera. Do not use the tab key to tab, but the space bar. (20%)
4.
The knowledge demonstrated by your definitions of the vocabulary. (10%)
5.
The depth of understanding demonstrated by your answers to the critical thinking questions. (20%)

Resources

1.
Your class notes.
2.
The textbook.
3.
The online help facilities of Windows and the C++ compiler.

Plan

1.
Skim through the lessons ``Writing Messages to Your Screen'', ``Programs Store Information in Variables'', ``Performing Simple Operations'', and ``Reading Keyboard Input'' of the book.
2.
Create your program, compile and execute it.
3.
Create the second modified program.
4.
E-mail the program to the instructor and group members.
5.
See what vocabulary you can define.
6.
See what critical thinking questions you can answer.
7.
Take the test and submit it.

Vocabulary

Select the best answer:

1.
I/O.
(a)
Getting data in and out of the computer
(b)
Independent Operations
(c)
Interactive Operations
2.
Variables.
(a)
Named storage locations
(b)
Names for storage locations
(c)
Storage locations that can contain anything
3.
Variable types.
(a)
Named storage locations
(b)
Names for storage locations
(c)
Allowable possible contents for storage locations
4.
Precedence.
(a)
Order in which statements occur
(b)
Order in which computations occur
(c)
Order in which compilation occurs

Critical Thinking Questions

Select the best answer:

1.
What output do you get from the statement:
    cout << "H" << "el" << 'l' << 'o' << endl;
(a)
Hello
(b)
H el l o
(c)
"H" << "el" << 'l' << 'o' << endl

2.
What output do you get from the statement:
    cout << "0.500 = " << 0.500 << endl;
(a)
0.500 = 0.500
(b)
0.5 = 0.5
(c)
0.500 = 0.5

3.
What is the difference in output between the program segments on the left and on the right:
    cout << "Line 1\nLine 2\n";       cout << "Line 1\n";
                                      cout << "Line 2\n";
(a)
Everything is printed on the same line by the left segment, on two separate lines for the right.
(b)
The left segment single-spaces two lines, the right segment double-spaces them.
(c)
There is no difference.

4.
What is the difference in output between the program pieces on the left and on the right:
    cout << "Line 1\n";      cout << "Line 1" << endl
    cout << "Line 2\n";           << "Line 2" << endl;
(a)
The Left segment single-spaces two lines, the right segment double-spaces them.
(b)
There is no difference.
(c)
The segment on the right will not compile: the cout is missing.

5.
What should you get from the statement:
    cout << "\a" << "Hi" << "there" << endl;
(Does not work on the PCs, but works on Unix.)
(a)
The program writes on the screen \a Hi there
(b)
The program writes on the screen \aHithere
(c)
The program writes on the screen Hithere and the computer beeps.

6.
Why is the program on the right much better than the one on the left:
    #include <iostream.h>            #include <iostream.h>
    void main(void) {                void main(void) {
       int a;                           int a;
       ...                              int b;
       int b;                           ...
       ...                              ...
       a=1;                             a=1;
       b=3;                             b=3;
       cout << a+b << endl;             cout << a+b << endl;
       ...                              ...
    }                                }
(The dots stand for lots of other statements that are not important.)
(a)
All declarations of variables must be at the start.
(b)
It is easier for someone to find out about a variable such as b if all declarations are grouped together.

7.
What is obviously wrong (except lack of neatness):
    #include <iostream.h>
    void main(void) {
    int pi;
    ...
    pi = 3.141593;
    ...
    }
(a)
There should be a semi-colon at the end of the include line.
(b)
The assignment statement pi = 3.141593; will crash.
(c)
Variable pi is set to 3, not to 3.141593

8.
Why is the program on the left much better than the one on the right:
    #include <iostream.h>           #include <iostream.h>
    void main(void) {               void main(void) {
       float pi = 3.141593;            float pi;
       ....                            ....
       ....                            pi=3.141593
       ....                            ....
       area = pi*r*r                   area = pi*r*r
       ....                            ....
(a)
It is easier to check whether variable pi has been given the right value.
(b)
Computers cannot store many lines of code. You worry about every line.
(c)
If we give pi a value early, it will be sure to have received the value by the time we reach the area= statement.

9.
Why is the comment on the left much better than the one on the right:
    #include <iostream.h>           #include <iostream.h>

    void main(void) {               void main(void) {
       // Declarations:                // Declarations:

       // Define pi:                   float pi = 3.141593; // pi is defined
       float pi = 3.141593;            ....
       ....                            ....
(a)
You cannot put comments behind statements, they must be on separate lines.
(b)
By the time the reader reads the comment in the program on the right, he/she is either mystified or already has figured the statement out by himself.

10.
Which people can benefit from the comments that you put in your program?
(a)
You right now.
(b)
You, when you try to reuse the program a year from now.
(c)
Someone who wants to use your program but does not know exactly what it does.
(d)
Someone who wants to modify your program for a similar task.
(e)
All of the above.

11.
Can you store arbitrarily large integers in an int? In a long int?
(a)
Yes. Yes.
(b)
No, there are limitations on the size. Yes.
(c)
No. No, but you can store larger integers.

12.
Can you store arbitrarily large real numbers in a float? In a double?
(a)
Yes. Yes.
(b)
No, there are limitations on the size. Yes.
(c)
No. No, but maybe you can store larger real numbers.

13.
Can you store arbitrarily accurate floating point numbers such as $\pi$ or $\sqrt 2$ in a float? In a double?
(a)
Yes. Yes.
(b)
No, there are limitations on the number of digits behind the first nonzero one that are going to be stored. Yes.
(c)
No. No, but you can store more digits.

14.
What do you get from:
    cout << "3*3 = " << 3*3 << endl;
(a)
3*3 = 3*3
(b)
9 = 9
(c)
3*3 = 9

15.
What do you get from:
    float a; ...; a=1.5; cout << a << endl;
(a)
0
(b)
1
(c)
1.5

16.
What do you get from:
    int a; ...; a=1.5; cout << a << endl;
(a)
0
(b)
1
(c)
1.5

17.
What do you get from:
    cout << ".33 = " << 1/3 << endl;
(a)
.33 = 0
(b)
.33 = .33
(c)
.33 = .3333333

18.
What do you get from:
    cout << ".33 = " << 1./3 << endl;
(a)
.33 = 0
(b)
.33 = .33
(c)
.33 = .3333333

19.
What do you get from:
    cout << "Large: " << 300000*300000 << endl;
(a)
Large: 300000*300000
(b)
Large: 90000000000
(c)
Neither

20.
What do you get from:
    cout << "9 = " << 1+2*3 << endl;
(a)
9 = 1+2*3
(b)
9 = 9
(c)
9 = 7

21.
What do you get from:
    cout << ".25 = " << 1./2.*2. << endl;
(a)
.25 = 1./2.*2.
(b)
.25 = .25
(c)
.25 = 1.

22.
What do you get from:
    cout << "1 = " << 1./2./2. << endl;
(a)
1 = 0
(b)
1 = .25
(c)
1 = 1.

23.
What do you get from:
    int i; ...; i=1; cout << i++; cout << i << endl;
(a)
11
(b)
1 1
(c)
12

24.
What do you get from:
    int i; ...; i=1; cout << ++i; cout << i << endl;
(a)
11
(b)
2 1
(c)
22

25.
What happens during the execution of:
    int i,j; ...; cin >> i >> j;
(a)
The program prints out ij.
(b)
The program prints out the values of variables i and j
(c)
The program waits for you to type in ij.
(d)
The program waits for you to type in i j.
(e)
The program pauses at the cin statement until you enter two integer values from the keyboard. It stores these in variables i and j.

Exercises

1.
How do you create a simple table?
2.
Print out a zero (O) if a number is odd and a 3 (an inverted E) if the number is even, using only what you have learned so far.

Evaluation

1.
Give two strengths of your group.
2.
Indicate two areas for improvement.
3.
What two new insights did your group obtain?
4.
Give two strengths of the class so far.
5.
Indicate two areas for improvement of the class.
6.
What two new insights did you obtain about the class?

next up previous
Up: Return
Author: Leon van Dommelen