Sei sulla pagina 1di 53

Functions

Practical No.68

Task: Write a program that accept a number from the user as an input and calculates the
square of a given number using function.

#include <iostream>

using namespace std;

int square(int);

int main()

int numb=0,result=0;

cout<<"Enter the number =";

cin>>numb;

result=square(numb);

cout<<"The square of " <<numb<<" is "<<result;

return 0;

int square(int number)

return(number*number);

cout << "My name is Muhammad Musab Mumtaz and my registration number is
BSEE01153257" << endl;

return 0;

Practical No. 69
Task: Write a program that by defining a function “even_odd”, to test whether a given
integer is even or odd. Pass an integer value as an argument to a function.

#include <iostream>

using namespace std;

int evenodd(int);

int main()

int num ;

cout<<"Enter the number=";

cin>>num;

evenodd(num);

return 0;

int evenodd(int num)

if(num%2==0)

cout<<"The Number is Even";

else

cout<<"The Number is Odd";

Practical No. 70

Task: Write a program that accepts three numbers from the user as an input and display
minimum of three numbers using function.

#include <iostream>
using namespace std;

int mini(int, int, int);

int main()

int num1,num2,num3;

cout<<"Enter the Numbers to Find the Minimum Number "<<endl;

cout<<"Enter First Number = ";

cin>>num1;

cout<<"Enter Second Number = ";

cin>>num2;

cout<<"Enter Third Number = ";

cin>>num3;

mini(num1,num2,num3);

return 0;

int mini(int num1, int num2, int num3)

if(num1<num2 && num1<num3)

cout<<"First number is minimum ";

else if(num2<num1 && num2<num3)

cout<<"Second number is minimum " ;

else
{

cout<<"Third number is minimum ";

Practical No. 71

Task: Write a program that calculates the area of the Ring using a single function.

#include <iostream>

using namespace std;

int ring(double, double);

int main()

double radius1,radius2;

cout<<"Enter the radius of Outer circle = ";

cin>>radius1;

cout<<"Enter the radius of Inner circle = ";

cin>>radius2;

ring(radius1,radius2);

return 0;

int ring(double radius1, double radius2)

{
double area1;

area1= 3.142*radius1*radius1;

double area2;

area2= 3.142*radius2*radius2;

cout<<"The area of Outer Circle is = "<<area1<<endl;

cout<<"The area of Inner Circle is = "<<area2<<endl;

cout<<"The area of ring is = "<< area1- area2;

Practical No. 72

Task: Write a function integerPower ( base, exponent ) that returns the value of

base exponent

For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is a positive,


nonzero integer and that base is an integer. The function integerPower should use for or
while to control the calculation. Do not use any math library functions.

#include <iostream>

using namespace std;

int raisetopower(double, int);

int main()

double result,x;
int power;

cout<<"Enter the number = ";

cin>>x;

cout<<"Enter the power = ";

cin>>power;

raisetopower(x,power);

return 0;

int raisetopower(double x, int power)

double result = 1.0;

for(int i=0;i<power;i++)

result= result*x;

cout<<"The result of number "<<x<<" with power " <<power<<" is = "<<result;

Practical No. 73

Task: Write an integer number is said to be a perfect number if the sum of its factors,
including 1 (but not the number itself), is equal to the number.

For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a function perfect that


determines whether parameter number is a perfect number. Use this function in a
program that determines and prints all the perfect numbers between 1 and 1000. Print the
factors of each perfect number to confirm that the number is indeed perfect.

#include <iostream>
using namespace std;

int perfect(int);

int main()

int number;

cout<<"The perfect numbers between 1-1000 are :"<<endl;

for(number=1;number<=1000;number++)

perfect(number);

return 0;

int perfect(int number)

int sum = 0;

for(int i =1;i<=number/2;i++)

if(number%i==0)

sum = sum+ i;

if(sum==number)

cout<<" "<<number<<" ";

}
Practical No. 74

Task: A positive integer is entered through the keyboard. Write a function to obtain the
prime factors of this number.

For example: Prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.

#include <iostream>

#include<stdio.h>

using namespace std;

int main()

int number;

int prime(int number);

int primefactor(int number);

printf("Enter the number whose prime factors are to be calculated: ");

scanf("%d", &number);

primefactor(number);

prime(int num)

int i, ifprime;

for (i=2; i<=num-1; i++)


{

if (num%i==0)

ifprime=0;

else

ifprime=1;

return (ifprime);

primefactor(int num)

int factor,ifprime;

for (factor=2; factor<=num;)

prime(factor);

if (ifprime)

if (num%factor==0)

printf("%d ", factor);

num=num/factor;
continue;

else

factor++;

return 0;

}}

