0% found this document useful (0 votes)
16 views7 pages

Deep Discription of Classes and Inheritance

The document contains 6 programming tasks involving arrays and pointers in C++. Task 1 defines a function to swap two integers passed by reference. Task 2 gets a string input and stores it in a dynamically allocated string variable. Task 3 defines a function to print all elements of a passed array. Task 4 dynamically allocates a 2D array, inputs elements, and calculates the sum. Task 5 dynamically allocates a jagged array and inputs/prints elements. Task 6 defines a function to copy an array and returns a pointer to the new copy.

Uploaded by

f228802
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)
16 views7 pages

Deep Discription of Classes and Inheritance

The document contains 6 programming tasks involving arrays and pointers in C++. Task 1 defines a function to swap two integers passed by reference. Task 2 gets a string input and stores it in a dynamically allocated string variable. Task 3 defines a function to print all elements of a passed array. Task 4 dynamically allocates a 2D array, inputs elements, and calculates the sum. Task 5 dynamically allocates a jagged array and inputs/prints elements. Task 6 defines a function to copy an array and returns a pointer to the new copy.

Uploaded by

f228802
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/ 7

ALEENA AKRAM

22F-8801

TASK NO 1
#include<iostream>
using namespace std;
void swapp(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int a=10, b=30;
int *ap, *bp;
ap = &a;
bp = &b;
swapp(&a, &b);
cout << a << endl;
cout << b << endl;
system ("pause");
}

TASK NO 2
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
string *sp;
sp = new string;
cout << "enter string" << endl;
getline (cin,s);
cout << s;
system("pause");
}
TASK 3
#include<iostream>
using namespace std;
void printfunction(int a[5])
{
for (int i = 0; i < 5; i++)
{
cout << a[i]<<endl;
}
}
int main()
{
int a[5];
int *ap;
ap = new int[5];
cout << " enter elements of array" << endl;
for (int i = 0; i<5; i++)
{
cin >> a[i];
}
cout << "______ELEMENTS OF ARRAY__________" << endl;
printfunction(a);//function call
system("pause");
}
TASK NO 4
#include<iostream>
using namespace std;
int main()
{
int row, col, sum = 0;
cout << "enter column" << endl;
cin >> col;
cout << "enter row" << endl;
cin >> row;
int **p;
p = new int*[row];
for (int i = 0; i < row; i++)
{
p[i] = new int[col];
}
cout << "enter elements of array" << endl;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
cin >> **p;
sum = sum + **p;
}
}
cout << "__________sum of all elements of array" << endl;
cout << sum;
system("pause");
}

TASKNO 5

#include<iostream>
using namespace std;
int main()
{
int row, sum = 0;
cout << "enter row" << endl;
cin >> row;
int **p;
p = new int*[row];
for (int i = 0; i < row; i++)
{
p[i] = new int[i+1];
}
cout << "enter elements of array" << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < i+1; j++)
{
cin >> p[i][j];
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < i + 1; j++)
{
cout << p[i][j];
}
cout << endl;
}
system("pause");
}

TASK NO 6
#include<iostream>
using namespace std;
int *copyfunction(int arr[], int size)
{
int *ptr;
ptr = new int[size];
for (int i = 0; i < size; i++)
{
ptr[i] = arr[i];
}
return ptr;
}
int main()
{
int size;
cout << "_enter size of the array________" << endl;
cin >> size;
int *p;
p = new int[size];
cout << "+++++++++++++++++populate the array+++++++++++++++" << endl;
for (int i = 0; i < size; i++)
{
cin >> p[i];
}
cout << "+++++++++++++++++ array before copy+++++++++++++++" << endl;
for (int i = 0; i < size; i++)
{
cout << p[i]<<endl;
}
int *cp;
cp = copyfunction(p, size);
cout << "+++++++++++++++++ array after copy+++++++++++++++" << endl;
for (int i = 0; i < size; i++)
{
cout << endl << cp[i];
}
system("pause");
}

You might also like