Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Dynamic Array, Pointers, Etc C++ Programming.

Uploaded by

aaahsan.ch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Dynamic Array, Pointers, Etc C++ Programming.

Uploaded by

aaahsan.ch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 05
(Spring 2024)

Course: Object Oriented Programming Lab Date: 12/03/2024


Course Code: Max Marks: 30
Faculty’s Name: Rizwan Khalid

Name: MUHAMMAD AHSAN Enroll No: 03-134232-053

Objective(s):
Understanding the basics of Dynamic Array, Pointers, etc C++ programming.

Question 1:

Write a program that asks the user to enter integers as inputs to be stored in the variables 'a' and
'b' respectively. There are also two integer pointers named ptrA and ptrB. Assign the values of 'a'
and 'b' to ptrA and ptrB respectively, and display them.

Program:
#include<iostream>

using namespace std;

int main()

int A, B, *ptrA, *ptrB;

cout<<"Ente Value of A:>";

cin>> A;

cout<<"Ente Value of B:>";

cin>> B;

ptrA = &A;
ptrB = &B;

cout<< "Value of A= "<< *ptrA<<endl;

cout<< "VAlue of B= "<< *ptrB<<endl;

return 0;

Question 2:

Write a C++ program to find the max of an integral data set. The program will ask the user to
input the number of data values in the set and each value. The program prints on screen a pointer
that points to the max value.

Program:
#include<iostream>

using namespace std;

int main()

int size;

cout<<"Enter the size of Integral sets :> ";

cin>>size;

int* array=new int [size];

int *pointer;
cout<<"Enter the Elements of sets :>"<<endl;

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

cin>> array[i];

int max;

max = array[0];

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

if(array[i]>max)

max=array[i];

pointer = &array[i];

cout<< " Adress of Max value :> " <<pointer<<endl;

cout<< "Max value :> " <<*pointer<<endl;

return 0;

Question 3:
Write a C++ program that create an array dynamically and fill it with integer, and print that
dynamic array. Make a function fill() and print() for enter data in array and print the data of
array.

Program:
#include<iostream>

using namespace std;

void fill(int input_array[], int size)

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

cin>> input_array[i];

void print( int output_array[],int size)

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

cout<< output_array[i]<<" ";

int main()

int size;
cout<<"Enter the size of Array :> ";

cin>>size;

int* array=new int [size];

cout<<"Enter the Elements of Array :>"<<endl;

fill( array, size);

cout<<"Entered Elements of Array :>"<<endl;

print( array, size);

return 0;

Question 4:

Write a C++ program that creates two arrays dynamically, copies the elements of the first array
into the second array. You can make input(), Copy() and Print() functions, respectively. In
print function print both arrays your enter array and your copy array.

Program:
#include<iostream>

using namespace std;

void input( int input_array[], int size )


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

void Copy( int first_array[] , int second_array[] , int size)


{
for(int i=0; i<size; i++)
{
second_array[i] = first_array[i];
}
}

void Print( int output_array[], int size )


{
for(int i=0; i<size; i++)
{
cout<< output_array[i]<< " ";
}
}

int main()
{
int size;
cout<<"Enter the size of Integral sets :> ";
cin>>size;
int* first_array=new int [size];
int* second_array=new int [size];
cout<<"Enter the Elements of Array :>"<<endl;

input( first_array , size );

Copy( first_array , second_array , size);


cout<<" Your entered array is sucessfully copied in another array "<<endl;
cout<<"The resultant array is :> ";
Print( second_array , size );

return 0;
}

Question 5:

Write a C++ program that creates an array dynamically, sort the elements of the array in
ascending order and print both unsorted and sorted arrays. You can make input() and sorted()
functions, respectively. Print both arrays your enter array and your sorted array.

Program:
#include<iostream>

using namespace std;


