# Programming Exercise Looping Structure P-4

Programming Exercise Looping Structure P-3

IF Statement Programming Exercise Part-1

#### Program 21: Write a program that inputs data into a table and finds out the maximum and minimum values entered in the table. It also displays the maximum value and minimum value and their locations in the table. // Searching Maximum and Minimum values from table

```#include<iostream.h>
#include<conio.h>
main()
{
int table[3][4];
int r, c, max, min, mx_r, mx_c, mn_r, mn_c;
clrscr();
r=0;
while(r<=2)
{
for(c = 0; c<=3; c++)
{
cout<<"Enter value for row "<<r<<" and column "<<c<<" ? ";
cin>>table[r][c];
}
r++;
}

// finding maximum & minimum values

max = min = table[0][0];
for(r = 0; r<=2; r++)
for(c = 0; c<=3; c++)
{
if(max<table[r][c])
{
max = table[r][c];
mx_r = r;
mx_c = c;
}
if(min>table[r][c])
{
min = table[r][c];
mn_r = r;
mn_c = c;
}
}
cout<<"\nMaximum value "<<max<<endl;
cout<<"Location["<<mx_r<<"]["<<mx_c<<"]\n";
cout<<"\nMinimum value "<<min<<endl;
cout<<"Location["<<mn_r<<"]["<<mn_c<<"]\n";
getch();
}```

#### Program 22: Write a program that initializes values in a table and searches a value from it. If value found, then it displays the value and its location otherwise it displays message “Value not found”. It also displays the values of the table in tabular form.

```#include<iostream.h>
#include<conio.h>
main()
{
int table[3][4] = {{22,33,2,8},{5,87,15,33},{23,65,33,23}};
int r, c, lr, lc, val, check = 0;
clrscr();
cout<<"Enter a value to search ? ";
cin>>val;

// searching a value from table

for(r = 0; r<=2; r++)
for(c = 0; c<=3; c++)
{
if(val == table[r][c])
{
lr = r;
lc = c;
check = 1;
}
}

// displaying values of table in tabular form

for(r = 0; r<=2; r++)
{
for(c = 0; c<=3; c++)
cout<<table[r][c]<<"\t";
cout<<endl;
}
if(check == 0)
else
cout<<"\nValue found at location ["<<lr<<"]["<<lc<<"]";
getch();
}```

#### Program 23: Write a program that initializes values in a one-dimensional array. It passes the array as argument to a function which computes the sum of the array. The function should return the calculated sum to the calling function. // sum of array using function

```#include<iostream.h>
#include<conio.h>
main()
{
int arr[5] = {4,6,3,4,7}, i;
int sum(int []);
clrscr();
cout<<"Values of array\n";
for(i = 0; i<=4; i++)
cout<<arr[i]<<endl;
cout<<"\nSum of array ="<<sum(arr);
getch();
}
// definition of sum() function

int sum(int x[])
{
int s = 0;
for(int i = 0; i<=4; i++)
s = s + x[i];
return s;
}```

#### Program 24: Write a program that initializes values in a table and also declares a linear array. It passes the table and linear array as arguments to a function. The function picks odd values of the table and stores them into the linear array. The function also returns number of odd values. It displays the values of the linear array in calling function.

```#include<iostream.h>
#include<conio.h>
main()
{
int r, c, size, odd[15] = {0};
int arr[3][5] = {{14,61,13,14,71},
{34,55,33,22,11},
{12,56,44,2,98}};
int pick(int [3][5], int[15]);
clrscr();
size = pick(arr,odd);
cout<<"\nValues of table\n";
for(r = 0; r<=2; r++)
{
for(c = 0; c<=4; c++)
cout<<arr[r][c]<<"\t";
cout<<endl;
}
cout<<"\nValues of array odd\n";
for(r = 0; r<= size - 1; r++)
cout<<odd[r]<<endl;
cout<<"Total odd values are = "<<size;
getch();
}

// definition of pick() function

int pick(int tab[3][5], int x[15])
{
int c = 0;
for(int i = 0; i<=2; i++)
for( int j = 0; j<=4; j++)
if (tab[i][j]%2 == 1)
{
x[c] = tab[i][j];
c++;
}
return c;
}```

