labsheet1stud

10
F2037 PROGRAMMING FUNDAMENTAL OF C++ LAB 1: VARIABLE, KEYWORD AND DATA TYPES Objectives By the end of this lab, students should be able to : Describe the structure of C++ programmes Write, compile and run simple C++ programmes Identify and list keywords List and define the various data types Define variables and constants Theory/ Topics A program must have the function named main(). Structure of C++ programmes o The structure of a simple C++ programme is similar to the structure of C. Structure Program 1

Upload: rohassanie

Post on 25-May-2015

580 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Labsheet1stud

F2037 PROGRAMMING FUNDAMENTAL OF C++

LAB 1: VARIABLE, KEYWORD AND DATA TYPES

Objectives

By the end of this lab, students should be able to : Describe the structure of C++ programmes Write, compile and run simple C++ programmes Identify and list keywords List and define the various data types Define variables and constants

Theory/ Topics

A program must have the function named main(). Structure of C++ programmes

o The structure of a simple C++ programme is similar to the structure of C.

Structure Program

< Comment Entry>

< Preprocessor directives >

main function { < declaration stat >; < C++ Statements >; }

// First C++ program

#include <iostream>#include <string>

int main() { int a; cout << "Welcome to Programming \n”; return 0; }

Table 1.1 : Structure of C++ Programme

Consider the code in the given program:1. // is used to comment a single line. In addition to

// symbol, C++ supports /* */ for comment entry

1

Page 2: Labsheet1stud

F2037 PROGRAMMING FUNDAMENTAL OF C++

operation. /* */ is used to comment a set of statements.

2. #include <iostream> includes the header file for the program.

3. main() is the function where the program is written.

4. int a; is the variable declaration.5. cout is used to display the output statements.6. Every statement is terminated with a semi-colon,

similar to C.

Keywords - have a strict meaning as individual tokens in C++. They cannot be redefined or used in other contexts.

Identifier - Sequence of letters, digits and the special character "_" which is called an underscore. A letter or underscore must be the first character of an identifier.

As C++ program is built from C, the C++ compiler supports all the features of C.

The following are the steps involved in writing, compiling and executing a C++ program :

1. Open Microsoft Visual C++ and type the program.2. Save the file with the corresponding extension

(filename.cpp)3. Compile the program. 4. Build & execute/run the program.

Lab 1A

2

Page 3: Labsheet1stud

F2037 PROGRAMMING FUNDAMENTAL OF C++

Procedure :

Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as lab1A.cpp.

The following program finds the sum of two numbers and displays it.

// Program to add two numbers

#include <iostream>using namespace std;

void main() { int a, b, sum; a = 5; b = 2; sum = a + b; cout << "The sum is: " << sum << "\n"; }

Lab 1B

Procedure :

Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Change the statement cout << "The sum is: " << sum; in line 9 to cout << "The average is: " << sum/2; Step 4: Save the program as lab1B.cpp.

// Program to find the average of two numbers#include <iostream>

3

Page 4: Labsheet1stud

F2037 PROGRAMMING FUNDAMENTAL OF C++

using namespace std;

void main() { int a, b, sum; a = 5; b = 2; sum = a + b; cout << "The sum is: " << sum; }

Lab 1C

Procedure :

Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as lab1C.cpp.

// The following program illustrates variable and // constant declaration.

#include <iostream>using namespace std;

const float PI = 3.14;

void main() { double radius = 3.0; double circumference; circumference = 2 * PI * radius; cout << "Circumference = " << circumference <<endl; }

Lab 1D

4

Page 5: Labsheet1stud

F2037 PROGRAMMING FUNDAMENTAL OF C++

Procedure :

Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as lab1D.cpp.

// The following program illustrates variable and // constant declaration.

#include <iostream>#define PI 3.14using namespace std;

void main() { double radius = 3.0; double circumference; circumference = 2 * PI * radius; cout << "Circumference = " << circumference <<endl; }

Lab 1E

Procedure :

Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as lab1E.cpp.

Program to show the declaration and initialization of variables with float, double, char, int and boolean data type.

#include <iostream>using namespace std;void main() { char grade = 'F';

5

Page 6: Labsheet1stud

F2037 PROGRAMMING FUNDAMENTAL OF C++

float price = 77.01; double average = 145525.92; bool boolean_variable = true; int age = 50; cout << price <<"\t"<< average <<"\t"<<grade<<"\t"<< boolean_variable <<"\t"<<age<<endl; }

Lab 1F

Procedure :

Step 1: Type the programs given belowStep 2: Compile and run the program. Write the output.Step 3: Save the program as lab1F.cpp.

Program to show the declaration and initialization of variables with string data type.

// my first string#include <iostream>#include <string>using namespace std;

void main (){ string mystring = "This is a string"; cout << mystring;}

LAB EXERCISE

6

Page 7: Labsheet1stud

F2037 PROGRAMMING FUNDAMENTAL OF C++

1. Describe the functionality of using a. #include <string> as Preprocessor directivesb. int main (void) as main function

2. For each statement below, state either variable or constant and find a suitable variable and constant name a. the number of month in a yearb. the sum of x + y if given x = 5 and y = 10

3. Based on IPO chart information below:a. Declare the variable in C++ by using the

appropriate data typeb. Transform the Algorithm into C++ code

IPO Chart InformationInputNumber of late days = 7Number of late charge = 0.2

ProcessingCalculate amount

Algorithm1. Declare the number

of late days, late charges and amount

2. Calculate the amount by multiplying the number of late days with late charge

3. Display amount

OutputDisplay amount

7