Assignment 4

Document Sample
Assignment 4 Powered By Docstoc
					CS140 OOP/Programming Fundamentals                Name:_______________________
Fall 2005                                         ID: _________________________
Date: October 5, 2005
Due Date: October 12, 2005
Total Score: 30

                                     Assignment 4

Assignment (5 marks)

There are several primary ways to modify the contents of a variable in C++.

      cin >>   variable; the '>>' operator places the contents of the input stream into the
       variable
      variable = 3; the '=' operator, called the assignment operator, takes whatever
       combination of values are on its right side and puts them into a variable on its left
       side.
      variable++; the '++' operator increments, or adds 1 to the variable to which it is
       appended. Similarly, the '--' operator will decrement an integer variable by 1.

During compilation, values on both sides of the >> and = operators are checked to insure
that the data received matches the data type of the variable to which it will be assigned.
Are the right and left hand sides of the assignment operators of compatible type in the
following statements?

int d=4;

double a; int c = c * (6 + a);

double q = "3";


What error does your compiler give in these cases?:

Statement    Error message
kq+2 = 19;
PI = 3;
xz = 20++;

Because a variable of any given type represents a fixed amount of space in memory, there
is a limit to how large a value it can store.

Size limitations are particularly evident with integers. On some computers, integers can
only be about 32,000, on others, they can go up to a little more than 2,000,000,000. What




CS140 OOP/Programming Fundamentals – Assignment 4
is the largest value your compiler will accept for a signed integer? What is the most
negative allowed value?




Floating point numbers don't round to the nearest integer value. Can you explain the
values your compiler assigns to f and g in:

double f = pow(10.0, 20.0) - 53;
double g = f;
g = g - 53;
cout << f << "\n" << g << "\n";




Arithmetic (5 marks)

Recalling what you've learned about integers and floating point values, what value is
assigned to Z by each of the following?

int Z = 8 / 4;

int Z = 9 / 4;

double Z = 9 / 4;

int Z = 222 / 300;

double Z = 222.0 / 300.0;

int Z = 222 / 300.00;

double Z = pow(3,6);


Translate the following algebraic expressions into C++ :

y = x + 3/4 - 2
y = x2 + 6x - 2
     x
y = ---
    1-x




CS140 OOP/Programming Fundamentals – Assignment 4
Relations and Relational Operators (2.5 marks)

The relational operators in C++ are == != < > <= and >=

       Formulate the following conditions in C++:

x is positive
x is zero or negative
x is at least 8
x is less than 8
x and y are both zero


Input Validation Exercise (5 marks)

       Build and run the following program. Describe what happens when the salaries
        from the first and second years are equal.




#include <iostream>
using namespace std;
int main()
{
double fyear_sal, syear_sal,tyear_sal;

cout << "Input the first year's salary:" << "\n";
cin >> fyear_sal;;
cout << "Input the second year's salary:" << "\n";
cin >> syear_sal;
cout << "Input the third year's salary:" << "\n";
cin >> tyear_sal;
cout << "The difference between the third and second year salary and second and first
is:"
cout << ((tyear_sal-syear_sal)/(syear_sal-fyear_sal))*100 << "%";
return 0;
}

       Correct and rebuild the program to not allow for case where both the first and
        second year salaries are the same.



CS140 OOP/Programming Fundamentals – Assignment 4
 /* paste program here */




       What are the results when fyear_salary = 100000 and syear_salary = 100000?




The if/else statement (5 marks)

In the previous examples, your program probably responded to user input by ignoring
cases that would result in a divide by zero. Instead, you can use the if/else format to
explicitly specify the action to be taken.

if (condition)
   /* do something ... */
else
   /* do something different ... */

The electric company gives a discount on electricity based upon usage.
The normal rate is $.60 per Kilowatt Hour (KWH). If the number of KWH
is above 1000, then the rate is $.45 per KWH. Write a program that
prompts the user for the number of Kilowatt Hours used and then




CS140 OOP/Programming Fundamentals – Assignment 4
calculates and prints the total electric bill.
 /* paste program here */




According to your program, how much will it cost for:

900 KWH?

1754 KWH?

10000 KWH?

Processing a Sequence of Inputs (2.5 marks)

Modify the electric rate program to allow the user to enter data for any number of
months. The user should be able to enter a -1 when finished. The program should then
compute the cost using the same formulas as above.


CS140 OOP/Programming Fundamentals – Assignment 4
 /* paste program here */




Functions (5 marks)

Write a function multiple that determines for a pair of integers whether the second integer
is a multiple of the first. The function should take two integer arguments and return true
if the second is a multiple of the first, false otherwise. Use this function in a program that
inputs a series of pairs of integers.




CS140 OOP/Programming Fundamentals – Assignment 4
 /* paste program here */




CS140 OOP/Programming Fundamentals – Assignment 4

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:30
posted:5/13/2011
language:English
pages:7