Practical No. 75

Task: Write a function that takes an integer value and returns the number with its digits
reversed.

For example, given the number 7631, the function should return 1367.

#include <iostream>

using namespace std;

int reverse(int);

int main()
{

int number;

cout<<"Enter a 4-digit number : ";

cin>>number;

reverse(number);

return 0;

int reverse(int number)

int number1,number2,number3,number4;

number1 = (number%10);

number2 = ((number%100) / 10);

number3 = ((number/100) % 10);

number4 = ((number/100) / 10);

cout<<"The reverse of the number is : ";

cout<<number1<<number2<<number3<<number4;

Practical No. 76

Task: The greatest common divisor (GCD) of two integers is the largest integer that evenly
divides each of the numbers. Write a function gcd that returns the greatest common
divisor of two integers.

#include <iostream>

#include<stdio.h>

using namespace std;


int gcd1(int, int);

int main()

int a, b;

cout << "Enter two integers: ";

cin >> a >> b;

cout << "GCD (Euclidian) is " << gcd1(a, b) << endl;

return 0;

int gcd1(int a, int b)

if(a == b)

return a;

while(b!= 0 || a!= 0)

if( b != 0)

a %= b;

else

return a;
if( a != 0)

b %= a;

else

return b;

Practical No. 77

Task: Write a function to evaluate the series

to five significant digits.

#include <iostream>

#include<stdio.h>

#include<math.h>

using namespace std;

int main()

float sum,x,m,n,g,i,c,h;

printf("\n Enter the base value:");

scanf("%f",&x);

printf("\n Enter the power value:");


scanf("%f",&n);

sum=x;

c=1;

i=3;

m=2;

ab:

m=m*i;

h=pow((-1),c);

g=pow(x,i);

sum=sum+(h*g)/m;

i=i+2;

c=c+1;

m=m*(i-1);

if(i<=n)

goto ab;

printf("\n Sum=%f",sum);

Practical No. 78

Task: Write a program using function to calculate the factorial value of any integer
entered by the user as an input.

(1) Without using recursion.


#include <iostream>

using namespace std;


int fact(int);
int main()
{
int number;
cout<<"Enter the no : ";
cin>>number;
fact(number);
return 0;
}
int fact(int number)
{
int fact,i;
fact=1;
for(i=1;i<=number;i++)
{
fact=fact*i;
}
cout<<"Factorial is : "<<fact;
}

(2) Using recursion.

#include <iostream>

using namespace std;


int factorial(int);
int main()
{
int number;
cout<<"Enter the no : ";
cin>>number;
cout<<"the Factorial is "<<factorial(number);
return 0;
}
int factorial(int n)
{
int fact;
if(n==0)
{
fact=1;
}
else
fact= n * factorial(n-1);
return fact;

Practical No. 79

Task: A 5-digit positive integer is entered through the keyboard, write a function to
calculate sum of digits of the 5-digit number:

(1) Without using recursion.

#include <iostream>

using namespace std;


int sum(int);
int main()
{
int number;
cout<<"Enter the five digit number :";
cin>>number;
sum(number);
return 0;
}
int sum(int number)
{
int num1,num2,num3,num4,num5;
num1=(number/10000);
num2=((number%10000) / 1000);
num3=((number%1000) / 100);
num4=((number%100) / 10);
num5=((number%100) %10);
cout<<"The Sum of digits is : ";
cout<<" "<<num1+num2+num3+num4+num5;
}

Practical No. 80
Task: Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In
a Fibonacci sequence the sum of two successive terms gives the third term. Following are
the first few terms of the Fibonacci sequence:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...

#include <iostream>

using namespace std;

int main()

int next , last , s;

for(next=0, last=1;last<=100;)

cout<<last<<"\t";

s = next+last;

next=last;

last=s;

return 0;

Practical No. 81

Task: Write a C++ program that uses an inline function circleArea to prompt the user for
the radius of a circle and to calculate and print the area of that circle.

Practical No. 82

Task: Write a C++ program that uses an inline function pound_kg to prompt the user for
the weight in pound and to calculate and print the equivalent in kilogram.
Practical No. 83

Task: Write a program by defining a function swap to exchange the values by passing
argument by reference to the function.

#include <iostream>

using namespace std;

int sum(int, int);

int main()

int num1,num2;

cout<<"Entet the value of number 1 : ";

cin>>num1;

cout<<"Enter the value of number 2 : ";

cin>>num2;

swap(num1,num2);

cout<<"The new value of number 1 : "<<num1<<endl;

cout<<"The new value of number 2 : "<<num2;

return 0;

int swap(int &num1,int &num2)

int temp=0;

temp=num1;

num1=num2;

num2=temp;

return 0;

}
Practical No. 84

Task: Computers are playing an increasing role in education. Write a program that will
help an elementary school student learn multiplication. Use rand to produce two positive
one-digit integers. It should then type a question such as:

How much is 6 times 7?

The student then types the answer. Your program checks the student's answer. If it is
correct, print "Very good!", and then ask another multiplication question. If the answer is
wrong, print "No. Please try again." and then let the student try the same question again
repeatedly until the student finally gets it right.

Practical No. 85

Task: Write a program that plays the game of “guess the number” as follows: Your
program chooses the number to be guessed by selecting an integer at random in the range
1 to 1000. The program then types:

I have a number between 1 and 1000.

Can you guess my number?

Please type your first guess.

The player then types a first guess. The program responds with one of the following:

1. Excellent! You guessed the number!

Would you like to play again (y or n)?

2. Too low. Try again.

3. Too high. Try again.

If the player's guess is incorrect, your program should loop until the player finally gets the
number right. Your program should keep telling the player Too high or Too low to help
the player “zero in” on the correct answer.

#include <iostream>
using namespace std;

int main()

for(int j = 1; j>0; j++)

int z , b ;

int arr[1000];

for(int i = 1;i<=1000 ; i++)

arr[i] = rand();

cout<<"I have a guess number game "<<endl;

cout<<"Can you guess the number "<<endl;

cout<<"Please! Enter the number: ";

cin>>z;

for(i = 1; i<=1000; i++)

if(z==arr[i]) b = 1;

if(b==1)

cout<<"Congradulations! You have guessed the correct number"<<endl;

else

cout<<"You are wrong man!!!!!!!!!!!!!"<<endl;


}

