Lab Repor10
Lab Repor10
Report
Lab Session 10
1 | Page
Introduction:
Conceptual Takeaways:
Background:
We were not new to concept of arrays as we had used both static and
dynamic arrays before in lab sessions and had a grasp over them we used
pointers this time along with arrays that made our problem solving much
easier.
Lab Tasks:
Tasks: Write pseudo code and C++ code for the following problems.
2 | Page
1. Write a C++ program to demonstrate the declaration and initialization
of a pointer variable.
2Create a program that swaps two variables using pointers.
3Write a program that takes an array (static) as input and finds the
largest element
using pointers.
4 Create a program to find the sum of all elements in an array (static)
using pointer arithmetic.
Implement a program to multiply corresponding elements of two arrays
(static) using pointers.
6 Write a program that sorts an array (static) in ascending order using
pointers.
7Create a program that copies the contents of one array (static) to
8another using pointers.
9Write a program that creates a dynamic array of user defined length
and initializes it.
10Write a program that deletes a dynamic array of user defined length
after using.
11Write a program that reads an integer n from the user, dynamically
allocates memory for an array of n integers, and then finds the sum of
those integers.
Develop a program to find the average of elements in a dynamic array.
12 Create a program that dynamically allocates memory for a string
(char array), copies a user-input string into it, and then reverses the
string using pointers.
13. Write a function that takes two dynamic arrays as input, multiplies
corresponding elements, and returns the resulting array.
3 | Page
Brainstorming Tasks
1. Implement a program that reads a sentence (a string with spaces) from
the user, dynamically allocates memory (user defined) for it, and then
counts the number of words.
2. Develop a program that reads a list of integers from the user until they
enter a negative number. Store these numbers in a dynamically allocated
array and display the elements of the dynamic array.
Task 1:
PSEUDO Code:
This C++ code is a simple program that demonstrates the use of pointers to
store the memory address of a variable and then prints that memory address.
Here's a step-by-step explanation of the code:
4 | Page
In summary, this program declares an integer variable a, a pointer p, assigns
the memory address of a to p, and then prints the memory address stored in
the pointer p. The output will be the memory address of the variable a.
Task 2:
Pseudo Code:
Certainly, here's a step-by-step explanation in a concise manner:
5 | Page
2. Declare a temporary integer variable t.
3. Print the original values of a and `b.
4. Declare two integer pointers p and q.
5. Point p to the address of a and q to the address of b.
6. Copy the value pointed to by p (value of a) into t.
7. Copy the value pointed to by q (value of b) into p to swap a and b.
8. Copy the value from t into q.
9. Print the new values of a and `b (which have been swapped).
Task 3:
Pseudo Code:
1. #include<iostream>: This line includes the input/output stream library, which provides
functionality for input and output operations.
2. using namespace std;: This line is a using directive, allowing you to use names from
the std namespace (standard C++ library) without explicitly specifying the namespace
each time.
3. int main(): This is the main function, the entry point for the program, which returns an
integer value (0 in this case) to indicate the program's exit status.
4. int x;: Declares an integer variable x to store the number of elements the user wants
to input.
5. cout << "enter number of elements \n";: This line prompts the user to enter the
number of elements they want to input.
6. cin >> x;: Reads the user's input and stores it in the variable x.
7. int *p;: Declares an integer pointer variable p.
6 | Page
8. int a[x];: Declares an integer array a with a size of x elements. The size of the array is
determined by the value entered by the user.
9. int max = 0;: Initializes an integer variable max to 0, which will be used to store the
maximum value from the array.
10. p = a;: Assigns the address of the array a to the pointer p.
11. The program uses a loop to input elements into the array and find the maximum value:
Task 4:
Pseudo Code:
7 | Page
1. #include<iostream>: This line includes the input/output stream library, which provides
functionality for input and output operations.
2. using namespace std;: This line is a using directive, allowing you to use names from
the std namespace (standard C++ library) without explicitly specifying the namespace
each time.
3. int main(): This is the main function, the entry point for the program, which returns an
integer value (0 in this case) to indicate the program's exit status.
4. int x;: Declares an integer variable x to store the number of elements the user wants
to input.
5. cout << "enter the number of elements: \n";: This line prompts the user to enter
the number of elements they want to input.
6. cin >> x;: Reads the user's input and stores it in the variable x.
7. int *p;: Declares an integer pointer variable p.
8. int a[x];: Declares an integer array a with a size of x elements. The size of the array is
determined by the value entered by the user.
9. int sum = 0;: Initializes an integer variable sum to 0, which will be used to store the
sum of the elements in the array.
10. p = a;: Assigns the address of the array a to the pointer p.
11. The program uses a loop to input elements into the array and calculate their su
8 | Page
Task 5:
Pseudo Code:
1. include<iostream>: Includes the input/output stream library for input and output
operations.
2. using namespace std;: Allows you to use names from the std namespace without
explicitly specifying the namespace each time.
3. int main(): The main function, which is the entry point for the program.
4. int x;: Declares an integer variable x to store the number of elements for the arrays.
5. cout << "enter number of elements : \n";: Prompts the user to enter the number
of elements.
6. cin >> x;: Reads the user's input and stores it in the variable x.
7. Declares three integer arrays a, b, and c, each with a size of x.
8. Declares two integer pointers p and q.
9. The program uses two separate loops to input elements for the two arrays a and b:
a. The first loop prompts the user to enter values for array a.
b. The second loop prompts the user to enter values for array b.
10. After filling the arrays a and b, the pointers p and q are assigned to point to the first
elements of the respective arrays.
11. The program calculates the element-wise product of arrays a and b and stores the
results in array c using a loop:
9 | Page
Task 6:
Pseudo Code:
1. #include<iostream>: Includes the input/output stream library for input and output
operations.
2. using namespace std;: Allows you to use names from the std namespace without
explicitly specifying the namespace each time.
3. int main(): The main function, which is the entry point for the program.
4. int x;: Declares an integer variable x to store the number of elements for the array.
5. cout << "enter the number of elements: \n";: Prompts the user to enter the
number of elements.
6. cin >> x;: Reads the user's input and stores it in the variable x.
10 | P a g e
7. Declares integer variables max and two integer arrays a and b, each with a size of x.
8. Declares two integer pointers p and g.
9. The program uses a loop to input elements for the array a:
a. The loop prompts the user to enter values for array a.
10. Initializes the pointer p to point to the first element of the array a.
11. The program sorts the array in descending order using the selection sort algorithm:
Task 7:
Pseudo Code:
1. include<iostream>: Includes the input/output stream library for input and output
operations.
11 | P a g e
2. using namespace std;: Allows you to use names from the std namespace without
explicitly specifying the namespace each time.
3. int main(): The main function, which is the entry point for the program.
4. int x;: Declares an integer variable x to store the number of elements for the arrays.
5. cout << "enter the number of elements: \n";: Prompts the user to enter the
number of elements.
6. cin >> x;: Reads the user's input and stores it in the variable x.
7. Declares two integer arrays a and b, each with a size of x.
8. Declares an integer pointer p and initializes it to point to the first element of array a.
9. The program uses a loop to input elements for the array a:
a. The loop prompts the user to enter values for array a.
10. Another loop is used to copy the elements from array a to array b:
12 | P a g e
Task 8:
Pseudo Code:
1. #include<iostream>: Includes the input/output stream library for input and output
operations.
2. #include<new>: Includes the new header, which is used for dynamic memory allocation.
3. using namespace std;: Allows you to use names from the std namespace without
explicitly specifying the namespace each time.
4. int main(): The main function, which is the entry point for the program.
5. int x;: Declares an integer variable x to store the number of elements for the
dynamically allocated array.
6. cout << "enter the number of elements: \n";: Prompts the user to enter the
number of elements.
7. cin >> x;: Reads the user's input and stores it in the variable x.
8. Declares an integer array a with a size of x. This array will be used for dynamic memory
allocation.
9. Declares an integer pointer p.
10. Allocates dynamic memory for an integer array using the new operator, where the size of
the array is determined by the value of x. The memory address of the allocated array is
stored in the pointer p.
11. The program uses a loop to input elements for the dynamically allocated array:
a. The loop prompts the user to enter values for the dynamic array.
12. After initializing the dynamic array, the program prints the elements:
a. A loop iterates through the elements of the dynamic array and prints their values.
13. Finally, the program releases the dynamically allocated memory, which is done implicitly
when the program exits.
13 | P a g e
Task 9:
Pseudo Code:
1. #include<iostream>: Includes the input/output stream library for input and output
operations.
2. #include<new>: Includes the new header, which is used for dynamic memory allocation.
3. using namespace std;: Allows you to use names from the std namespace without
explicitly specifying the namespace each time.
4. int main(): The main function, which is the entry point for the program.
14 | P a g e
5. int x;: Declares an integer variable x to store the number of elements for the
dynamically allocated array.
6. cout << "enter the number of elements: \n";: Prompts the user to enter the
number of elements.
7. cin >> x;: Reads the user's input and stores it in the variable x.
8. Declares an integer array a with a size of x. This array will not be used for dynamic
memory allocation.
9. Declares an integer pointer p.
10. Allocates dynamic memory for an integer array using the new operator, where the size of
the array is determined by the value of x. The memory address of the allocated array is
stored in the pointer p.
11. The program uses a loop to input elements for the dynamically allocated array:
a. The loop prompts the user to enter values for the dynamic array.
12. After initializing the dynamic array, the program prints the elements:
a. A loop iterates through the elements of the dynamic array and prints their values.
13. The program deallocates the dynamically allocated memory using the delete operator
with [] (since it's an array). This frees the memory so it can be used by the system again.
14. The program attempts to print the elements of the array after the memory has been
deallocated. However, this will likely result in undefined behavior, as the memory has
been freed, and accessing it is not allowed.
15 | P a g e
Task 10:
Pseudo Code:
16 | P a g e
11. Allocates dynamic memory for an integer array using the new operator, where
the size of the array is determined by the value of x. The memory address of
the allocated array is stored in the pointer p.
12. The program uses a loop to input elements for the dynamically allocated
array:
a. The loop prompts the user to enter values for the dynamic array.
13. After initializing the dynamic array, the program calculates the sum of the
elements:
a. A loop iterates through the elements of the dynamic array, adds the value
pointed to by p to the current value of sum, and increments the pointer p.
14. The program prints the sum of the elements in the dynamically allocated
array.
15. The program deallocates the dynamically allocated memory using the delete
operator with [] (since it's an array). This frees the memory so it can be used
by the system again.
17 | P a g e
Task 11:
Pseudo Code:
1. #include<iostream>: Includes the input/output stream library for input and output
operations.
2. #include<new>: Includes the new header, which is used for dynamic memory allocation.
3. using namespace std;: Allows you to use names from the std namespace without
explicitly specifying the namespace each time.
4. int main(): The main function, which is the entry point for the program.
5. int x;: Declares an integer variable x to store the number of elements for the
dynamically allocated array.
6. cout << "enter the number of elements: \n";: Prompts the user to enter the
number of elements.
7. cin >> x;: Reads the user's input and stores it in the variable x.
8. Declares an integer array a with a size of x. This array will not be used for dynamic
memory allocation.
9. Declares an integer pointer p.
10. Initializes an integer variable sum to 0, which will be used to store the sum of the
elements in the dynamically allocated array.
11. Initializes a floating-point variable avg to store the calculated average of the elements.
12. Allocates dynamic memory for an integer array using the new operator, where the size of
the array is determined by the value of x. The memory address of the allocated array is
stored in the pointer p.
18 | P a g e
13. The program uses a loop to input elements for the dynamically allocated array:
a. The loop prompts the user to enter values for the dynamic array.
14. After initializing the dynamic array, the program calculates the sum of the elements:
a. A loop iterates through the elements of the dynamic array, adds the value pointed to
by p to the current value of sum, and increments the pointer p.
15. The program calculates the average by dividing the sum by the number of elements ( x).
16. The program prints the calculated average.
Task 12:
Pseudo Code:
19 | P a g e
1. #include<iostream>: Includes the input/output stream library for input and output
operations.
2. #include<new>: Includes the new header, which is used for dynamic memory allocation.
3. using namespace std;: Allows you to use names from the std namespace without
explicitly specifying the namespace each time.
4. int main(): The main function, which is the entry point for the program.
5. int x;: Declares an integer variable x to store the number of elements for the
dynamically allocated character array.
6. cout << "enter the number of elements: \n";: Prompts the user to enter the
number of elements.
7. cin >> x;: Reads the user's input and stores it in the variable x.
8. Declares two character arrays a and b, each with a size of x. a will be used for dynamic
memory allocation, and b will store the reversed elements.
9. Declares a character pointer p.
10. Allocates dynamic memory for a character array using the new operator, where the size
of the array is determined by the value of x. The memory address of the allocated array
is stored in the pointer p.
11. The program uses a loop to input elements for the dynamically allocated character
array:
a. The loop prompts the user to enter values for the dynamic array.
12. After initializing the dynamic array, the program reverses the order of the elements:
a. A loop iterates through the elements of the dynamic array in reverse order, storing
them in the corresponding position of array b.
13. The program prints the reversed character array b.
20 | P a g e
Task 13:
Pseudo Code:
1. #include<iostream>: Includes the input/output stream library for input and output
operations.
2. #include<new>: Includes the new header, which is used for dynamic memory allocation.
3. using namespace std;: Allows you to use names from the std namespace without
explicitly specifying the namespace each time.
4. int main(): The main function, which is the entry point for the program.
5. int x;: Declares an integer variable x to store the number of elements for the arrays.
21 | P a g e
6. cout << "enter the number of elements: \n";: Prompts the user to enter the
number of elements.
7. cin >> x;: Reads the user's input and stores it in the variable x.
8. Declares three integer arrays a, b, and c, each with a size of x.
9. Declares two integer pointers p and q.
10. Allocates dynamic memory for two integer arrays using the new operator, where the size
of the arrays is determined by the value of x. The memory addresses of the allocated
arrays are stored in the pointers p and q.
11. The program uses two separate loops to input elements for arrays a and b using the
pointers p and q:
a. The first loop prompts the user to enter values for array a.
b. The second loop prompts the user to enter values for array b.
12. After initializing arrays a and b, the program uses a loop to calculate the product of
corresponding elements from arrays a and b and stores the results in array c:
a. A loop iterates from 0 to x and multiplies the values pointed to by p and q, storing
the result in the corresponding position in array c.
b. The pointers p and q are incremented to move to the next elements in arrays a and b.
13. Finally, the program prints the resulting array c, which contains the element-wise
products of arrays a and `b.
In summary, this program takes two arrays of numbers, multiplies their corresponding
elements, and prints the resulting array c.
22 | P a g e
Brainstorming Tasks:
QUESTION 1:
Pseudo Code:
1. 1– #include<iostream>: Includes the input/output stream library for input and output operations.
2. #include<new>: Includes the new header, which is used for dynamic memory allocation.
3. #include<string.h>: Includes the C library for string operations.
4. using namespace std;: Allows you to use names from the std namespace without explicitly
specifying the namespace each time.
5. int main(): The main function, which is the entry point for the program.
6. int x;: Declares an integer variable x to store the number of characters in the sentence.
7. string a;: Declares a string variable a to store the sentence entered by the user.
8. int y = 1;: Initializes an integer variable y to 1, which will be used to count words.
9. cout << "enter your sentence: \n";: Prompts the user to enter a sentence.
10. getline(cin, a);: Reads the user's input, including spaces, and stores it in the string a.
11. cout << "enter the number of characters your sentence comprises of: \n"; : Prompts the
user to enter the number of characters in the sentence.
12. cin >> x;: Reads the user's input and stores it in the integer variable x.
13. Declares a character pointer p and allocates dynamic memory for a character array using the new
operator, where the size of the array is determined by the value of x. The memory address of the
allocated array is stored in the pointer p.
14. The program uses a loop to copy characters from the string a to the dynamic character array p:
a. The loop iterates through the characters of the string and copies them to the corresponding
positions in the dynamic array.
15. The program uses another loop to count the number of words in the sentence based on spaces:
23 | P a g e
a. The loop iterates through the characters in the dynamic array and checks if each character is a
space (' '). If a space is found, it increments the word count y.
16. Finally, the program prints the number of words in the sentence.
In summary, this program takes a sentence as input, counts the number of words based on spaces,
and prints the word count.
QUESTION 2:
Pseudo Code:
24 | P a g e
1. 2 - #include<iostream>: Includes the input/output stream library for input and output
operations.
2. #include<string.h>: Includes the C library for string operations (although not used in
this program).
3. using namespace std;: Allows you to use names from the std namespace without
explicitly specifying the namespace each time.
4. int main(): The main function, which is the entry point for the program.
5. Declares an integer pointer p and initializes it to 0. This pointer will be used to manage
the dynamic array.
6. Declares an integer variable size and initializes it to 0, which will be used to keep track
of the dynamic array's size.
7. Declares an integer variable a to store the user's input.
8. cout << "enter list of integers: \n";: Prompts the user to enter a list of integers.
9. The program enters an infinite loop (while(true)) to continuously accept user input
until a negative integer is entered.
10. Inside the loop, the program reads an integer from the user's input into the variable a.
11. The program checks if the entered integer a is less than 0. If it is, the loop is terminated
using the break statement.
12. If the entered integer is non-negative, the program allocates dynamic memory for a new
integer array t with a size of size + 1.
13. The program copies the contents of the old dynamic array p into the new dynamic array
t using a loop.
14. The newly entered integer a is added to the end of the new dynamic array t.
15. The old dynamic array p is deleted using the delete operator.
16. The pointer p is updated to point to the new dynamic array t, and the size is
incremented by 1.
17. After exiting the input loop, the program checks if there are any integers in the dynamic
array (if size > 0).
18. If there are integers in the dynamic array, the program prints the values in the dynamic
array.
19. If no numbers were entered (size is still 0), the program displays a message indicating
that no numbers were entered.
20. Finally, the program deallocates the dynamic memory used by the array using
delete[]p.
In summary, this program allows the user to input a list of non-negative integers, stores
them in a dynamic array, and prints the values. It also handles cases where no integers
are entered.
25 | P a g e
Code Templates / Starting
Point:
TASK 1:
#include<iostream>
using namespace std;
int main()
{
int a=6;
26 | P a g e
int *p;
p=&a;
cout<<"address will be as "<<p;
return 0;
}
TASK 2:
#include<iostream>
using namespace std;
int main()
{
int a=2;
int b=4;
int t;
cout<<"value of first variable is "<<a<<"\n";
cout<<"value of second varaible is "<<b<<"\n";
int *p;
int *q;
p=&a;
q=&b;
t=*p;
*p=*q;
*q=t;
cout <<*p<<"\n";
cout <<*q;
return 0;
}
TASK 3:
#include<iostream>
using namespace std;
int main()
{
int x;
27 | P a g e
cin>>x;
int *p;
int a[x];
int max=0;
p=a;
for(int i=0;i<x;i++)
{
cout<<"enter entry of array ";
cin>>a[i];
}
for(int k=0;k<x;k++)
{
if(*p>max)
{
max=*p;}
p++;
}
cout<<"maximun number is from array is "<<max;
return 0;
}
TASK 4:
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"enter number of elements \n";
cin>>x;
int *p;
int a[x];
int sum=0;
28 | P a g e
for(int i=0;i<x;i++)
{
cout<<"enter entry : ";
cin>>a[i];
}
p=a;
for(int k=0;k<x;k++)
{
sum=sum + *p;
p++;
}
cout<<"sum of elements from array is "<<sum;
return 0;
}
TASK 5:
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"enter number of elements : \n";
cin>>x;
int a[x];
int b[x];
int c[x];
int *p;
int *q;
cout<<"for array 1\n";
for(int i=0;i<x;i++)
{
cout<<"enter enrty ";
cin>>a[i];
29 | P a g e
}
cout<<c[k]<<" ";
}
return 0;
}
TASK 6:
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"enter number of elements \n";
cin>>x;
int max=0;
int a[x];
int b[x];
int *p;
int *g;
for(int i=0;i<x;i++)
30 | P a g e
{
cout<<"enter entry "<<endl;
cin>>a[i];
}
p=a;
for(int k=0;k<x;k++)
{
for(int j=0;j<x;j++)
{
if(*p>max)
{
max=*p;
g=p;
}
p++;
}
p=a;
b[x-k-1]=*g;
max=0;
*g=0;
}
cout<<"sorted array is ";
for(int j=0;j<x;j++)
{
cout<<b[j]<<" ";
}
return 0;
}
TASK 7:
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"enter number of elements : \n";
31 | P a g e
cin>>x;
int a[x];
int b[x];
int *p;
p=a;
for(int i=0;i<x;i++)
cin>>a[i];
}
for(int j=0;j<x;j++)
{
b[j]=*p;
p++;
cout<<b[j]<<" ";
}
return 0;
}
TASK 8:
#include<iostream>
#include<new>
using namespace std;
int main()
{
int x;
cout<<"enter number of elements \n";
cin>>x;
int a[x];
int *p;
p=new int [x];
for(int i=0;i<x;i++)
32 | P a g e
{
cout<<p[i]<<" ";
}
return 0;
}
TASK 9:
#include<iostream>
#include<new>
using namespace std;
int main()
{
int x;
cout<<"enter number of elements \n";
cin>>x;
int a[x];
int *p;
p=new int [x];
for(int i=0;i<x;i++)
{
cout<<"enter entry ";
cin>>p[i];
}
for(int i=0;i<x;i++)
{
cout<<p[i]<<" ";
33 | P a g e
}
delete []p;
cout<<"\n after delete \n";
for(int i=0;i<x;i++)
{
cout<<p[i]<<" ";
}
return 0;
}
TASK 10:
#include<iostream>
#include<new>
using namespace std;
int main()
{
int x;
cout<<"enter number of elements \n";
cin>>x;
int a[x];
int *p;
int sum=0;
34 | P a g e
p++;
}
cout<<"sum is "<<sum;
return 0;
}
TASK 11:
#include<iostream>
#include<new>
using namespace std;
int main()
{
int x;
cout<<"enter number of elements \n";
cin>>x;
int a[x];
int *p;
int sum=0;
float avg;
cout<<"average is"<<avg;
return 0;
35 | P a g e
}
TASK 12:
#include<iostream>
#include<new>
using namespace std;
int main()
{
int x;
cout<<"enter number of elements \n";
cin>>x;
char a[x];
char b[x];
char *p;
p=new char [x];
for(int i=0;i<x;i++)
{
cout<<b[k]<<" ";
}
return 0;
}
TASK 13:
36 | P a g e
#include<iostream>
#include<new>
using namespace std;
int main()
{
int x;
cout<<"enter number of elements \n";
cin>>x;
int a[x];
int b[x];
int c[x];
int *p;
int *q;
p=new int [x];
37 | P a g e
cout<<c[k]<<" ";
}
return 0;
}
TASK 14:
#include<iostream>
#include<new>
#include<string.h>
using namespace std;
int main()
{
int x;
string a;
int y=1;
cout<<"enter your sentence \n";
getline(cin,a);
cout<<"enter number of characters your sentense comprises of \n";
cin>>x;
char *p;
p=new char [x];
for(int i=0;i<x;i++)
{
p[i]=a[i];
}
for(int i=0;i<x;i++)
{
if(p[i]==' ')
{
y++;
}
}
cout<<"your sentence has words "<<y;
return 0;
}
38 | P a g e
TASK 15:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int *p=0;
int size=0;
int a;
cout<<"enter list of integers \n";
while(true)
{
cin>>a;
if(a<0)
{
break;
}
int *t;
t=new int [size + 1];
for(int i=0;i<size;i++)
{
t[i]=p[i];
}
t[size]=a;
delete []p;
p=t;
size++;
}
if(size>0)
{
cout<<"integers in dynamic array will be ";
for(int i=0;i<size;i++)
{
cout<<p[i]<<" ";
39 | P a g e
}
cout<<"\n";
}
else
{
cout<<"no number entered \n";
}
delete []p;
return 0;
}
Constraints:
Be careful to point at the correct variables a mixing up pointers
can cause the code to malfunction.
Take caution while using brackets for arrays as different brackets
serve different purposes.
Required Material:
We used dev c++ or visual studio code as a basic compiler for running
our basic codes in C ++ language .
We used the following libraries;
1. Include <iostream>
2. Include <string.h>
3. Include <conio.h>
4. Include<math.h>
40 | P a g e
Conclusion:
In this lab we learned how to use pointers to make our job easy while
using static and dimensional arrays that have different input for pointers
but almost perform the same purpose. We practiced our array skills now
with pointers this time.
41 | P a g e