tutorial 1 chapter 1 and 2 exercises1. 2 exercise 1 which of the following identifiers are: main...

12
Tutorial 1 Chapter 1 and 2 Exercises 1

Upload: angel-paul

Post on 02-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

Tutorial 1

Chapter 1 and 2

Exercises1

Page 2: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

2

Exercise 1

Which of the following identifiers are:

main1. invalid identifiers

2. valid identifiers

and why?

double

2time

G

sue’s

return

cout

xzy123

part#2

#insert

this_is_along_one

Page 3: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

3

Show the output displayed by the following program lines when the data entered are 5 and 7 respectively.

Cout<<“Enter two integers“;Cin>>m>>n;m = m + 5;n = 3 * n;Cout<<“m =“<<m<<endl;Cout <<“n=“<<n;

Exercise 2

Page 4: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

4

Show the output displayed by the following lines if the value of exp=11.

Cout<<“My name is “;Cout<<“Hoda Ahmed.”;Cout<<endl;Cout<<“I live in “;Cout<<“Riyadh, Olaya Street “<<endl;Cout<<“and I have “ << exp << “ years “;Cout<<“of programming experience.”;

Cout<<“ *****\n****\n***\n**\n*\n; “

Exercise 3

Page 5: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

5

Write a program that draws a simple triangle of stars on screen.

Exercise 4

Exercise 5

Write 2 separate programs that show the following:1.)Hello, World!2.)Hello, World!

***************

Page 6: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

********************Salary 2500Overtime 1250Total-Sal= 3750********************

Exercise 6:

Draw a flowchart then write a program that calculates the total salary, Given that a user will enter the salary and overtime.

Note: Total-Sal = Salary +Overtime

Page 7: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

Please enter first number: 5.5Please enter second number: 2.5The Product is 13.75

Draw a flowchart and write a program to calculate the product of two numbers .

Your output must be:

Exercise 7:

Page 8: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

Exercise 8:

Where possible, write equivalents for the following statements using

compound assignment operators:

a) r = r / 10;

b) z = z * x + 1;

c) q = q + r * m;

 

Exercises8

Page 9: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

Exercise 9:

Find the value of x, when j = 5 and k = 2:

a) x = j +1*k-3;

b) x = k-j*2(j+1);

c) x = j++ - ++k;

d) x = j++;

e) X=++k;

Exercises9

Page 10: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

Exercise 10:

State the order of evaluation of the operators in each of the following C statements and show the value of x after each statement is performed

• x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 1+2 ) ) ) );

Exercises10

Page 11: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

Exercises11

Exercise 11:Assume the following:int j = 6; int k = 10; int n; bool b = false; Give the value that is assigned, or illegal.1.__________ n = k++;2.__________ n = (k++);3.__________ n = ++k;4.__________ n = 7++;5.__________ n = k++ + ++j;6.__________ n = k+++++j;7.__________ n = k = j = 5;8.__________ n = k = (j = 5);9.__________ n = (k = j) = 5;10.__________ 3 = 4;11.__________ n = k; n += 1;12.__________ n = k; n *= 2;13.__________ n = k; n /= 2;

Page 12: Tutorial 1 Chapter 1 and 2 Exercises1. 2 Exercise 1 Which of the following identifiers are: main 1.invalid identifiers 2.valid identifiers and why? double

Exercise 12:

Assume that: double y = 50.964;

Do the following :• Print the integer part of y• Print y on 10 places• Print y on 7 places and fill the blank places with ‘#’• Print 1 number after the decimal point• Print 2 numbers after the decimal point • Store the integer part of y in x• Add 30.9 to y and store the integer part of the result in z

Exercises12