You are on page 1of 13

Department of Computing

CS 212: Object Oriented Programming

Class: BEE-13D
Fall 2022

Lab01: Fundamentals of C++

Date: 12th September, 2022

Time: 9:00 a.m. - 12:00 p.m.

Instructor: Mehreen
Tahir

Lab Engineer: Mehwish Kiran

CS 212: Object Oriented Page 2


Programming
LAB NO. 1

NAME: MUHAMMAD MAAHD JUNAID


CLASS: BEE-13D
SUBJECT: OOP
CMS ID: 368522

CS 212: Object Oriented Page 3


Programming
Objective
The main purpose of this lab is to revise the most basic concepts of C++ using Visual Studio.
Description
• C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form
programming language that supports procedural, object-oriented, and generic
programming.
• C++ is regarded as a middle-level language, as it comprises a combination of both high-
level and low-level language features.
• C++ was developed by Bjarne Stroustrup starting in 1979 at Bell Labs in Murray Hill,
New Jersey, as an enhancement to the C language and originally named C with Classes
but later it was renamed C++ in 1983.
• C++ is a superset of C, and that virtually any legal C program is a legal C++ program.
Use of C++
• C++ is used by hundreds of thousands of programmers in essentially every application
domain.
• C++ is being highly used to write device drivers and other software that rely on direct
manipulation of hardware under realtime constraints.
• C++ is widely used for teaching and research because it is clean enough for successful
teaching of basic concepts.
• Anyone who has used either an Apple Macintosh or a PC running Windows has
indirectly used C++ because the primary user interfaces of these systems are written in
C++.

CS 212: Object Oriented Page 4


Programming
LAB TASKS
1. Pascal's triangle is a triangular array of numbers in which those at the ends of the rows are
1 and each of the others is the sum of the nearest two numbers in the row above (the apex,
1, being at the top).

To build the triangle, start with "1" at the top, and then continue placing numbers below it in a
triangular pattern. Each number is the numbers directly above it added together. (Here I have
highlighted that 1+3 = 4)

This lab task was excluded by the instructor ( Miss Mehreen Tahir)
Activity#1
Run the following program and attach screen shot of output
Programs Output

CS 212: Object Oriented Page 5


Programming
THIS ACTIVITY WAS EXCLUDED BY INSTRUCTOR ( MISS MEHREEN TAHIR)

THIS ACTIVITY WAS EXCLUDED BY INSTRUCTOR ( MISS MEHREEN TAHIR)

CHARACTERISTICS OF ‘C++’ LANGUAGE


1. Modularity
Ability to breakdown a large module into manageable sub modules called as modularity
that is an important feature of structured programming languages. Advantages are:
 Projects can be completed in time.
 Debugging will be easier and faster.

2. Portability
The ability to port i.e. to install the software in different platform is called portability.
Highest degree of portability: ‘C++’ language offers highest degree of portability i.e.,
percentage of changes to be made to the sources code is at minimum when the software is
to be loaded in another platform. Percentages of changes to the source code are minimum.

CS 212: Object Oriented Page 6


Programming
3. Extendibility
Ability to extend the existing software by adding new features is called as extendibility.

CS 212: Object Oriented Page 7


Programming
Speed
‘C++’ is also called as middle level language because programs written in ‘c++’ language
run at the speeds matching to that of the same programs written in assembly language so
‘c++’ language has both the merits of high level and middle level language and because if
this feature it is mainly used in developing system software.

Activity#2:
Write a complete C++ program that produces the following output:

\/
\\//
\\\///
///\\\
//\\
/\
THIS ACTIVITY WAS EXCLUDED BY INSTRUCTOR

Lab Tasks:

2. Write a C++ program to print the area and perimeter of a circle.

CODE:

#include<iostream>
using namespace std;
int main()
{
int r,A,C;
cout << "Enter the radius of circle"<<endl;
cin >> r;
A = (3.14)*(r)*(r);
C = (2)*(3.14)*(r);
cout << "The Area of circle is" <<A<<endl;
cout << "The perimeter of circle is" <<C<<endl;
system("pause");
return 0;
}

SCREENSHOT:

CS 212: Object Oriented Page 8


Programming
3. Write a C++ program that accepts four integers from the user and prints equal if all
four are equal, and not equal otherwise.
CODE:
#include<iostream>
using namespace std;
int main()
{
int a, b, c, d;
cout << "Enter the four digits";
cin >> a;
cin >> b;
cin >> c;
cin >> d;
if ((a == b) && (b == c) && (c = d) && (d == a)){
cout << "The numbers are equal"<<endl;
}

else{
cout << "The numbers are not equal"<<endl;
}
system("pause");
return 0;
}
SCREENSHOT:

CS 212: Object Oriented Page 9


Programming
4. Write a C++ program to find the largest element of a given array of integers.
CODE:
#include <iostream>
using namespace std;

int main() {

int i, n;
float arr[500];

cout << "Enter total number of elements(1 to 500): ";


cin >> n;
cout << endl;

CS 212: Object Oriented Page


Programming 10
// Store number entered by the user
for (i = 0; i < n; ++i) {
cout << "Enter Number " << i + 1 << " : ";
cin >> arr[i];
}

// Loop to store largest number to arr[0]


for (i = 1; i < n; ++i) {

if (arr[0] < arr[i])


arr[0] = arr[i];

cout << endl << "Largest element = " << arr[0];


cout << endl;
system("pause");
return 0;

SCREENSHOT:

5. Write a C++ program to count the number of occurrences of given number in a sorted
array of integers.

CODE:
#include <iostream>
using namespace std;
int frequency_count(int arr[], int num, int size){
int count = 0;
for (int i = 0; i<size; i++){
if (num == arr[i]){
count++;
}
CS 212: Object Oriented Page
Programming 11
}
return count;
}
int main(){
int arr[] = { 0, 1, 1,5,1,1,2, 2, 3, 4 };
int num =1;
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Count of number of occurrences (or frequency) are: " << frequency_count(arr,
num, size);
cout << endl;
system("pause");
return 0;
}
SCREENSHOT:

6. Write a C++ program to update every array element by multiplication of next and
previous values of a given array of integers

THIS TASK WAS EXCLUDED BY THE INSTRUCTOR ( MISS MEHREEN


TAHIR)

7. Input 10 numbers and find their sum, Multiplication and average, also find prime
numbers from the entered numbers.

CODE:

#include<iostream>
using namespace std;
int main()
{
int a, b, c, A, d, e, f, g, h, i, j, S, M;
cout << "Enter the 10 numbers";
cin >> a;
cin >> b;
cin >> c;
CS 212: Object Oriented Page
Programming 12
cin >> d;
cin >> e;
cin >> f;
cin >> g;
cin >> h;
cin >> i;
cin >> j;
S = (a + b + c + d + e + f + g + h + i + j);
A = (a + b + c + d + e + f + g + h + i + j) / 10;

M = (a*b*c*d*e*f*g*h*i*j);
cout << "The average og given numbers is " << A << endl;
cout << "The Sum of the given numbers is " << S << endl;
cout << "The product of given numbers is " << M << endl;

system("pause");
return 0;

}
SCREENSHOT:

8. Find the average age for a group of students.


CODE:

int main()
{
int a, b, c,A,d;
cout << "Enter the age of students";
cin >> a;
cin >> b;
cin >> c;
cin >> d;
A = (a + b + c + d) / 4;
cout << "The average age of students is " << A << endl;

system("pause");
return 0;
CS 212: Object Oriented Page
Programming 13
}

SCREENSHOT:

Deliverables: Complete lab manual by performing all tasks. Copy paste your code and screen
shot of console window as a solution of each task. You are required to upload the lab tasks on
LMS and the name of the task must be in this format YourFullName_reg#.

CS 212: Object Oriented Page


Programming 14

You might also like