first homework, starting out with c++

12
First Homework, Programming Languages. José Giles ID number: 21530021 2.15 Write a C++ statement that de nes the double variables temp, weight, and height all in the same statement. Answer: double temp, weight, height; 2.25 (A) What will the following programs print on the screen? Answer: The output will be: 0 100 Because even though we initialized the variables as “freeze = 32, boil = 212”, we changed the values before printing, so the output will be the last value stored in the variables (freeze = 0, boil = 100) 2.27 There are a number of syntax errors in the following program. Locate as many as you can. 1- In the first line they use the following format to comment the line:

Upload: jose-arturo-giles-hernandez

Post on 11-Nov-2015

27 views

Category:

Documents


0 download

DESCRIPTION

This is my homework for my class of Programming Languages, we used the book Starting Out With C++: Early Objects, Seventh Edition. This Homework just include the first and second chapter.

TRANSCRIPT

First Homework, Programming Languages.

Jos Giles ID number: 21530021

2.15 Write a C++ statement that denes the double variables temp, weight, and height all in the same statement.

Answer: double temp, weight, height;

2.25 (A) What will the following programs print on the screen?

Answer: The output will be: 0100

Because even though we initialized the variables as freeze = 32, boil = 212, we changed the values before printing, so the output will be the last value stored in the variables (freeze = 0, boil = 100)

2.27 There are a number of syntax errors in the following program. Locate as many as you can.

1- In the first line they use the following format to comment the line: */ comment/* And it has to be in this way: /* your comment here */ (Starting the comment with backward slash followed by an asterisk and finishing the comment with an asterisk followed by a backward slash)Also if you want to comment just one line, its better using // instead of /* comment */ .

2- In the second line the preprocessor directive it is written incorrectly, iostream has to be inside brackets, like this: #include

3- In the fourth line we have another error, because it is written in this way:int main();With a semicolon at the end, but the correct way is without the semicolon, like this: int main()

4- The braces are written in the other way around. The first brace (opening brace) has to be { and the last brace (closing brace) has to be }

5- They defined the variables a, b and c without using a semicolon at the end of the statement. Also they forgot to put the semicolon when they initialized the values of the variables a, b and c. The correct way is the following:

int a,b,c; // Three integersa=3;b=4;c=a+b;

I can see some errors at the moment to print out the value of c. The first one is that Cout it is written with capital C, and it has to be written in lower case. The second error in this statement is that they are using just one bracket, and it has to have two brackets (