#### Program 25 Write a program that initializes values in two tables A and B. It adds tables A and B and stores result into table C. It also displays the values of three tables in tabular form on the screen. //Note: The addition of matrices A and B is obtained by adding the elements of A to the corresponding elements of B. To add/subtract two matrices, the number of rows and columns of two matrices must be equal. For example: // A = ad be cf B = gj hk il // // A + B = (a + g)(d + j) (b + h)(e + k) (c + i)(f + l) // // Addition of matrices

```#include<iostream.h>
#include<conio.h>
main()
{
int A[3][3] = {{2,3,8},{5,8,3},{2,6,3}};
int B[3][3] = {{2,3,1},{6,1,2},{6,2,1}};
int C[3][3];
clrscr();
void print(char [15], int [3][3]);
print("Matrix A: ", A);
print("Matrix B: ", B);
print("addition of A & B ", C);
getch();
}

void addition(int X[3][3], int Y[3][3], int Z[3][3])
{
int r, c;
for(r = 0; r<=2; r++)
for(c = 0; c<=2; c++)
Z[r][c] = X[r][c]+Y[r][c];
}

// definition of print() function
void print( char str[], int R[3][3])
{
int r, c;
cout<<endl<<str<<endl;
for(r = 0; r<=2; r++)
{
for(c = 0; c<=2; c++)
cout<<R[r][c]<<"\t";
cout<<endl;
}
}```

#### // Multiplication of matrices

```#include<iostream.h>
#include<conio.h>
void multiply(int [3][3], int[3][3], int [3][3]);
void print(char [25], int [3][3]);
main()
{
int A[3][3] = {{2,3,8},{4,5,3},{2,6,3}};
int B[3][3] = {{3,9,1},{1,2,4},{6,2,1}};
int C[3][3];
clrscr();
multiply(A, B, C);
print("Matrix A: ",A);
print("Matrix B: ",B);
print("Multiplication of A & B: ",C);
getch();
}

// function for multiplying data of tables

void multiply(int X[3][3], int Y[3][3], int Z[3][3])
{
int r, c, k, sum;
for(r = 0; r<=2; r++)
{
for(c = 0; c<=2; c++)
{
for(k = 0, sum = 0; k<=2; k++)
sum = sum + X[r][k] * Y[k][c];
Z[r][c] = sum;
}
}
}

// function for displaying data of tables in tabular form

void print(char str[], int R[3][3])
{
int r, c;
cout<<endl<<str<<endl;
for(r = 0; r<=2; r++)
{
for(c = 0; c<=2; c++)
cout<<R[r][c]<<"\t";
cout<<endl;
}
}```

#### // Transpose of a Matrix

```#include<iostream.h>
#include<conio.h>
void transpose(int [2][4], int [4][2]);
void print(char [25], int [4][2]);
main()
{
int A[2][4]={{24,31,18,51},{4,15,13,2}};
int T[4][2];
char str[25];
clrscr();
cout<<endl<<"Matrix A:"<<endl;
for(int r = 0; r<=1; r++)
{
for(int c = 0; c<=3; c++)
cout<<A[r][c]<<"\t";
cout<<endl;
}

transpose(A, T);
print("Transpose of A: ",T);
getch();
}

// definition of transpose() function

void transpose(int X[2][4], int Y[4][2])
{
int r, c;
for(r = 0; r<=1; r++)
for(c = 0; c<=3; c++)
Y[c][r] = X[r][c];
}

void print(char str[], int R[4][2])
{
int r, c;
cout<<endl<<str<<endl;
for(r = 0; r<=3; r++)
{
for(c = 0; c<=1; c++)
cout<<R[r][c]<<"\t";
cout<<endl;
}
}```