Unformatted text preview:

Jacaranda (Engineering) 3333 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax: 818.677.7062College of Engineering and Computer ScienceComputer Science DepartmentComputer Science 106 Computing in Engineering and ScienceSpring 2006 Class number: 11672 Instructor: Larry CarettoEngineering Building Room 2303 Mail Code Phone: 818.677.6448E-mail: [email protected] 8348 Fax: 818.677.7062February 14 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 3Homework Solutions –February 14, 2006Page 74, problem 7 – Modify the following program so that it prints two blank lines between each line of text.#include <iostream>using namespace std;int main(){ cout << “Two mandolins like creatures in the”; cout << “dark”; cout << “Creating the agony of ecstasy.”; cout << “ - George Barker”; return 0;}The corrected code is shown below with changes shown in red. (This solution assumes that the word dark is part of the first line and does not require any spaces before the word.) Note that three newline characters are required to insert two blank lines.#include <iostream>using namespace std;int main(){ cout << “Two mandolins like creatures in the”; cout << “dark\n\n\n”; cout << “Creating the agony of ecstasy.\n\n\n”; cout << “ - George Barker\n\n\n”; return 0;}Page 74, problem 8 – What will the following programs print on the screen.A) #include <iostream>using namespace std;int main(){ int freeze = 32, boil = 212; freeze = 0; boil = 100; cout << freeze << endl << boil << endl; return 0;}This program will print the following:0100After printing the 100, the output will move to a new line for any subsequent output.B) #include <iostream>using namespace std;int main(){ int x = 0, y = 2;February 14 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 4 x = y * 4; cout << x << endl << y << endl; return 0;}This program will print the following:82After printing the 2, the output will move to a new line for any subsequent output.C) #include <iostream>using namespace std;int main(){ int x = 0, y = 2; x = y * 4; cout << “I am the incredible”; cout << “computing machine”; cout << “\nand I will\namaze\n”; cout << “you.”; return 0;}The output from this program is shown below. Note that the cout commands do not leave a space between incredible and computing:I am the incrediblecomputing machineAnd I willamazeyou.D) #include <iostream>using namespace std;int main(){ int x = 0, y = 2; x = y * 4; cout << “Be careful\n”; cout << “This might/n be a trick ”; cout << “question\n; return 0;}The output from this program is shown below. Note that the /n is printed as written; the newline character, \n, uses the backslash character.Be carefulThis might/n be a trick questionAfter printing the second line, the output will move to a new line for any subsequent output.February 14 homework solutions Comp 106, L. S. Caretto, Spring 2006 Page 5E) #include <iostream>using namespace std;int main(){ int a, x = 23; a = x % 2; cout << x << endl << a << endl; return 0;}The output from this program is shown below.231After printing the1, the output will move to the next line for subsequent output.Page 78, problem 27 – There are a number of syntax errors in the following program. Locate as many as you can.*/ What’s wrong with this program /*#include iostreamusing namespace std;int main();} int a, b, c \\ three integers a = 3 b = 4 c = a + b Cout < “The value of c is %d” << C; return 0;{The corrected program is shown below with corrections in red and comments to the left of the corrected statements./* What’s wrong with this program? */ // Reverse the /* and */#include <iostream> // Add the < and > around iostreamusing namespace std;int main() // delete the semicolon following the ();{ // change to opening brace, { int a, b, c; // three integers // add semicolon and change \\ to // a = 3; // add the semicolon b = 4; // add the semicolon c = a + b; // add the semicolon cout << “The value of c is ” << c; // change Cout to cout; change < to // <<;, remove the %d and change C to c return 0;} // change to closing brace,


View Full Document

CSUN COMP 106 - Homework Solutions

Download Homework Solutions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework Solutions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework Solutions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?