1
|
- Homework 3 is due tonight.
- Iteration and basic functions.
- Exam 1 on Monday.
- You will need to know all the C++ syntax covered in the lecture notes
up to recursion.
- You will need to be able to write programs of similar difficulty to
those on homework 1,2 & 3.
|
2
|
|
3
|
|
4
|
- Program Trace
- Fib
- Fact
- Visualization of Recursion with Java
- http://www.iol.ie/~jmchugh/csc302/
|
5
|
- Anatomy of a Computer
- Types and Variables
- Operators
- Control Structures
- Selection (if
else, switch)
- Iteration (while, for)
|
6
|
- Input and Output
- Functions
- Calling Functions
- Function Prototypes
- Function Definitions
- Function overloading
|
7
|
- Find the bug in this code:
- #include <iostream>
- using namespace std
- int main( )
- {
- cout << "hello world!" << endl;
- return 0;
- }
|
8
|
- Find the bug in this code:
- #include <iostream>
- using namespace std;
- int main( )
- {
- double width, area;
- cout<<"Enter the width of the square: ";
- cin >> width;
- Area = width * width;
- cout << "The area of the square with the width = "
- << width <<
" is " << Area << endl;
- return 0;
- }
|
9
|
- Find the bugs in this code:
- #include <iostream>
- int main()
- {
- int i = 30;
- while( i != 100 )
- {
- i++;
- if ( i == 100 )
- {
- i = i 1;
- }
- cout << i << << endl;
- }
- }
|
10
|
- Find the bug in this code:
- #include <iostream>
- int test( int n );
- int main()
- {
- int n = 5;
- cout << test( n );
- return 0;
- }
- int test( int )
- {
- return (n+1);
- }
|
11
|
- Find the bug in this code:
- #include (iostream)
- int main()
- {
- int i = 30;
- while( i < 100 )
- {
- i++
- if ( i == 100 )
- {
- i = i + 1;
- }
- cout << i << << endl;
- }
- }
|
12
|
- Calculate the value of n:
- int a = 3, b = 2, p = 8, q = 5;
- double n = (p/b + q/a) % 3;
|
13
|
- Give the value of n after this code:
- int n = 3, a = 2, b = 7, c = 4;
- if (b>6 && c<4 || a==0)
- {
- n = n + 5;
- }
- if (a>3 && b>5 || c<=9)
- {
- n = n + 20;
- }
- if (c>2 || a<8 && b==7)
- {
- n = n - 5;
- }
|
14
|
- What does this program print?
- for ( int i = 0; i < 5; i++ )
- {
- for ( int j = 0; j < i; j++ )
- {
- cout << j << ;
- }
- cout << endl;
- }
|
15
|
- What does this code print?
- #include <iostream>
- using namespace std;
- int main()
- {
- int n = 1;
- for ( int i = 1; i < 6;
i++ )
- {
- n *= i;
- cout << n <<
" ";
- }
- }
|
16
|
- What does this code print? Find the bug.
- #include <iostream>
- using namespace std;
- int main()
- {
- int n = 1;
- for ( int i = 0; i < 6;
i++ )
- {
- n *= i;
- cout << n <<
" ";
- }
- }
|
17
|
- What does this code print?
- int n = 1;
- while ( n < 5 )
- {
- for ( int i = 1; i < 5;
i++ )
- {
- cout << n++ <<
" ";
- cout << ++n <<
" ";
- }
- }
|
18
|
- Write a program that sums the even numbers between 1 and 100 inclusive.
- Ans:
- #include <iostream>
- int main()
- {
- int sum = 0;
- for ( int i = 1; i <= 100; i++ )
- {
- if ( i % 2 == 0)
- {
- sum = sum + i;
- }
- }
- }
|
19
|
- What does the following code print:
- char foo = b, bar = a;
- n = 4;
- while ( n < 20 )
- {
- if ( n % 4 == 0)
- {
- cout << foo;
- }
- if ( n % 2 == 0)
- {
- cout << bar;
- }
- n = n + 2;
- }
- cout << c;
|
20
|
- What does the following code print:
- #include <iostream>
- using namespace std;
- int cow( int );
- int sheep( int );
- int main()
- {
- int n = 1;
- for ( int i = 1; i < 3;
i++ )
- {
- cout << "quack
" << endl;
- cow( i + 1);
- }
- }
|
21
|
- What does the following code print:
- #include <iostream>
- using namespace std;
- int test( int x, int y );
- int main()
- {
- int j = 1;
- while( test( j, j ) < 8*j )
- {
- cout << j <<
" ";
- j = j * 2;
- }
- return 0;
- }
- int test( int x, int y )
- {
- return (x * y);
- }
|
22
|
- Write a program that gets three floating point numbers from the user and
prints the one which is neither largest nor smallest.
- Write a program that converts one system of measurement to another. Feet
to inches for example.
|