char ch;cout<<"Press Y for playing and N to exit ";

cin>>ch;

if(ch=='y' || ch=='Y')

j=1;

if(ch=='n' || ch=='N')

break;

cout<<"The Game has ended ";

return 0;

Practical No. 86

Task: Write a complete C++ program with the two alternate functions specified below, of
which each simply triples the variable count defined in main. Then compare and contrast
the two approaches. These two functions are

a) Function tripleCallByValue that passes a copy of count call-by-value, triples the copy
and returns the new value.

b) Function tripleByReference that passes count with true call-by-reference via a


reference parameter and triples the original copy of count through its alias (i.e., the
reference parameter).
#include <iostream>

using namespace std;

int TripleCallByValue(int );

int main()

int count;

cout<<"Enter the value of number :";

cin>>count;

cout<<" \n\nThe value of number before calling :"<<count;

cout<<"\n\nThe value of number after calling in main function :"<<count;

return 0;

int TripleCallByValue(int& count)

count = count * count * count ;

cout<<"\n\nThe value of number in function dafination "<<count;

return 0;

Practical No. 87

Task: Write a C++ program that demonstrates the concept of function overloading by
calculating the cube of any number. The function cube calculate cube of different data
type having same or different parameter.

Practical No. 88

Task: Write a C++ program that demonstrates the concept of default argument in
calculating the volume of the box. This function calculate volume of the box 4 time having
default values at first and changing them until having no default vales in last call.

Arrays
Array: An Array is a set of contigious memory locations i.e. all the elements of an array are stored
contigiously / continuously in memory. All the elements of array have same name as that of array
but have different indexes or subscripts. So every element of array is referenced by
1. Array Name
2. Corresponding index or Subscript
That’s why array is also known as "subscripted variable". Moreover the length of array is always
fixed (defined at the time of declaration) and all the elements of an array have same data type e.g.
int, float, long etc...
Note that the index of an array starts from 0 (instead of 1) and ends at N-1 where N is the size of
array. i.e. an array of 5 variables will have its indexes from 0 to 4 as a[0],a[1],a[2],a[3] and a[4].

Practical No.88

Task: Write a program that uses an array of five elements. It just accepts the elements of
array from the user and displays them in the same order using for loop.

#include <iostream>

using namespace std;

int main()

int arr[5],i;

for(i=0;i<5;i++)

{
cout<<"Enter value at position "<<i+1<<" : ";

cin>>arr[i];

cout<<"\tReverse values"<<endl;

for(i=0;i<=4;i++)

cout<<"Value at position "<<i+1<<" : ";

cout<<arr[i]<<endl;

return 0;

Practical No.89

Task: Write a program that uses an array of five elements. It just accepts the elements of
array from the user and displays them in the reverse order using for loop.

#include <iostream>

using namespace std;

int main()

{
int arr[5],i;

for(i=0;i<5;i++)

cout<<"Enter value at position "<<i+1<<" : ";

cin>>arr[i];

cout<<"\tReverse values"<<endl;

for(i=4;i>=0;i--)

cout<<"Value at position "<<i+1<<" : ";

cout<<arr[i]<<endl;

return 0;

Practical No.90

Task: Write a program that uses an array of 8 elements entered by user and then find out
total number of odd and even values entered by the user in a one dimensional array.

#include <iostream>

using namespace std;


int main()

int arr[8],num, num1=0, num2=0;

for(num=0; num<8; num++)

cout<<"Enter the numbers "<<num+1<<" : ";

cin>>arr[num];

for(num=0; num<8; num++)

if(arr[num]%2==0)

num1= num1+1;

else

num2= num2+1;

cout<<"Even numbers in array : "<<num1<<endl;

cout<<"Odd numbers in array : "<<num2<<endl;

return 0;

}
Practical No.91

Task: Write a program to enter the data in two linear arrays, add the two arrays and store
the sum in third array.

#include <iostream>

using namespace std;

int main()

int A[7],B[7],C[7],i;

cout<<"Enter value in 1st Array : "<<endl;

for(i=0;i<7;i++)

cin>>A[i];

cout<<"Enter value in 2nd Array : "<<endl;

for(i=0;i<7;i++)

cin>>B[i];

for(i=0;i<7;i++)

C[i]=A[i]+B[i];

cout<<"Sum of Arrays : "<<endl;

for(i=0;i<7;i++)
{

cout<<C[i]<<"\n";

return 0;

Practical No.92

Task: Write a program that calculates the sum of square of numbers stored in an array of
10 elements.

#include <iostream>

using namespace std;

int main()

int A[10],i;

cout<<"Enter values in Arrays: \n";

for(i=0;i<10;i++)
{

cin>>A[i];

for(i=0;i<10;i++)

A[i]=A[i]*A[i];

cout<<"Square of values: \n";

for(i=0;i<10;i++)

{cout<<A[i]<<"\n";

return 0;

Practical No. 93

Task: Write a program that accepts five elements from a user in an array and find their
maximum and minimum.
Logic: Main Logic of this program is
1. We assume that first element (0) of array is maximum or minimum.
2. We compare the assumed max / min with the remaining (1... N-1) elements of the arrays and if a
number greater than max or less than min is found ,then that is put in the max or min variable,
overwriting the previous value.

#include <iostream>

using namespace std;


int a(int);
int main()
{
const int Numb = 10;
int i;
int a[Numb];
int max = a[0];
int min = a[0];

cout << "Enter 10 values" << endl;


cin >> a[i];

for (i = 0; i < 10; i++)


{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}
}
cout << max << endl;
cout << min << endl;

cout << "My name is Muhammad Musab Mumtaz and my registration number is BSEE01153257" << endl;
return 0;
}

Practical No. 94

Task: Write a program that should accepts five elements from the user in an array and
search a particular element (entered by the user) in it. If the element is found in an array
its index and contents (value) is displayed, else (not found) a massage “Element not found
in an array” is displayed. (Using Linear /Sequential Search Algorithm)

#include <iostream>

using namespace std;


int main()

int A[5],i,p=0,num;

cout<<"Enter values in Arrays: \n";

for(i=0;i<5;i++)

cin>>A[i];

cout<<"\nEnter the number to be searched "<<endl;

cin>>num;

cout<<endl;

for(i=0;i<5;i++)

if(num==A[i])

p=i+1;

if(p==0)

cout<<"Number not found "<<endl;

else

cout<<"Number found at position "<<endl<<p<<endl;

return 0;

}
Practical No. 95

Task: Write a program that should accepts five elements from the user in an array and
search a particular element (entered by the user) in it. If the element is found in an array
its all (apparent) and total number of occurrences are displayed, else (not found) a
massage “Element not found in an array” is displayed.

#include <iostream>

using namespace std;

int main()

int a[5],i,num,s,x=0;

cout<<"Enter the Array length : "<<endl;

for(i=0;i<5;i++)

cin>>a[i];

cout<<"Enter Number to be Searched "<<endl;

cin>>num;

cout<<endl;

for(i=0;i<5;i++)
{

if(num==a[i])

x=x+1;

cout<<"Total Number of Occurrences "<<endl<<x<<endl;

return 0;

Practical No. 96

Task: Write a program that should accepts 10 elements from the user in an array and
search a particular element (entered by the user) in it. If the element is found in an array
its index is displayed, else (not found) a massage “Element not found in an array” is
displayed. (Using Binary Search Algorithm)

#include <iostream>

using namespace std;

int main()

{
int a[10];

int i,loc,num,mid,beg=0,end=9;

cout<<"Enter value in Array \n";

for(i=0;i<10;i++)

cin>>a[i];

cout<<"\nEnter number to be searched \n";

cin>>num;

while(beg<=end)

mid=(beg+end)/2;

if(num==a[mid])

loc=mid;

break;

else if (num<a[mid])end=mid-1;

else if(num>a[mid])beg=mid+1;

if(loc==0)cout<<"\nThe Value searched not present in array ";

else

cout<<"\nValue found at\n"<<loc+1<<endl;

return 0;

}
Practical No. 97

Task: Write a program that accepts ten elements from a user in an array and sorts them in
ascending order using:

1. Bubble sort

#include <iostream>

using namespace std;

int main()

int a[10],i,j,t=0;

for(i=0;i<10;i++)

cout<<"Enter Array Element at "<<i<<"= ";

cin>>a[i];

for(i=0;i<9;i++)

for(j=0;j<9-i;j++)

{
if(a[j]>a[j+1])

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

cout<<"Sorted array "<<endl;

for(i=0;i<10;i++)

cout<<a[i]<<"\t";

return 0;

Practical No. 98

Task: Write a program that accepts ten elements from a user in an array and sorts them in
Descending order.

#include <iostream>
using namespace std;

int main()

int a[10],i,j,t=0;

for(i=0;i<10;i++)

cout<<"Enter Array Element at "<<i<<"= ";

cin>>a[i];

for(i=0;i<9;i++)

for(j=0;j<9-i;j++)

if(a[j]<a[j+1])

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

cout<<"Sorted Array";

cout<<endl;

for(i=0;i<10;i++)

{
cout<<a[i]<<"\t";

return 0;

Practical No. 99

Task: Write a program that accepts ten elements from a user in an array and sorts first five
elements in ascending order and rest of the five elements in descending order using
bubble sort.
#include <iostream>

using namespace std;

int main()
{
int a[10],i,j,t=0;
for(i=0;i<10;i++)
{
cout<<"Enter Array Element at "<<i<<"= ";
cin>>a[i];
}
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=5;i<9;i++)
{
for(j=5;j<9-i;j++)
{
if(a[j]<a[j+1])
{
t=a[j];a[j]=a[j+1];
a[j+1]=t;
}
}
}
cout<<"Sorted Array";
cout<<endl;
for(i=10; i>=0; i--)
{
cout<<a[i]<<"\t";
}
return 0;
}

Practical No. 100

Task: Write a program that accepts two arrays (A and B) of ten elements each from the
user and merges them in a single array (c) of twenty elements.

#include <iostream>

using namespace std;

int main()

int a[10],b[10],c[20],i,j,m,k,l;

cout<<"Enter Elements of 1st Array \n";


for(i=0;i<10;i++)

cin>>a[i];

cout<<"Enter Elements of 2nd Array \n";

for(j=0;j<10;j++)

cin>>b[j];

cout<<"Emerging Array"<<endl;

for(k=0;k<10;k++)

c[k]=a[k];

for(m=0;m<10;m++)

c[m+10]=b[m];

for(l=0;l<20;l++)

cout<<c[l]<<"\t";

return 0;

}
Practical No. 101

Task: Write a program to insert a new value at specified location in a 1D array


(Using Insertion Algorithm).

#include <iostream>

using namespace std;

int main()
{
int array[11] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
int num = 0;
int data,i;
cout<<"Values of Array Before Inserting : ";
for(i=0;i < 11 ;i++)
cout<<array[i]<<",";
cout<<endl;
do
{
cout << "Where do you want to insert an element from 0-10 "<<endl;
cin >> num;
}
while (num < 0 || num > 10);
cout << "Enter data to insert : "<<endl;
cin >> data;
for(i = 9; i > num - 1; i--)
array[i + 1] = array[i];
array[num] = data;
cout << "Final Array: "<<endl;
for(i = 0; i < 11; i++)
cout << array[i] <<endl;
return 0;
}
Practical No. 102

Task: Write a program to delete a value from a specified location in a 1D array


(Using Deletion Algorithm).
Practical No. 103

Task: Write a program that takes values from user to fill a two-dimensional array (Matrix)
having two rows and three columns and display the values in row column format.

#include <iostream>

using namespace std;

int main()

int a[2][3],i,j;

cout<<endl<<"Enter Elements of Arrary"<<endl;

for(i=0;i<2;i++)

for(j=0;j<3;j++)

cout<<endl<<"a["<<i<<"]["<<j<<"]::";

cin>>a[i][j];

}
cout<<endl<<"Number you Entered are in Matrix Form"<<endl;

for(i=0;i<2;i++)

for(j=0;j<3;j++)

cout<<a[i][j]<<"\t";

cout<<endl;

return 0;

Practical No. 104

Task: Write a program that takes values from user to fill a two-dimensional array (Matrix)
having three rows and three columns and display it’s contents and also display the
flipping the same matrix (i.e. reversing the row order) using functions.

const int maxRows = 3;

const int maxCols = 3;

int readMatrix(int arr[][maxCols]);

int displayMatrix(int a[][maxCols]);

int displayFlippedMatrix(int a[][maxCols]);

int main(int)

{
int a[maxRows][maxCols];

readMatrix(a);

cout << "\n\n" << "The original matrix is: " << '\n';

displayMatrix(a);

cout << "\n\n" << "The flipped matrix is: " << '\n';

displayFlippedMatrix(a);

return 0;

int readMatrix(int arr[][maxCols])

int row, col;

for (row = 0; row < maxRows; row ++)

for(col=0; col < maxCols; col ++)

cout << "\n" << "Enter " << row << ", " << col << " element: ";

cin >> arr[row][col];

cout << '\n';

int displayMatrix(int a[][maxCols])

int row, col;

for (row = 0; row < maxRows; row ++)

for(col = 0; col < maxCols; col ++)

cout << a[row][col] << '\t';

}
cout << '\n';

int displayFlippedMatrix(int a[][maxCols])

int row, col;

for (row = maxRows - 1; row >= 0; row --)

for(col = 0; col < maxCols; col ++)

cout << a[row][col] << '\t';

cout << '\n';

}
Practical No. 105

Task: Write a program that should accepts five elements from the user in a 2D-array and
search a particular element (entered by the user) in it. If the element is found in an array
its indexes are displayed, else (not found) a massage “Element not found in an array” is
displayed (Using Linear /Sequential Search Algorithm).

#include <iostream>

using namespace std;

int main()

int mata[2][3],num,i,j,index1,index2,check=0;

cout<<"Enter 5 elements in a matrix : \n\n";

for(i=0;i<2;i++)

for(j=0;j<3;j++)

cin>>mata[i][j];

cout<<"Enter an element to be searched in the matrix : \n\n";

cin>>num;

for(i=0;i<2;i++)

for(j=0;j<3;j++)

if(num==mata[i][j])

check=1;index1=i;index2=j;
}

if(check==1)

cout<<"Element is found at "<<index1+1<<","<<index2+1;

else

cout<<"Element not found in array ";

return 0;

Practical No. 106

Task: Write a program that takes values from user to fill a 2 two-dimensional array
(Matrix) having three rows and three columns and then add these two matrixes and store
the result in another matrix and display all matrixes values in row column format
(Addition of two matrixes).

#include <iostream>

using namespace std;

int main()

int Mata[3][3],Matb[3][3],Matc[3][3],row,col;

cout<<"\n Enter Element of Matrix A"<< endl;

for (row = 0; row < 3; row ++)

{
for(col=0; col < 3; col ++)

cout << "\n" << "Enter " << row << "," << col << "element : ";

cin >> Mata[row][col];

cout<<"\n Enter Element of Matrix B"<< endl;

for (row = 0; row < 3; row ++)

for(col=0; col < 3; col ++)

cout << "\n" << "Enter " << row << "," << col << "element : ";

cin >> Matb[row][col];

for (row = 0; row < 3; row ++)

for(col=0; col < 3; col ++)

Matc[row][col]=Mata[row][col] + Matb[row][col];

cout<<"\n Result Of Matrix A+B is C"<< endl;

for (row = 0; row < 3; row ++)

for(col=0; col < 3; col ++)

cout<<Matc[row][col]<<"\t";

cout<<"\n";
}

return 0;

Practical No. 107

Task: Write a program that takes values from user to fill a 2 two-dimensional array
(Matrix) having any order and then multiply these two matrixes if possible (i.e. Checking
multiply rule first) and store the result in another matrix and display all matrixes values
in row column format (Multiplication of two matrixes).

#include <iostream>

using namespace std;

int main()

int Mata[3][3],Matb[3][3],Matc[3][3]={0},row,col,k;
cout<<"\n Enter Element of Matrix A"<< endl;

for (row = 0; row < 3; row ++)

for(col=0; col < 3; col ++)

cout << "\n" << "Enter " << row << "," << col << "element : ";

cin >> Mata[row][col];

cout<<"\n Enter Element of Matrix B"<< endl;

for (row = 0; row < 3; row ++)

for(col=0; col < 3; col ++)

cout << "\n" << "Enter " << row << "," << col << "element : ";

cin >> Matb[row][col];

for (row = 0; row < 3; row ++)

for(col=0; col < 3; col ++)

for(k=0; k < 3; k++)

Matc[row][col]=Matc[row][col] + Mata[row][k] * Matb[k][col];

cout<<"\n Result Of Matrix A*B is C"<< endl;

for (row = 0; row < 3; row ++)

for(col=0; col < 3; col ++)


{

cout<<Matc[row][col]<<"\t";

cout<<"\n";

return 0;

Practical No. 108

Task: Write a program that takes values from user to fill a two-dimensional array (Square
Matrix) then calculate the transpose of that matrix and display its contents in matrix form.

#include <iostream>
using namespace std;

int main()

int A[3][3],i,j;

cout<<"Enter Elements of Array"<<endl;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cout<<"A["<<i<<"]["<<j<<"]::";

cin>>A[i][j];

cout<<"\nEntered Matrix is \n";

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cout<<A[i][j]<<"\t";

cout<<endl;

cout<<"Transpose of Matrix is"<<endl;

for(j=0;j<3;j++)

for(i=0;i<3;i++)

cout<<A[i][j]<<"\t";

cout<<endl;
}

return 0;

Practical No. 109

Task: Write a program use a single-subscripted array to solve the following problem.
Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is
read, print it only if it is not a duplicate of a number already read. Provide for the “worst
case” in which all 20 numbers are different. Use the smallest possible array to solve this
problem.

Potrebbero piacerti anche