Вы находитесь на странице: 1из 30

2018-2019

NAME: SOHAN CHAKRABORTY


ROLL NO:
Array Multiplication (Matrix
Multiplication) Program
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr(); //clears the screen
int a[10][10], b[10][10], mult[10][10], r1, c1,
r2, c2, i, j, k;
cout << "Enter rows and columns for first
matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for
second matrix: ";
cin >> r2 >> c2;
/* If colum of first matrix in not equal to row
of second matrix, asking user to enter the
size of matrix again. */
while (c1!=r2)
{
cout << "Error! column of first matrix not
equal to row of second.";
cout << "Enter rows and columns for
first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for
second matrix: ";
cin >> r2 >> c2;
}
/* Storing elements of first matrix. */
cout << endl << "Enter elements of matrix
1:" << endl;
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
cout << "Enter element a" << i+1 << j+1
<< " : ";
cin >> a[i][j];
}
/* Storing elements of second matrix. */
cout << endl << "Enter elements of matrix
2:" << endl;
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
cout << "Enter element b" << i+1 << j+1
<< " : ";
cin >> b[i][j];
}
/* Initializing elements of matrix mult to 0.*/
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in
array mult. */
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}
/* Displaying the multiplication of two
matrix. */
cout << endl << "Output Matrix: " << endl;
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
cout << " " << mult[i][j];
if(j==c2-1)
cout << endl;
}
getch(); // wait for input
}
Dynamic Memory Allocation
for Arrays Example

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr(); //clears the screen
int i,n;
int * p;
cout << "How many numbers would you
like to type? ";
cin >> i;
p= new (nothrow) int[i];
if (p == nullptr)
cout << "Error: memory could not be
allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
getch(); // wait for input
}
Random Number Example
using(rand() function

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
clrscr(); //clears the screen
//printing random numbers between 1 to
100
int i = 0;
while(i++ < 10) {
int r = (rand() % 100) + 1;
cout << r << " ";
}
getch(); // wait for input
}
Fibonacci Series using
Recursion

#include <iostream.h>
#include <conio.h>
int fibonacci(int n)
{
if((n==1)||(n==0))
{
return(n);
}
else
{ //recursive function call
return(fibonacci(n-1)+fibonacci(n-
2));
}
}
void main()
{
clrscr(); //clears the screen
int n,i=0;
cout<<"Input the number of terms for
Fibonacci Series:";
cin>>n;
cout<<endl<<"Fibonnaci Series is as
follows";
while(i<n)
{ cout<<" "<<fibonacci(i)<<endl;
i++;
}
getch(); // wait for input
}
Array Addition (Matrix
Addition) Program

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr(); //clears the screen
int
r,c,a[100][100],b[100][100],sum[100][100]
,i,j;
cout << "Enter number of rows
(between 1 and 100): ";
cin >> r;
cout << "Enter number of columns
(between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st
matrix: " << endl;
/* Storing elements of first matrix
entered by user. */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element a" << i+1
<< j+1 << " : ";
cin >> a[i][j];
}
/* Storing elements of second matrix
entered by user. */
cout << endl << "Enter elements of 2nd
matrix: " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element b" << i+1
<< j+1 << " : ";
cin >> b[i][j];
}
/*Adding Two matrices */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];
/* Displaying the resultant sum matrix. */
cout << endl << "Sum of two matrix is: "
<< endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << sum[i][j] << " ";
if(j==c-1)
cout << endl;
}
getch(); // wait for input
}
Array Subtraction (Matrix
Subtraction) Program

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr(); //clears the screen
int r,c,a[100][100],b[100][100],
sum[100][100],i,j;
cout << "Enter number of rows
(between 1 and 100): ";
cin >> r;
cout << "Enter number of columns
(between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st
matrix: " << endl;
/* Storing elements of first matrix
entered by user. */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element a" << i+1
<< j+1 << " : ";
cin >> a[i][j];
}
/* Storing elements of second matrix
entered by user. */
cout << endl << "Enter elements of 2nd
matrix: " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element b" << i+1
<< j+1 << " : ";
cin >> b[i][j];
}
/*Subtracting Two matrices */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]-b[i][j];
/* Displaying the resultant sum matrix. */
cout << endl << "Sum of two matrix is: "
<< endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << sum[i][j] << " ";
if(j==c-1)
cout << endl;
}
getch(); // wait for input
}
Decimal to Binary Conversion
#include <iostream.h>
#include <conio.h>
void main()
{clrscr(); //clears the screen
long dec,rem,i=1,sum=0;
cout<<"Enter the decimal to be
converted:";
cin>>dec;
do
{ rem=dec%2;
sum=sum + (i*rem);
dec=dec/2;
i=i*10;
}while(dec>0);
cout<<"The binary of the given
number is:"<<sum<<endl;
getch(); // wait for input}
WAP to print FLOYDS TRIANGLE
pattern

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int rows,i,j,k=0;
cout<<"Enter number of
rows: ";
cin>>rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;++j)
cout<<k+j<<" ";
++k;
cout<<endl;
}
getch();
}
WAP to make a simple
calculator using switch
statement

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
char o;
float num1,num2;
cout<<"Select an operator
either + or - or * or / \n";
cin>>o;
cout<<"Enter two operands:
";
cin>>num1>>num2;
switch(o) {
case '+':
cout<<num1<<" +
"<<num2<<" = "<<num1+num2;
break;
case '-':
cout<<num1<<" -
"<<num2<<" = "<<num1-num2;
break;
case '*':
cout<<num1<<" *
"<<num2<<" = "<<num1*num2;
break;
case '/':
cout<<num1<<" /
"<<num2<<" = "<<num1/num2;
break;
default
printf("Error!
operator is not correct");
break;
}
getch();
}
return 0; }
WAP to print factorial of a
number using recursive
function

#include <iostream.h>
#include <conio.h>
int factorial(int n);
void main()
{
clrscr();
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factorial of " << n << " = " <<
factorial(n);
getch();
}
int factorial(int n)
{
if(n!=1)return n*factorial(n-1);}
WAP to enter a number and
check if it is positive
negative or equal to zero

#include<iostream.>
#include<conio.h>
void main() {
clrscr();
int number;
cout<< "Enter an integer:
";
cin>> number;
if ( number > 0) {
cout << "You entered a
positive integer:
"<<number<<endl;
}
else if (number < 0){
cout<<"You entered a
negative integer:
"<<number<<endl;
}
else {
cout<<"You entered
0."<<endl;
}
getch();
}

Вам также может понравиться