#include <iostream>
using namespace std;
int main() {
int arr[4], i;
cout << "Enter four elements of an array: ";
for(i = 0; i < 4; i++) {
cin >> arr[i];
}
cout << "The elements of array are: ";
for(i = 0; i < 4; i++) {
cout << arr[i] << " ";
}
}
Q2. Program to find the largest and smallest number in the array.
#include <iostream>
using namespace std;
int main() {
int smallest, largest, i, temp = 0, arr[10] = {2, 5, 6, 7, 1, -3, 15, 0, -5, -9};
smallest = largest = arr[0];
for(i = 0; i < 10; i++) {
if(arr[i] > largest)
largest = arr[i];
if(arr[i] < smallest)
smallest = arr[i];
}
cout << "largest number: " << largest << "\n" << "smallest number: " << smallest;
return 0;
}
OUTPUT:
largest number: 15
smallest number: -9
Q4. Program to reverse the elements of an array.
#include <iostream>
using namespace std;
int main() {
int start, end, temp = 0, i, j , arr[5] = {2, 5, 3, 1, 9};
start = arr[0];
end = arr[4];
for( i = 0, j = 4; i < j; i++, j-- ) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
start = arr[i];
end = arr[j];
}
for(i = 0; i < 5; i++)
cout << arr[i] << " ";
}
OUTPUT:
9 1 3 5 2
Q5. Program to pass array elements to a function.
#include <iostream>
using namespace std;
void check(int num);
int main() {
int arr[5];
for(int i = 0; i < 5; i++) {
cin >> arr[i];
check(arr[i]);
}
}
void check(int num) {
if(num%2 == 0)
cout << num << " is even number" << endl;
else
cout << num << " is odd number" << endl;
}
INPUT:
5 7 0 4 6
OUTPUT:
5 is odd number
7 is odd number
0 is even number
4 is even number
6 is even number
Q6. Program to pass an array to a function.
#include <iostream>
using namespace std;
void func(int val[]);
int main() {
int arr[5] = {1,2,3,4,5};
func(arr);
cout << "Contents of array are now : ";
for( int i = 0; i < 5; i++)
cout << arr[i] << " ";
}
void func(int val[]) {
int sum = 0;
for(int i = 0; i < 5; i++) {
val[i] = val[i] * val[i];
sum += val[i];
}
cout << "The sum of squares = " << sum << endl;;
}
OUTPUT:
The sum of squares = 55
Contents of array are now : 1 4 9 16 25
Q7. Program to input and display matrix.
#include <iostream>
#define ROW 3
#define COL 4
using namespace std;
int main() {
int mat[ROW][COL];
cout << "Enter the element of the matrix [3][4]: \n";
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) {
cin >> mat[i][j];
}
}
cout << "The matrix that you have entered is: \n";
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) {
cout << mat[i][j] << " ";
}
cout << "\n";
}
}
INPUT:
Enter the element of the matrix [3][4]:
1 2 3 4
5 6 7 8
9 0 1 2
OUTPUT:
The matrix that you have entered is:
1 2 3 4
5 6 7 8
9 0 1 2
Q8. Program for the addition of two matrices.
#include <iostream>
#define ROW 3
#define COL 4
using namespace std;
int main() {
int mat1[ROW][COL], mat2[ROW][COL], mat3[ROW][COL], i, j;
cout << "Enter the element of the matrix [3][4]: \n";
for(i = 0; i < ROW; i++) {
for(j = 0; j < COL; j++) {
cin >> mat1[i][j];
}
}
cout << "Enter the element of 2nd matrix [3][4]: \n";
for(i = 0; i < ROW; i++) {
for(j = 0; j < COL; j++) {
cin >> mat2[i][j];
}
}
/* Addition */
for(i = 0; i < ROW; i++) {
for(j = 0; j < COL; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
cout << "The matrix that you have entered is: \n";
for(i = 0; i < ROW; i++) {
for(j = 0; j < COL; j++) {
cout << mat3[i][j] << " ";
}
cout << endl;
}
}
INPUT:
Enter the element of the matrix [3][4]:
1 2 3 4
5 6 7 8
9 0 1 2
Enter the element of 2nd matrix [3][4]:
2 1 0 9
8 7 6 5
4 3 2 1
OUTPUT:
The matrix that you have entered is:
3 3 3 13
13 13 13 13
13 3 3 3
Q9. Program for multiplication of two matrices.
#include <iostream>
#define ROW1 3
#define COL1 4
#define ROW2 COL1
#define COL2 2
using namespace std;
int main() {
int mat1[ROW1][COL1], mat2[ROW2][COL2], mat3[ROW1][COL2], i, j;
cout << "Enter the element of the matrix [3][4]: \n";
for(i = 0; i < ROW1; i++) {
for(j = 0; j < COL1; j++) {
cin >> mat1[i][j];
}
}
cout << "Enter the element of 2nd matrix [4][2]: \n";
for(i = 0; i < ROW2; i++) {
for(j = 0; j < COL2; j++) {
cin >> mat2[i][j];
}
}
/* Addition */
for(i = 0; i < ROW1; i++) {
for(j = 0; j < COL2; j++) {
mat3[i][j] = mat1[i][j] * mat2[i][j];
}
}
cout << "The matrix that you have entered is: \n";
for(i = 0; i < ROW1; i++) {
for(j = 0; j < COL2; j++) {
cout << mat3[i][j] << " ";
}
cout << endl;
}
}
INPUT:
Enter the element of the matrix [3][4]:
1 2 3 4
5 6 7 8
8 9 1 2
Enter the element of 2nd matrix [4][2]:
1 2
3 4
5 6
7 8
OUTPUT:
The matrix that you have entered is:
1 4
15 24
40 54
-:POINTERS:- Q1. Program to dereference pointer variables.
-:STRUCTRE:-
Q1. Program to display the values of structure members.
#include <iostream>
using namespace std;
int main() {
int a = 87;
float b = 4.5;
int *p1 = &a;
float *p2 = &b;
printf("Value of p1 = Address of a = %p\n", p1);
printf("Value of p2 = Address of b = %p\n", p2);
printf("Address of p1 = %p\n", &p1);
printf("Address of p2 = %p\n", &p2);
printf("Value of a = %d %d %d\n", a, *p1, *(&a));
printf("Value of b = %f %f %f\n", b, *p2, *(&b));
}
OUTPUT:
Value of p1 = Address of a = 0x7ffe34a4bb98
Value of p2 = Address of b = 0x7ffe34a4bb9c
Address of p1 = 0x7ffe34a4bba0
Address of p2 = 0x7ffe34a4bba8
Value of a = 87 87 87
Value of b = 4.50 4.50 4.50
Q2. Program to understand pointer to pointer.
#include <iostream>
using namespace std;
int main() {
int a = 5;
int *pa;
int **ppa;
pa = &a;
ppa = &pa;
printf("Address of a = %p\n", &a);
printf("Value of pa = Address of a = %p\n", pa);
printf("Value of *pa = Value of a = %d\n", *pa);
printf("Address of pa = %p\n", &pa);
printf("Value of ppa = Address of pa = %p\n", ppa);
printf("Value of *ppa = Value of pa = %p\n", *ppa);
printf("Value of **ppa = Value of a = %d\n", **ppa);
printf("Address of ppa = %p\n", &ppa);
}
OUTPUT:
Address of a = 0x7ffdc128220c
Value of pa = Address of a = 0x7ffdc128220c
Value of *pa = Value of a = 5
Address of pa = 0x7ffdc1282210
Value of ppa = Address of pa = 0x7ffdc1282210
Value of *ppa = Value of pa = 0x7ffdc128220c
Value of **ppa = Value of a = 5
Address of ppa = 0x7ffdc1282218
Q3. Program to print the value and address of the elements of an array.
#include <iostream>
using namespace std;
int main() {
int arr[5] = {5, 10, 15, 20, 25};
int i;
for(i = 0; i < 5; i++) {
printf("Value of arr[%d] = %d\t", i, arr[i]);
printf("Address of arr[%d] = %p\n", i, &arr[i]);
}
}
OUTPUT:
Value of arr[0] = 5 Address of arr[0] = 0x7ffe3748bc60
Value of arr[1] = 10 Address of arr[1] = 0x7ffe3748bc64
Value of arr[2] = 15 Address of arr[2] = 0x7ffe3748bc68
Value of arr[3] = 20 Address of arr[3] = 0x7ffe3748bc6c
Value of arr[4] = 25 Address of arr[4] = 0x7ffe3748bc70
Q4. Program to print the value and address of elements of an array using pointer.
#include <iostream>
using namespace std;
int main() {
int arr[5] = {5, 10, 15, 20, 25};
int i;
for(i = 0; i < 5; i++) {
printf("Value of arr[%d] = %d\t", i, *(arr+i));
printf("Address of arr[%d] = %p\n", i, arr+i);
}
}
OUTPUT:
Value of arr[0] = 5 Address of arr[0] = 0x7ffc148936f0
Value of arr[1] = 10 Address of arr[1] = 0x7ffc148936f4
Value of arr[2] = 15 Address of arr[2] = 0x7ffc148936f8
Value of arr[3] = 20 Address of arr[3] = 0x7ffc148936fc
Value of arr[4] = 25 Address of arr[4] = 0x7ffc14893700
Q5. Program to call by value.
#include <iostream>
using namespace std;
void value(int x, int y);
int main() {
int a = 5, b = 8;
printf("Before calling the function, a = %d and b = %d\n", a, b);
value(a, b);
printf("After calling the function, a = %d and b = %d\n", a, b);
}
void value(int x, int y) {
x++;
y++;
printf("Inside function x = %d, y = %d\n", x, y);
}
OUTPUT:
Before calling the function, a = 5 and b = 8
Inside function x = 6, y = 9
After calling the function, a = 5 and b = 8
Q6. Program to call by reference.
#include <iostream>
using namespace std;
void ref(int *p, int *q);
int main() {
int a = 5, b = 8;
printf("Before calling a function a = %d and b = %d\n", a, b);
ref(&a, &b);
printf("After calling a function a = %d and b = %d\n", a, b);
}
void ref(int *p, int *q) {
(*p)++;
(*q)++;
printf("Inside function *p = %d, *q = %d\n",*p, *q);
}
OUTPUT:
Before calling a function a = 5 and b = 8
Inside function *p = 6, *q = 9
After calling a function a = 6 and b = 9
Q7. Program to show how to return more than one value from a function using call by reference.
#include <iostream>
using namespace std;
void func(int x, int y, int *ps, int *pd, int *pp) {
*ps = x+y;
*pd = x-y;
*pp = x*y;
}
int main() {
int a, b, sum, diff, prod;
a = 6, b = 4;
func(a, b, &sum, &diff, &prod);
printf("Sum = %d, Difference = %d, Product = %d\n", sum, diff, prod);
}
OUTPUT:
Sum = 10, Difference = 2, Product = 24
Q8. Program to show a function that returns a pointer.
#include <iostream>
using namespace std;
int *fun(int *p, int n);
int mian() {
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, n, *ptr;
n = 5;
ptr = fun(arr, n);
printf("arr = %p, ptr = %p, *ptr = %d\n", arr, ptr, *ptr);
}
int *fun(int *p, int n) {
p = p+n;
return p;
}
Q9. Program to show that changes to the array made inside the function affect the original array.
#include <iostream>
using namespace std;
void func(int a[]);
int main() {
int i, arr[5] = {2, 4, 1, 8, 6};
func(arr);
printf("Inside main() : ");
for(i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
}
void func(int a[]) {
int i;
printf("Inside func() : ");
for(i = 0; i < 5; i++)
{
a[i] = a[i] + 2;
printf("%d ", a[i]);
}
printf("\n");
}
OUTPUT:
Inside func() : 4 6 3 10 8
Inside main() : 4 6 3 10 8
Q10. When an array is passed to a function, the receiving argument is declared as a pointer.
#include <iostream>
using namespace std;
void func(float f[], int *i, char c[5]);
int main() {
float f_arr[5] = {1.4, 2.5, 3.7, 4.1, 5.9};
int i_arr[5] = {1, 2, 3, 4, 5};
char c_arr[5] = {'a', 'b', 'c', 'd', 'e'};
printf("Inside main(): ");
printf("Size of f_arr = %u\t", sizeof(f_arr));
printf("Size of i_arr = %u\t", sizeof(i_arr));
printf("Size of c_arr = %u\t", sizeof(c_arr));
func(f_arr, i_arr, c_arr);
}
void func(float f[], int *i, char c[5]) {
printf("Inside func(): ");
printf("Size of f = %u\t", sizeof(f));
printf("Size of i = %u\t", sizeof(i));
printf("Size of c = %u\t", sizeof(c));
}
Q11. Program to an array of pointers.
#include <iostream>
using namespace std;
int main() {
int *pa[3];
int i, a = 5, b = 10, c = 15;
pa[0] = &a;
pa[1] = &b;
pa[2] = &c;
for(i = 0; i < 3; i++) {
printf("pa[%d] = %p\t", i, pa[i]);
printf("*pa[%d] = %d\n", i, *pa[i]);
}
}
OUTPUT:
pa[0] = 0x7ffec0c32910 *pa[0] = 5
pa[1] = 0x7ffec0c32914 *pa[1] = 10
pa[2] = 0x7ffec0c32918 *pa[2] = 15
#include <iostream>
#include <string.h>
using namespace std;
struct student {
char name[20];
int rollno;
float marks;
};
int main() {
struct student stu1 = {"Mark", 02, 98};
struct student stu2, stu3;
strcpy(stu2.name,"Bob");
stu2.rollno = 24;
stu2.marks = 97;
printf("Enter name, rollno and marks for stu3: ");
scanf("%s %d %f", stu3.name, &stu3.rollno, &stu3.marks);
printf("stu1 : %s %d %.2f\n", stu1.name, stu1.rollno, stu1.marks);
printf("stu2 : %s %d %.2f\n", stu2.name, stu2.rollno, stu2.marks);
printf("stu3 : %s %d %.2f\n", stu3.name, stu3.rollno, stu3.marks);
}
INPUT:
Enter name, rollno and marks for stu3: Alice 20 95.5
OUTPUT:
stu1 : Mark 2 98.00
stu2 : Bob 24 97.00
stu3 : Alice 20 95.50
Q2. Program to assign a structure variable to another structure variable.
#include <iostream>
#include <string.h>
using namespace std;
struct student {
char name[20];
int rollno;
float marks;
};
int main() {
struct student stu1 = {"Mark", 02, 98}, stu2;
stu2 = stu1;
printf("stu1 : %s %d %.2f\n", stu1.name, stu1.rollno, stu1.marks);
printf("stu2 : %s %d %.2f\n", stu2.name, stu2.rollno, stu2.marks);
}
OUTPUT:
stu1 : Mark 2 98.00
stu2 : Mark 2 98.00
Q3. Program to understand an array of structures.
Q4. Program to understand arrays within structures.
Q5. Program to understand pointers to structures.
#include <iostream>
#include <string.h>
using namespace std;
struct student {
char name[20];
int rollno;
int marks;
};
int main() {
struct student stu = {"Ahad", 1, 48};
struct student *ptr = &stu;
printf("Name - %s\t", ptr->name);
printf("Rollno - %d\t", ptr->rollno);
printf("Marks - %d\n", ptr->marks);
}
OUTPUT:
Name - Ahad Rollno - 1 Marks - 48
Q6. Program to understand how to structure members are sent to a function.
#include <iostream>
#include <string.h>
using namespace std;
struct student {
char name[20];
int rollno;
int marks;
};
void display(char name[], int rollno, int marks);
int main() {
struct student stu1 = {"Ahad", 1, 48};
struct student stu2;
strcpy(stu2.name, "Ahsan");
stu2.rollno = 18;
stu2.marks = 90;
display(stu1.name, stu1.rollno, stu1.marks);
display(stu2.name, stu2.rollno, stu2.marks);
}
void display(char name[], int rollno, int marks)
{
printf("Name - %s\t", name);
printf("Rollno - %d\t", rollno);
printf("Marks - %d\n", marks);
}
OUTPUT:
Name - Ahad Rollno - 1 Marks - 48
Name - Ahsan Rollno - 18 Marks - 90
Q7. Program to understand how a structure variable is sent to a function.
#include <iostream>
#include <string.h>
using namespace std;
struct student {
char name[20];
int rollno;
int marks;
};
void display(struct student *);
void inc_marks(struct student *);
int main() {
struct student stu1 = {"Waleed", 2, 89};
struct student stu2 = {"Umar", 9, 90};
inc_marks(&stu1);
inc_marks(&stu2);
display(&stu1);
display(&stu2);
}
void inc_marks(struct student *stuptr)
{
(stuptr->marks)++;
}
void display(struct student *stuptr)
{
printf("Name - %s\t", stuptr->name);
printf("RollNo - %d\t", stuptr->rollno);
printf("Marks - %d\n", stuptr->marks);
}
OUTPUT:
Name - Waleed RollNo - 2 Marks - 90
Name - Umar RollNo - 9 Marks - 91
0 Comments