Examples of arrays in c++ programing
All Contributions
Example of how to Write a program that Multiply Matrix by Passing it to a Function :
#include <iostream>
using namespace std;
void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void display(int mult[][10], int rowFirst, int columnSecond);
int main()
{
int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;
cout << "Enter rows and column for first matrix: ";
cin >> rowFirst >> columnFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rowSecond >> columnSecond;
// If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again.
while (columnFirst != rowSecond)
{
cout << "Error! column of first matrix not equal to row of second." << endl;
cout << "Enter rows and column for first matrix: ";
cin >> rowFirst >> columnFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rowSecond >> columnSecond;
}
// Function to take matrices data
enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);
// Function to multiply two matrices.
multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond);
// Function to display resultant matrix after multiplication.
display(mult, rowFirst, columnSecond);
return 0;
}
void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
int i, j;
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnFirst; ++j)
{
cout << "Enter elements a"<< i + 1 << j + 1 << ": ";
cin >> firstMatrix[i][j];
}
}
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < rowSecond; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
cout << "Enter elements b" << i + 1 << j + 1 << ": ";
cin >> secondMatrix[i][j];
}
}
}
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
int i, j, k;
// Initializing elements of matrix mult to 0.
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
mult[i][j] = 0;
}
}
// Multiplying matrix firstMatrix and secondMatrix and storing in array mult.
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
for(k=0; k<columnFirst; ++k)
{
mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}
void display(int mult[][10], int rowFirst, int columnSecond)
{
int i, j;
cout << "Output Matrix:" << endl;
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
cout << mult[i][j] << " ";
if(j == columnSecond - 1)
cout << endl << endl;
}
}
}
How to Write a program that stores 7 numbers given by the user and find the
average of those numbers :
#include <iostream>
using namespace std ;
int main()
{
float numbers[7], sum = 0, average ;
for (int i = 0 : i < 7 ; i++)
{
cout << " Enter numbers [" << i << "] :" ;
cin >> numbers [i] ;
sum = sum + numbers[i] ;
}
average = sum / 7 ;
cout << " Average =" << average ;
return 0 ;
}
How to write a program to store in an array 10 numbers given by the user, then
display the elements of the array :
#include
using namespace std ;
int main()
{
int numbers[10];
for (int i = 0 : i < 10 ; i++)
{
cout << "Enter numbers[" << i << "] :" ;
cin >> numbers[i] ;
}
cout << " the array is : " ;
for (int i = 0 ; i < 10 ; i++)
{
cout << numbers[i] << " , " ;
}
return 0
}
total contributions (4)
Example of how to Write a program that Multiply Matrix by Passing it to a Function :