void input(int input_array[], int size)
{
for(int i=0; i<size; i++)
{
cin>> input_array[i];
}
}
void sorted( int sorting[], int size)
{
int temporary;
for(int i=0 ; i<size ; i++)
{
for(int j=i+1 ; j<size ; j++)
{
if(sorting[i]> sorting[j] )
{
temporary = sorting[i];
sorting[i] = sorting[j];
sorting[j] = temporary;
}
}

}
}

int main()
{
int size;

cout<<"Enter the size of Array :> ";


cin>>size;
int* array=new int [size];
cout<<"Enter the Elements of Array :>"<<endl;
input( array, size);

sorted( array , size);

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


{
cout<<array[i]<<" ";
}

return 0;
}
Question 6:

Write a C++ programs which reads a text file and calculate its size. The size you calculated will
be passed as a parameter to another function, to dynamically allocate memory. Then read the file
element and store in the array and finally display the array.

int c_filesize(char* filename);

int* dynamic_mem(int s);

void read(char* filename,int* arr,int s);

void display(int* ptr,int s);


Main

int size=0;

int* arr=NULL;

char

f_name[20]="data.txt";

size=c_filesize(f_name);

arr=dynamic_mem(size);

read(f_name,arr,size);

display(arr,size);

Program:
#include <iostream>
#include <fstream>

using namespace std;

int c_filesize(char* filename) {


ifstream file(filename);
if (!file.is_open()) {
cout << "Error: Unable to open file '" << filename << "'" << endl;
return -1;
}
int size = 0;
char ch;
while (file.get(ch)) {
++size;
}
file.close();
return size;
}

int* dynamic_mem(int size) {


return new int[size];
}
void read(char* filename, int* arr, int size) {
ifstream file(filename);
if (!file.is_open()) {
cout << "Error: Unable to open file '" << filename << "'" << endl;
return;
}
for (int i = 0; i < size; ++i) {
file >> arr[i];
}
file.close();
}

void display(int* ptr, int size) {


for (int i = 0; i < size; ++i) {
cout << ptr[i] << " ";
}
cout << endl;
}

int main() {
int size = 0;
int* arr = NULL;
char f_name[20] = "data.txt";

if (size <= 0) {
return 1;
size = c_filesize(f_name);
}

arr = dynamic_mem(size);
read(f_name, arr, size);
cout << "Data read from file:" << endl;
display(arr, size);
delete[] arr;

return 0;
}

Lab 05 Extension
Topic Dynamic Memory using double pointer

 Double Pointer and Function


o Need of double pointer.
o Declaration of double pointer.
o Dereferencing double pointer.
o Difference between single and double pointers.
 Dynamic Memory Allocation & Double Pointer
Objective
o Dynamic Memory allocation involving double pointer.
o Creating 2D array dynamically using double pointer.
o Explaining benefits of creating 2D arrays dynamically.
o Compare the memory allocation and memory mapping of static 2D array and
dynamic 2D array.
o Discuss the use of double pointer as parameter with 2D static and 2D dynamic

Lab Description:

This lab is basically designed for the use of double pointer, 2D dynamic memory and use of double
pointer with functions.

Double Pointer:

We already know that a pointer points to a location in memory and thus used to store the address of
variables. So, when we define a pointer to pointer. The first pointer is used to store the address of the
variable. And the second pointer is used to store the address of the first pointer. That is why they are
also known as double pointers.

Declaration:

Declaring double pointer is similar to declaring pointer in C++. The difference is we have to place an
additional ‘*’ before the name of pointer.

Syntax:

int **ptr; // declaring double pointers

Initialization:

Initializing double pointer is similar to initializing pointer in C++.

Syntax:

int **ptr=nullptr; // initializing double pointers


Double pointer working:

2D dynamic memory:

As we discuss earlier we can allocate an array dynamically using pointer. When we need multiple array
we use 2D array instead of 1D array. We can also create a 2D dynamic memory.

How can we create 2D dynamic array?

First we need to create an array of pointers.

int **ptr = new int*[row];

Each index of array of pointers represent a row.


Now we create arrays for each row one by one.

for (int r = 0; r < row; r++)

ptr[r] = new int[col];

