Docsity

Iostream - Computers and Programming I - Assignment, Exercises of Computer Programming

Toggle sidebar
bg1
COMPUTERS & PROGRAMMING I
FINAL Exercise
Student Name: ______________________________
Student ID: ______________________________
Part 1 Basic
1. Why is it necessary to include iostream in a program that uses cin and cout?
2. What is a compiler? What's the name of the compiler we use for our course?
3. What is the difference between an object and a class?
4. What is the difference between class and struct?
5. What is the difference between pass by value and pass by reference?
6. List one advantage and one disadvantage of passing parameters by reference instead of by value.
7. In the function foo (int t[], int size); is the array t passed by value or by reference?
8. When would you use a do/while loop instead of a while loop?
9. Members of a class specified as ____________ are accessible only by members of that class and by
friends of that class.
10. What is the difference between a void and a non-void function?
11. Suppose your executable is named a.out. Write the complete UNIX command to execute a.out but
redirect its input so that cin reads from the file input.dat.
12. Suppose x and y are two int variables different than zero. Write a statement that computes the real
number that results from dividing x by y. Assume x and y are already declared.
13. Write a single C++ statement that will compute the value of the formula y = ax2+bx+c.
14. Write a statement that assigns random integers to the variable n in the following ranges:
a) 0 ≤ n ≤ 11
b) -4 ≤ n ≤ 15
15. Declare a string variable called firstname and give it the value "John".
Part 2 Control Structures
16. Rewrite the following if/else statement using the conditional operator (?:)
if (x % 3 == 0)
cout << "x is a multiple of 3";
else
pf3
pf4
pf5
pf8

Partial preview of the text

Download Iostream - Computers and Programming I - Assignment and more Exercises Computer Programming in PDF only on Docsity!

COMPUTERS & PROGRAMMING I

FINAL Exercise

Student Name: ______________________________

Student ID: ______________________________

Part 1 Basic

  1. Why is it necessary to include iostream in a program that uses cin and cout?
  2. What is a compiler? What's the name of the compiler we use for our course?
  3. What is the difference between an object and a class?
  4. What is the difference between class and struct?
  5. What is the difference between pass by value and pass by reference?
  6. List one advantage and one disadvantage of passing parameters by reference instead of by value.
  7. In the function foo (int t[], int size); is the array t passed by value or by reference?
  8. When would you use a do/while loop instead of a while loop?
  9. Members of a class specified as ____________ are accessible only by members of that class and by friends of that class.
  10. What is the difference between a void and a non-void function?
  11. Suppose your executable is named a.out. Write the complete UNIX command to execute a.out but redirect its input so that cin reads from the file input.dat.
  12. Suppose x and y are two int variables different than zero. Write a statement that computes the real number that results from dividing x by y. Assume x and y are already declared.
  13. Write a single C++ statement that will compute the value of the formula y = ax^2 +bx+c.
  14. Write a statement that assigns random integers to the variable n in the following ranges: a) 0 ≤ n ≤ 11 b) -4 ≤ n ≤ 15
  15. Declare a string variable called firstname and give it the value "John".

Part 2 Control Structures

  1. Rewrite the following if/else statement using the conditional operator (?:)

if (x % 3 == 0) cout << "x is a multiple of 3"; else

cout << "x is NOT a multiple of 3";

  1. A company gives its customers a 20% discount if they order 1000 or more of the same item; 15% if they order 500-999; 10% if they order 100-499; and no discount if quantity < 100. Write an if/else if/else statement that will figure out the amount of discount and the order total. Assume that the variables quantity, discount, subtotal (order subtotal before discount is applied), and total (order total after proper discount has been applied) have already been declared.
  2. Write a nested if/else statement that would assign grades to students based on their overall average. The grade cutoffs are A:90, B:80, C:70, and D:60. Assume that the variables grade and avrg were defined as: char grade; double avrg;
  3. Rewrite the following for loop using a while loop.

for (TempF = 0; TempF <= 100; TempF += 10) { TempC = (TempF - 32.0) * 5.0 / 9.0; cout << TempF << " " << TempC << endl; }

  1. Re-write the following code using a for loop instead of the while loop.

x = 1; product = 1; while (x <= 8) { product *= x; x += 2; }

  1. Write the code (using two nested for loops) that would print the following shape (notice there is a difference of 2 stars between two consecutive lines)
  1. Write the code (using two nested for loops) that would print the following shape


  1. Using while loop to write the code (not an entire program) that will ask a user for a number. As long as the number given by the user is a multiple of 5, print that number out and ask the user for a new number.

Part 3 Functions

  1. In the function foo (const int &x); is it possible for the function to modify the value of x? Why or why not?
  2. Comment on the design of the following function: readAndPrintData (int dataArray[], int &arraySize).

int map[ROWS][COLS]; int numRows; int numCols; }; bool find (const bitmap s, const int val, int &row, int &col);

Part 4 Arrays

  1. State what happens in memory when the statement char x[10]; is executed.
  2. Can an array contain elements of different types?
  3. Define a 3 by 5 array of string. Call it nameTable.
  4. Declare the array score of size 100 and type double, and initialize its first 5 elements to 1, 3, 5, 7, 9 and the remaining elements to 0.
  5. Declare the array X of size 100 and type int. Using a for loop, initialize its elements to 3, 6, 9, 12, 300.
  6. Declare the array Y of size 100 and type int. Using a for loop, initialize the elements of the array with even indices to 2 and the ones with odd indices to 3.
  7. Consider the one-dimensional array t declared as int t[100]. Write single statements that perform the following operations: a) Initialize the elements of the array to zero b) Add 2 to every element of the array c) In one statement, declare the array score of type int and size 1000 and initialize its first three elements to 5, 10, 15 and the remaining elements to 0.
  8. Consider a 4 by 10 integer array t. a) How many elements does t have? b) Write a nested for structure that initializes each element of t to 3. c) Write a statement that prints out the elements of the first row of t. d) Write a statement that prints out the elements of the second column of t.

Part 5 Structure and Class

  1. A car is described by its make, model, and year. Define a struct named structCar to store such information about a car.
  2. Answer parts a-c based on the following code: class Cjar { private: const int increment; int count; string label; public: void set(string plabel); // set label to the value of plabel int get()const; // return the value of data member count };

a) The code below is the body of the implementation of member function get(). What is the error in it, and why is it an error? // body of function get() {

count += increment; return count; };

b) Implement the constructor for Cjar. It should set increment to 1, count to zero, and label to "?????".

c) Assume Cjar is correctly defined/implemented. The following code is from a client of the Cjar class. If there are errors, identify them. All required include files have been included. // code from client of Cjar const Cjar jar1; Cjar jar2; cout << jar1.get() << jar2.get(); jar2.set("Pickles");

  1. Fill in the blanks. #include #include class Customer { public: string FirstName; // customer first name string LastName; // customer last name int IdNumber; // customer unique ID int AcctNumber; // customer account number double AcctBalance; // customer account balance };

int main() { Customer john; cout << "Enter customer ID number" << endl; cin >> _____________________; cout << "Enter customer first and last name" << endl; cin >> _____________________ >> _____________________; return 0; }

  1. Consider the struct Sstudent. It has two data members: name (a string), and GPA (a double). Suppose that we declared s to be an Sstudent. Write C++ code to print the name of s and the GPA of s.
  2. Write a class declaration for a class called herd which represents a herd of dairy cows. This class should have a private variable to represent the size of the herd and two public member functions, one of which will set the size of the herd, and the other which will return it. You do not need to implement these functions. Choose appropriate variable types, function names, parameter types, return types, etc.
  3. Write the declaration for a class called Box which can contain up to MAX_SIZE objects of type Toy. Assume that class Toy and the constant MAX_SIZE have been correctly defined. You should be able to put Toy objects into the Box, take objects out of the Box, search for a specific Toy, and find out how many Toys are currently stored in the Box. You do not need to implement any of the member functions.

Part 6 Tracing

  1. What's wrong with the following code?

product = 1; while (x <= 5) { product *= 2; x += 2; }

  1. The function definition of incrementByValue and incrementByReference is shown below. int incrementByValue( int a ) { return a += 4; } int incrementByReference( int &a ) { return a += 4; } What are the values of x, y, z, and w after executing the following code? int x = 5, y = 10, z, w; z = incrementByValue( x ); w = incrementByReference( y );
  2. There are two problems with the following function definition. Point them out?

int incrementNum (const double &a) { return (a += 2); }

  1. What is wrong with the following function?

void readstring (string s) { cout << "Enter a string: "; cin >> s; }

  1. Identify the error(s) in the following function and state briefly why they are errors.

int sumOf (const int num1, const int num2) { cin >> num1; cin << num2; // ā€œ>>ā€ sum = num1 + num2; return; }

  1. What is wrong with the following code? (Assume that key, a, and size are correctly defined and initialized.) for (int i = 0; i<size; i++) { if (a[i] == key) break; } if (i < size) cout << "key found" << endl; else cout << "key not found" << endl;
  1. The function swap, defined below, was supposedly designed to take two doubles as parameters and exchange their values. For example, it the function is called like swap(a,b) with a = 3.5 and b = 1. then, after the function call we should have a = 1.5 and b = 3.5. This function, as written below, was tested and it did not perform as expected. Describe what's wrong with it and how it could be fixed.

void swap(double a, double b) { double temp; temp = a; a = b; b = temp; }

  1. What does the following function do? (Assume n represents the size of the array a.)

void mystery (double a[], int n) { for (int i = n-1; i>=0; i--) cout << a[i] << endl; return; }

Part 7 Programming

  1. The functions readNums(int &n1, int &n2), int min(int n, int m) are given to you. readNums will read from the user two integers. min will accept as parameters two integers and return the the value of the smaller one. Write a main program that asks the user for two integers and prints the smaller of the two integers using these functions. (No need for function prototypes / definitions or #includes.)
  2. Write a program that would ask the user for 10 integers, get the integers, compute their average, and print it out. You must use a for or a while loop in your code.

Part 8 Recursion

  1. What are the two cases you must design in a recursive function?
  2. Define a recursive function factorial() which takes a positive integer as a parameter, and returns the factorial of that integer.
  3. What exactly will be printed by the following function if it is called as mysteryprint(0,1)?

void mysteryprint (int last, int current) { int temp; if (current < 10) { cout << current << endl; temp = current + last; mystryprint(current, temp); } }