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

Array & Function PROGRAM

The document provides C++ code examples demonstrating matrix addition, function definition, factorial calculation, binary to decimal conversion, and number swapping using both call by value and call by reference. Each section includes code snippets and sample outputs to illustrate the functionality. The examples are aimed at helping users understand basic programming concepts in C++.

Uploaded by

Jitendra Dangra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Array & Function PROGRAM

The document provides C++ code examples demonstrating matrix addition, function definition, factorial calculation, binary to decimal conversion, and number swapping using both call by value and call by reference. Each section includes code snippets and sample outputs to illustrate the functionality. The examples are aimed at helping users understand basic programming concepts in C++.

Uploaded by

Jitendra Dangra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Matrix Addition 2 D (dimensional) or Multidimensional Array Example

Program In C++

#include <iostream>
#include<conio.h>

using namespace std;

int main()
{
int rowCount, columnCount, i, j;
int firstMatrix[10][10], secondMatrix[10][10], resultMatrix[10][10];

cout<<"Simple C++ Example Program for 2 D (dimensional) Array Matrix


Addition Example\n";

cout<<"Number of rows of matrices to be added : ";


cin>>rowCount;

cout<<"Number of columns matrices to be added : ";


cin>>columnCount;

cout<<"Elements of first matrix : \n";

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


for (j = 0; j < columnCount; j++)
cin>>firstMatrix[i][j];

cout<<"Elements of second matrix : \n";


for (i = 0; i < rowCount; i++)
for (j = 0; j < columnCount; j++)
cin>>secondMatrix[i][j];

cout<<"Sum of entered matrices : \n";

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


{
for (j = 0; j < columnCount; j++)
{
resultMatrix[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
cout<<resultMatrix[i][j]<<"\t";
}
cout<<"\n";
}

getch();

return 0;
}
Sample Output
Number of rows of matrices to be added : 2
Number of columns matrices to be added : 2
Elements of first matrix :
5
5
5
5
Elements of second matrix :
1
1
1
1
Difference of entered matrices :
6 6
6 6

Defining a Function
A function is like a black box. It takes in input, does something with it, and
then spits out an answer.

Syntax

return_type function_name(argumnent)
{
// body Of the Function
}

Simple Example Program Of Function Find Factorial Number

#include<iostream>
#include<conio.h>

using namespace std;

// Simple factorial Function


int factorial(int var) {
int fact = 1;
for (int i = 1; i <= var; i++)
fact = fact * i;
return fact;
}

int main() {
cout << "5 Factorial Number :" << factorial(5);
getch();
return 0;
}

Output

5 Factorial Number: 120

/* C++ Program to Convert Binary Number to Decimal using functions */

#include <iostream>
#include <cmath>
using namespace std;
int convertBinaryToDecimal(long long);
int main()
{
long long n;

cout << "\nEnter any binary number :: ";


cin >> n;
cout <<"\n [ " << n << " ] in binary = [ " << convertBinaryToDecimal(n) << " ] in
decimal"<< endl ;
return 0;
}
int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}
OUTPUT:
/* C++ Program to Convert Binary Number to Decimal using functions */

Enter any binary number :: 111111

[ 111111 ] in binary = [ 63 ] in decimal

Process returned 0

Program to Swap Two Numbers Using Functions In C++

Call by Value

#include<iostream>

using namespace std;

void swap(int ,int );

/*Swapping of Two Numbers in C++ Using Functions Call by Value*/

int main()

int a,b;

cout<<"Enter the Two Numbers to Swap in C++: ";

cin>>a>>b;

cout<<"\nAfter Swapping of Two Numbers:";

swap(a,b);
return 0;

void swap(int x,int y)

int z;

/*Extra veriable for storing the value of first or second variable*/

z=x;

/*Copying the first variable value to the tempriory variable*/

x=y;

/*Copying the second variable value to the first variable*/

y=z;

/*Copying the tempriory variable value to the second variable*/

cout<<" "<<x<<" "<<y;

Output:

Enter the Two Numbers to Swap in C++: 8 6

After Swapping of Two Numbers: 6 8

Call by Reference

#include<iostream>

using namespace std;

void swap(int *x ,int *y );

/*Swapping of Two Numbers in C++ Using Functions Call by Reference*/

int main()

{
int a,b;

cout<<"Enter Two Numbers To Swap: ";

cin>>a>>b;

swap(&a,&b);

cout<<"\nAfter Swapping Two Numbers: ";

cout<<a<<" "<<b<<" \n";

return 0;

void swap(int *x,int *y)

int z;

z=*x;

/*Copying the first variable Address to the temporary variable*/

*x=*y;

/*Copying the second variable Address to the first variable*/

*y=z;

/*Copying the temporary variable Address to the second variable*/

Output:

Enter Two Numbers To Swap: 4 56

After Swapping Two Numbers: 56 4

You might also like