0% found this document useful (0 votes)
2 views14 pages

Lab 1

The document outlines a lab session on Data Structures and Algorithms, focusing on C++ programming concepts including variables, data types, flow control, functions, arrays, and object-oriented programming principles. It provides detailed explanations of static and dynamic arrays, along with practical coding tasks for array manipulation such as summing elements, finding maximum and minimum values, and calculating average marks. The lab is conducted by Instructor Jawad Hassan for students in the 3rd semester of the Fall 2024 session.

Uploaded by

shehzaibmirza428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Lab 1

The document outlines a lab session on Data Structures and Algorithms, focusing on C++ programming concepts including variables, data types, flow control, functions, arrays, and object-oriented programming principles. It provides detailed explanations of static and dynamic arrays, along with practical coding tasks for array manipulation such as summing elements, finding maximum and minimum values, and calculating average marks. The lab is conducted by Instructor Jawad Hassan for students in the 3rd semester of the Fall 2024 session.

Uploaded by

shehzaibmirza428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Revised Previous Concept

Data Structures & Algorithms


LAB 01

Lab Instructor: Jawad Hassan


Department of Artificial Intelligence

“ SHEHZAIB ”
S2024332016
Revised Previous Concept

SESSION: Fall-2024
SEMESTER: 3rd
Revised Previous Concept
C++ Introduction

Variables
Data Types
Basic I/O
Type Conversion
Operators
Comments

Flow Control

switch Statement
if...else
while Loop
do...while Loop
for Loop
break
continue

Functions

Default Functions
Parametrize Functions
Return Type Functions
Parametrize and Return Type Functions

Arrays

Array
One Dimensional Array
Two Dimensional Array
Revised Previous Concept

Object-Oriented Programming (OOP)


When studying data structures, several Object-Oriented Programming (OOP) concepts are
particularly important. These concepts help you design and implement data structures effectively
by leveraging the principles of encapsulation, abstraction, inheritance, and polymorphism.
Here are the key OOP topics that are most relevant to learning and applying data structures:
 OOP Introduction
 Class
 Object
 Data Members
 Member Member Functions
Unified Modeling Language to Code
 Class
 Object
 Data Members
 Member Member Functions
 Constructor
 Defualt
 Parametrize

Code to the given Scenarios


Code Practice
 Constructor
 Defuale
 Parametrize
 Destructor
 Passing Object into Function
 passing object by value
 passing object by reference
 Friend Function
Class Relationships
 Association
 Aggregation
 Composition
 Inheritance
Inheritence
 Single Inheritance
Revised Previous Concept
 Multiple Inheritance
 Multilevel Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance
Encapsulation
Abstraction
Polymorphism
Types of Polymorphism
Runtime Polymorphism
 Function Overloading
Compile Time Polymorphism
 Function Overriding
 Virtual Functions

Arrays
Array
Revised Previous Concept
In C++, an array is a variable that can store multiple values of the same type. For example,
Suppose a class has 27 students, and we need to store all their grades. Instead of creating 27 separate
variables, we can simply create an array

Types of Array

 Static Array:

Fixed-size array where the size is defined at compile time and cannot be changed at runtime.

 Dynamic Array:

Flexible-size array where memory is allocated at runtime, and the size can be modified as needed.

1. Size is fixed at compile time.


1. Size is flexible and determined at
runtime.

2. Memory is allocated on the heap using 2. Memory is allocated on the stack.


the new keyword.

3. It requires manual deallocation of 3. Efficient access but lacks flexibility to


memory using delete[] to avoid memory grow or shrink.
leaks.

 Static Array:
Revised Previous Concept

double grade[27];

dataType arrayName[arraySize];

int - type of element to be stored


x - name of the array
6 - size of the array
Access Elements in C++ Array:
In C++, each element in an array is associated with a number. The number is known as an array index.
We can access elements of an array by using those indices.

One Dimensional Array


You can declare and initialize a one-dimensional array in a single line in C++ like this:
Code:

Two Dimensional Array


you can declare and initialize a two-dimensional array in one line in C++ like this:
Revised Previous Concept
Code:

Task

1. Sum of Array Elements:


