CENG 106 Lab1

4
Qatar University College of Engineering Dept. of Computer Science & Eng’g Computer Programming - GENG106 - Lab Handbook Fall 2012 Semester Lab1: Introduction to C++ Objectives: At the end of this lab, you should be able to: Use C++ IDE to write, debug, and run simple programs. Use input / output statements in C++. Use the assignment statement. Use data types and arithmetic operators in the program. Quick Review: A C++ program must have a main function that acts as the entry point to start the program execution. The main function starts with a left brace “{“ and ends with a right “}”. Each statement ends with a semicolon “;”. C++ fundamental data types are classified as: o floating-point types (which include float, double and long double); and o integral types (which include: bool, char, signed char, unsigned char, short int, unsigned short int, int, unsigned short int, long int and unsigned long int). Strings in C++ are represented using the string class. Unlike fundamental data types, string is not built into C++; it is added to the language through a library. C++ is a case-sensitive language (e.g., int grade is different from int Grade). 1) Type the following program, then save and run it: #include <iostream> #include <string> using namespace std; int main() { string firstName; cout <<"Please enter your first name: "; cin >> firstName; cout<<"Hello Engineer "<< firstName << "\n"; cout<<"Welcome to programming in C++\n"; return 0; } Write down the output of the program ………………………………………………………………………………………………. ………………………………………………………………………………………………. Add \n after the word “Welcome” in the above program. What is the effect of \n? Write down the output ……………………………………………………………………………………………….

Transcript of CENG 106 Lab1

Page 1: CENG 106 Lab1

Qatar University

College of Engineering

Dept. of Computer Science & Eng’g

Computer Programming -

GENG106 - Lab Handbook

Fall 2012 Semester

Lab1: Introduction to C++ Objectives:

At the end of this lab, you should be able to:

Use C++ IDE to write, debug, and run simple programs.

Use input / output statements in C++.

Use the assignment statement.

Use data types and arithmetic operators in the program.

Quick Review:

A C++ program must have a main function that acts as the entry point to start the program

execution.

The main function starts with a left brace “{“ and ends with a right “}”.

Each statement ends with a semicolon “;”.

C++ fundamental data types are classified as:

o floating-point types (which include float, double and long double); and

o integral types (which include: bool, char, signed char, unsigned char, short int, unsigned short int, int, unsigned short int, long

int and unsigned long int).

Strings in C++ are represented using the string class. Unlike fundamental data types, string is

not built into C++; it is added to the language through a library.

C++ is a case-sensitive language (e.g., int grade is different from int Grade).

1) Type the following program, then save and run it:

#include <iostream>

#include <string>

using namespace std;

int main()

{

string firstName;

cout <<"Please enter your first name: ";

cin >> firstName;

cout<<"Hello Engineer "<< firstName << "\n";

cout<<"Welcome to programming in C++\n";

return 0;

}

Write down the output of the program

……………………………………………………………………………………………….

……………………………………………………………………………………………….

Add \n after the word “Welcome” in the above program. What is the effect of \n?

Write down the output

……………………………………………………………………………………………….

Page 2: CENG 106 Lab1

1

2) Write a program that will print the letter T using the character (*) as follows:

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

*

*

*

*

*

Hint: Use \t and \n escape sequences.

3) Type the following program, then save and run it:

#include <iostream>

using namespace std;

int main()

{

int a,b,c;

cout<<"Enter first number:";

cin>>a;

cout<<"Enter second number:";

cin>>b;

c=a+b;

cout<<"Total="<<c<<endl;

return 0;

}

Write down the output of this program

…………………………………………………..

Modify the program above so it also prints the difference and the product of the two read

numbers.

In order to calculate the average, add these statements to the end of your program, but before

the last brace:

int avg=c/2;

cout<<"Average="<<avg<<endl;

Run the program and enter 8 for first number and 3 for second number. Is the result of the

average correct? Explain and fix this problem.

……………………………………………………………………………………………….

……………………………………………………………………………………………….

4) Write a program that reads two characters from the keyboard and then print them on the screen.

5) Write a program to calculate the value of from the following

where and are integers entered by the user.

Page 3: CENG 106 Lab1

2

6) Write a C++ program that asks the user

to enter his/her mass (in kilograms, kg)

and height (in meters, m). Let your

program compute and output the Body

Mass Index, BMI for the user. The BMI

formula is

7) Write a C++ program that reads the temperature

represented in degree Celsius ( ) and output the

equivalent in the following units:

a. Fahrenheit ( ) where 325

9 CF

b. Kelvin( ) : where = +273.15

c. Rankine ( ) where =

8) Write a C++ program that computes the area of a triangle with base and height . Use the formula:

9) Write a C++ program that reads the radius r of a circle, then computes and prints the diameter,

the circumference, and the area of the circle. Assume that π=3.14, and use the following

formulas:

Diameter = 2r

Circumference = 2 π r

Area = π r2

10) The axial stress, (sigma) in a structural member is

the force, divided by the cross-sectional area of that

member, , that is to say:

The picture in the right shows the stress in a member

of a circular cross-section.

Page 4: CENG 106 Lab1

3

Write a C++ program that reads the force N (in Newton) and the radius (in cm) of the circular

cross-section, and then outputs the stress value. The SI unit for stress is the Pascal (symbol Pa),

which is equivalent to one Newton (force) per square meter (unit area).

11) Write a C++ program that reads three resistances R1,

R2, and R3 which are connected in parallel. Let your

program output the equivalent resistance, R, which is

found from the formula: