CS 142L Programming Fundamentals Lab 1 Handout: Theory / Syntax / Example
CS 142L Programming Fundamentals Lab 1 Handout: Theory / Syntax / Example
CS 142L Programming Fundamentals Lab 1 Handout: Theory / Syntax / Example
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
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
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.
*
***
*****
*******
*********
*******
*****
***
*
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:
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