Create an array of integers and calculate the sum of all elements in the array. Print the
Revised Previous Concept

result to the console.


#include <iostream>
using namespace std;

int main(){

int size ;
cout<<"Enter Size of array = ";
cin>> size;

int arr[size];

cout <<"Enter Elements of array = ";


int i , sum = 0;
for( i = 0 ; i< size ; i++){

cin>>arr[i];

}
for( i = 0 ; i< size ; i++){
sum = sum + arr[i];
}
cout << "Sum = " <<sum;
}

2. Find Maximum and Minimum Values:


Declare an array of integers and write a program to find both the maximum and
minimum values in the array. Display these values.
#include <iostream>
Revised Previous Concept
using namespace std;

int main(){
int size;
cout <<"Enter size of array = " ;
cin>>size;

int arr[size];
int i;
cout <<"Enter Elements of array = ";
for(i = 0 ; i<size ; i++){

cin>> arr[i];
}
int max = arr[0];
for ( i= 0 ; i< size ; i++){

if (arr[i]> max){
max = arr[i];
}
}

cout <<" MAX = " << max <<endl;;

int min = arr[0];


for ( i= 0 ; i< size ; i++){

if (arr[i]< min){
min = arr[i];
}

}
cout <<" MIN = " << min ;
}

3.Find Size of Array


create a program to calculate sized of array
#include<iostream>
Revised Previous Concept
using namespace std ;
int main(){

int arr[] = {10 , 20 , 0 , -31, 56 ,17 , 100 , 37 };


int size = sizeof(arr)/sizeof(arr[0]);

for ( int i = 0 ; i<size ; i++){


cout <<arr[i] <<" ";
}

cout <<"\nSize = " << size;

4. Array Reversal:
Create an array of characters to store a word. Write a program that reverses the word in
the array and then prints the reversed word.
#include<iostream>
using namespace std;
int main(){

int size ;
cout <<"Enter size of WORD= ";
cin>> size ;
char arr[size];

cout <<"Enter WORD = " ;


for (int i = 0 ; i< size ; i++){

cin >> arr[i];

}
for (int i = size ; i>= 0 ; i--){

cout << arr[i];

}
Revised Previous Concept

6. Find Even Number:


Create an array of integers and find even number. Print the number array to the console.
#include <iostream>
using namespace std ;
int main(){

int size ;
cout <<"Enter size of arra = " ;
cin>> size ;

int arr[size ] ;
cout << "Enter Numbers = ";

for (int i = 0 ; i<size ; i++){

cin >> arr[i];


}
cout << "Even numbers are = ";
for (int i =0 ; i< size ; i++){

if (arr[i]%2== 0){

cout << arr[i]<< " ";


}

7: Print 2D array
Revised Previous Concept
#include <iostream>
using namespace std ;
int main(){
int rows , cols;

cout << "Enter Rows = ";


cin>> rows;
cout << "Enter Columns = ";
cin >>cols;

int arr[rows][cols];
cout << "Enter Elements = " ;
for (int i = 0 ; i<rows ; i++){
for (int j = 0 ; j<cols ; j++){

cin >> arr[i][j];


}

cout << "Elements = \n " ;


for (int i = 0 ; i<rows ; i++){
for (int j = 0 ; j<cols ; j++){

cout << arr[i][j] << " ";


}
cout <<endl;
}

8: Calculate Average Marks of Each Student


You have a 2D array where each row represents the marks of a student in 5 subjects.
Write a program to calculate and print the average marks of each student.

#include <iostream>
using namespace std;

int main() {

int students = 3;
Revised Previous Concept
int subjects = 5;

int marks[students][subjects];

for (int i = 0; i < students; i++) {


cout << "Enter marks for Student " << i + 1 << ":" << endl;
for (int j = 0; j < subjects; j++) {
cin >> marks[i][j];
}
}

int sum = 0;
for (int i = 0; i < students; i++) {

for (int j = 0; j < subjects; j++) {


sum += marks[i][j];
}

float average = static_cast<float> (sum) / subjects;


cout << "Average marks of Student " << i + 1 << ": " << average
<< endl;
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy