Chapter 2 - Introduction to C++

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

One operand is required for unary operator. Two operands are required for binary operator. Three operands are required for ternary operator. Examples: Unary operator ! represents logical not operator. If a=false, then !a=true. Notice that the operator ! takes one argument (one operand). Operator + is a binary operator. It takes two operands and result is their sum. Operator ?: is ternary operator. It is used as follows: a ? b : c It is interpreted as: "If a is true, then execute b and if a is false, then execute c".

How many operands does each of the following types of operators require? ______ Unary ______ Binary ______ Ternary

To define more than one variable in one statement we use comma to separate the variables. As such, to define double variables we use following statement: double temp, weight, age; Use semicolon to indicate end of the statement.

How may the double variables temp , weight , and age be defined in one statement?

To define more than one variable in one statement we use comma to separate the variables. To initialize the variable to some value use = operator. As such, to define int variables we use following statement: int months = 2, days, years = 3; Use semicolon to indicate end of the statement.

How may the int variables months , days , and years be defined in one statement, with months initialized to 2 and years initialized to 3?

Comment is written using multi-line comment symbols. Symbol /* represents the start of the paragraph we are about to put in comment and symbol */ represents the end of the commented paragraph.

Is the following comment written using single-line or multi-line comment symbols? /* This program was written by M. A. Codewriter*/

Comment is written using the single-line comment symbols. Symbol // represents the start of the comment. Everything in line that is written after // symbol is a part of the comment.

Is the following comment written using single-line or multi-line comment symbols? // This program was written by M. A. Codewriter

Start with compiling and running written example. As you can see, different parts of text are simply stacked one after another. There is no space between them. To modify it, we are going to use command \n which indicates new line. We want two blank rows between each part of text so we have to add three new line commands to the end of each part of the text - it is equivalent of pressing "Enter" key three times when writing something in notepad. int main() { cout << "Two mandolins like creatures in the\n\n\n"; cout << "dark\n\n\n"; cout << "Creating the agony of ecstasy.\n\n\n"; cout << " - George Barker"; return 0; } We don't need to add new line commands after the last line is printed out. Try to compile and run the program. Another way of solving this problem is to use endl command. Using this command, first line would look like this: cout << "Two mandolins like creatures in the" << endl << endl << endl;

Modify the following program so 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; }

In all these solutions we are going to assume that appropriate variables have already been declared. A.) To store value of variable a increased by 2 to variable b we use following line: b = a + 2; B.) To store value of variable b multiplied by 4 to variable a we use following line: a = 4 * b; C.) To store value of variable a divided be 3.14 to variable b we use following line: b = a / 3.14; D.) To store value of variable b reduced by 8 to variable a we use following line: a = b - 8; E.) To store 27 to variable a we use following line: a = 27; F.) To store character 'K' in variable c we use following line: c = 'K'; G.) Look at the ASCII table and fine the value of character 'B'. As you can see from the table, ASCII code for 'B' is 66. So, code we need is given below: c = 66;

Write assignment statements that perform the following operations with the variables a , b , and c . A) Adds 2 to a and stores the result in b . B) Multiplies b times 4 and stores the result in a . C) Divides a by 3.14 and stores the result in b . D) Subtracts 8 from b and stores the result in a . E) Stores the value 27 in a. F) Stores the character 'K' in c . G) Stores the ASCII code for 'B' in c .


संबंधित स्टडी सेट्स

Chapter 1 "Introduction to Professional Ethics"

View Set

NURS12154 Pharmacology for Nursing Practice

View Set

Chapter 2: Learning About Death (Socialization)

View Set

PrepU Chapter 65: Management of Patients with Oncologic or Degenerative Neurologic Disorders

View Set

Module 8: Upper GI EAQs (MEDSURG)

View Set

Fundamentals of Physics I Final Exam Review Fall 2017

View Set

Inquizitive: Incorporating Quotations

View Set

AP Psychology Commonly Missed Terms

View Set

SPPC: Musculoskeletal Ultrasound

View Set