0% found this document useful (0 votes)
39 views3 pages

CS 142L Programming Fundamentals Lab 1 Handout: Theory / Syntax / Example

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Page 1 of 3

DEPARTMENT OF COMPUTER SCIENCE


Rachna College of Engineering & Technology, Gujranwala
(a constituent college of UET Lahore)

CS 142L Programming Fundamentals Lab 1 Handout

Name: Registration No:


Marks: Evaluation Comments:

Lab objectives:
• In this lab you will get familiar with writing program in C++. Program parts, errors,
understanding error and basic program design.
• The process of converting source program into executable files via various stages
• Producing formatted output via various escape sequences
• Use of arithmetic operators on literals

Theory / Syntax / Example

1 About the C++


• Advance version of C language developed in 1980s by Bjarne Stroustrup
• High level compiled language
• C/C++ mostly used to write system programs
• Ada Augusta is known as first programmer of the history

2 Basic Program
//include header file for output
#include <iostream>
//use the std namespace
using namespace std;
//start the main function
int main()
{
//print something on screen
cout<<”Department of Computer Science RCET”;
return 0;
}

3 Arithmetic Operators
• +, -, *, /, % are arithmetic operators, they take two operands
Session BSCS 2021 1st Semester November 22, 2021 Teacher: Shahzad Aslam
Page 2 of 3

• % can be used to find remainder. It works only with integers


• / used to find quotient. For integer division it truncates the point

4 Output
• cout is called console output object. It is used to display on console screen
• << is called stream insertion operator that pushes given thing in a stream
• cout<<”A word”;
• Anything written in double quotes will appear as it is
• cout<<”2+3”; and cout<<2+3; are different
• cout<<”2” <<’+’<<”3 is “<<(2+3);
• endl takes output to new line

5 Escape sequences
• Used to format the output such a produce new line, show quotes, give carriage return or
backspace etc
• \n, \t, \r, \b, \a, \\, \’, \”, \f, \? Are escape sequences
• Each escape sequence is considered as single character

Lab Tasks
1. *Draw a diagram to show compilation process of C++ programs.
2. *Write a program to show diamond of stars using simply output statement. Use escape
sequence.
*
***
*****
*******
*********
*******
*****
***
*

3. *Print hollow rectangle of stars using simply output statement


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

4. *Input Print first letter of your name using stars. E.g E


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

Session BSCS 2021 1st Semester November 22, 2021 Teacher: Shahzad Aslam
Page 3 of 3

5. *Print your First name at the end of line then use escape sequence to print your last name at
the start of same line.
6. *Calculate and result of x3+y2+a. Let x is 2, y is 3 and a is 5. Program should have only one
output statement
7. *Write a program that prints following using escape sequences:

8. *Convert Temperature from Fahrenheit to Celsius. Let Fahrenheit temperature is 38.

9. *Write a single program which has following statements. Use minimum number of escape
sequences to produce the required results
Output statement Required Result
Cout<<”123456”; 136
Cout<<”Erase the first word”; thefirst wor
Cout<<”split the lines” Lit
Lines
Cout<<”Dont wait for someone”; Don’t wait
Cout<<”The time will always never be just “The Time will never be just right”
right.”
Cout<<”Is not it?”; Isn’t it?
Cout<<”We can split”<<endl<<”These line We can split
or the lines like so”; the lines like so
Cout<<”You sbatter best. Good. Never liet it Good batter best.
rest.” Never let it rest

Note: Tasks marked as * are required to be added in lab manual. Use separate A4 pages, tasks
should be hand written. You have to draw flow chart, and write pseducode where asked. Use
proper comments to describe your code where required.

Session BSCS 2021 1st Semester November 22, 2021 Teacher: Shahzad Aslam

You might also like