Sie sind auf Seite 1von 8

Maliks Exercises for Lecture 2 and Lab 3

Exercises

3:4 Suppose x and y are int variables and ch is a char variable.


Assume the following input data:

13 28 D
14 E 98
A B 56
What value (if any) is assigned to x, y, and ch after each of the
following statements executes? (Use the same input for each
statement.)

a.
b.

cin >> x >> y;


cin.ignore(50, \n);
cin >> ch;
cin >> x;
cin.ignore(50, \n);
cin >> y;
cin.ignore(50, \n);
cin.get(ch);

3:7 Suppose that age is a variable of the type int and name is a
variable of the type string. What are the values of age and
name after the following input statements execute?

cin >> age;


getline(cin, name);
if the input is:

a.
b.

35 Mickey Balto
35
Mickey Balto

6:3 Consider the following statement:


double num1, num2, num3;
int int1, int2, int3;
int value;
num1 = 5.0; num2 = 6.0; num3 = 3.0;
int1 = 4; int2 = 7; int3 = 8;
and the function prototype:
double cube(double a,double b,double c);
Which of the following statements are valid? If they are invalid,
explain why.

a.
b.
c.
d.

value =
cout <<
cout <<
cout <<

cube (num1, 15.0, num3);


cube(num1, num3, num2);
cube(6.0, 8.0, 10.5);
cube(num1, num3);

6:5 Consider the following function prototypes:


int test(int, char, double, int);
double two(double, double);
char three(int, int, char, double);
Answer the following questions.
a. How many parameters does the function test have? What is
the type of the function test?
b. How many parameters does function two have? What is the
type of function two?
c. How many parameters does function three have? What is the
type of function three?

7:2 Consider the programming code given below. Identify the


following:
a.

Function prototype, function heading, function body, and


function definitions.
b. Function call statements, formal parameters, and actual
parameters.
c. Value parameters and reference parameters.
d. Local variables and global variables.
#include <iostream>
using namespace std;
int one;
void hello(int&, double, char);
int main() {
int x;
double y;
char z;
.
.
hello(x, y, z);
.
.
hello(x, y-3.5, S);
.
.
}
void hello(int& first, double second, char ch)
int num;
double y;
int u;
.
.
}

7:4 What is the output of the following program?


#include <iostream>
#include <iomanip>
using namespace std;
void test(int first, int& second);
int main() {
int num;
num = 5;
test(24, num);
cout << num << endl;
test (num, num);
cout << num << endl;
test(num * num, num);
cout << num << endl;
test(num + num, num);
cout << num << endl;
return 0;

}
void test(int first, int& second)
int third;

third = first + second * second + 2;


first = second - first;
second = 2 * second;
cout << first << << second <<
<< third << endl;
}

// Example 6-1 Ch6_PredefinedFunctions.cpp


// How to use predefined functions.
#include <iostream>
#include <cmath>
#include <cctype>
#include <cstdlib>
using namespace std;
int main()
{
int
x;
double u,v;
cout << "Line 1: Uppercase a is "
<< static_cast<char>(toupper('a'))
<< endl;
//line 1
u = 4.2;
//line 2
v = 3.0;
//line 3
cout << "Line 4: " << u << " to the power of "
<< v << " = " << pow(u, v) << endl;
//line 4
cout << "Line 5: 5 to the power of 4 = "
<< pow(5, 4) << endl;

//line 5

u = u + pow(3, 3);
cout << "Line 7: u = " << u << endl;

//line 6
//line 7

x = -15;
cout << "Line 9: Absolute value of " << x
<< " = " << abs(x) << endl;

//line 8

return 0;
}

//line 9

Programming Exercises
2.9Write a program that does the following:
a. Prompts the user to input five decimal numbers.
b. Print the five decimal numbers.
c. Converts each decimal numbers to the nearest integer.
d. Adds the five integers.
e. Prints the sum and average of the five integers.
3:4 Write a program to calculate the property tax. Property tax is
calculated on 92% of the assessed value of the property. For
example, if the assessed value is $100000, the property tax is on
$92000. Assume that the property tax rate is $1.05 for each $100 of
the assessed value. Your program should prompt the user to enter the
assessed value of the property. Store the output in a file in the
following format. (Here is a sample output.)
Assessed Value:
Taxable Amount:
Tax Rate for each $100.00:
Property Tax:

100000.00
92000.00
1.05
966.00

Format your output to have two decimal places. (Note that the left
column is left justified and the right column is right-justified.)

Das könnte Ihnen auch gefallen