Pointers and 2D dynamic Array:

In a two dimensional array, we can access each element by using two subscripts, where first subscript
represents the row number and second subscript represents the column number. The elements of 2-D
array can be accessed with the help of pointer notation also. Suppose arr is a 2-D array, we can access
any element arr[r][c] of the array using the pointer expression *(*(arr + r) + c). Now we’ll see how this
expression can be derived.

Deallocation of 2D dynamic Memory:

We must delete the dynamic memory if there is no more use of that memory.

We follow the steps in reverse order of memory allocation.

First we delete each row than we delete the array of pointers.

Example:

for (int r = 0; r < row; r++)

delete[] ptr[r];

delete [] ptr;

Use of double pointer with function:

We can pass a double pointer as a parameter to a function and return double pointer as well.

Example:

Input File:
First integer of file shows the number of rows. Second integer of file shows the number of cols.
Function Prototypes:

Function Calling:
Function Definition:
Lab Tasks
Image Editor Problem:

Images are the most common part of our life. It holds different memories. But in some case we want to
edit our image. We can apply different effect on are image. These effect are applied on images using
some functionality. Let’s discuss some of the functionalities.

Functionality: Blur

Purpose:

In this functionality you have to blur the input image.

Method:

For this we use average method. Now the question is what is average method? For each index of
a 2-D Matrix you have to calculate the average of its neighbor indexes. Each index have 8
neighbor in case of 3x3 average (not boundary cases). Your task is to calculate the average
value of those 9 index (8 neighbors and 1 itself) and place the average value on output matrix.

How to handle boundary cases:

As in boundary cases we should extend our input image as well. To do that we are to add extra
rows at the top and at the bottom of the image and add extra columns to the left and to the
right. Which contain value 0. Number of extra row and column depends on the size of average
scale.

Example 3x3:

We add 1 extra row at the top and at the bottom of the image and 1 extra columns to the left
and to the right.

Program:
#include <iostream>
using namespace std;

void calculateAverage(int inputMatrix[][3], int outputMatrix[][3]) {

for (int i = 1; i < 2; ++i) {

for (int j = 1; j < 2; ++j) {

int total = 0;

for (int x = -1; x <= 1; ++x) {

for (int y = -1; y <= 1; ++y) {

total += inputMatrix[i + x][j + y];

int average = total / 9.0;

outputMatrix[i][j] = average;

int main() {

int inputMatrix[3][3] = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

int outputMatrix[3][3] = {0};


calculateAverage(inputMatrix, outputMatrix);

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

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

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

cout << endl;

return 0;

Example 5x5:

We add 2 extra row at the top and at the bottom of the image and 2 extra columns to the left
and to the right.

Functionality: Crop

Purpose:

In this functionality you have to crop the image.

Method:

Get input from user from where to where user want to crop the image. Now verify the input if
input is in given rage (between row and column size of image) than crop the image according to
input. The output image will be smaller in size (row and column).

Functionality: Addborder
Purpose:

In this functionality you have to add border to the image.

Method:

We should extend our output image. To do that we are to add extra rows at the top and at the
bottom of the image and add extra columns to the left and to the right. Which contain value 0.
The more number of extra rows and columns creates the thicker border.

Functionality: Zoom

Purpose:

In this functionality you have to zoom in or zoom out the image.

Method:

For this we use nearest neighbor method. For zoom in we replicate the image index. And for
zoom out we pick 1 value between 4 values.

Example zoom in: Example zoom out:


1 1 2 2 1 2 3 4
1 2
1 3
1 1 2 2 5 6 7 8
3 4
9 11
3 3 4 4 9 10 11 12

3 3 4 4 13 14 15 16
Functionality: Remove color

Purpose:

In this functionality we convert color image into gray scale image.

Method:

For this we use average method. Average method is the simplest one. You just have to take the
average of three colors. Since it’s an RGB image, so it means that you have add the values of r
with g with b and then divide it by 3 to get your desired grayscale image value.

You might also like