Dynamic Array, Pointers, Etc C++ Programming.
Dynamic Array, Pointers, Etc C++ Programming.
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>
int main()
cin>> A;
cin>> B;
ptrA = &A;
ptrB = &B;
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>
int main()
int size;
cin>>size;
int *pointer;
cout<<"Enter the Elements of sets :>"<<endl;
cin>> array[i];
int max;
max = array[0];
if(array[i]>max)
max=array[i];
pointer = &array[i];
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>
cin>> input_array[i];
int main()
int size;
cout<<"Enter the size of Array :> ";
cin>>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>
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;
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>
}
}
int main()
{
int size;
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 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>
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
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:
Initialization:
Syntax:
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.
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.
We must delete the dynamic memory if there is no more use of that memory.
Example:
delete[] ptr[r];
delete [] ptr;
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:
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.
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;
int total = 0;
outputMatrix[i][j] = average;
int main() {
int inputMatrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
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:
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:
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:
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.
3 3 4 4 13 14 15 16
Functionality: Remove color
Purpose:
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.