Uploaded by Riyan Alvi

Assignment 5(A-B)

advertisement
Computer Fundamentals and
Programming
Assignment 5(A-B)
Submitted By: Riyan Ali Alvi
Department: Electrical Engineering
Program and Session: BS-EE-20-24
Registration Number: BS-20-LB-103921
Submitted To: Dr.Muhammad Tufail
DATED: 01/11/2021
PAKISTAN INSTITUTE OF ENGINEERING AND APPLIED SCIENCES
NILORE, ISLAMABAD
1. Vector Multiplication: Write a C++ program to multiply a row vector with
column vector.
Code
#include <iostream>
using namespace std;
void VecMul(int i,int j) {
float rowv[1][i],colv[j][1],res[1][1];
cout<<"\nEnter Elements of Row Vector: \n";
for(int k=0;k<i;++k)
cin>>rowv[0][k];
cout<<"\nRow Vector: ";
for(int k=0;k<i;++k)
cout<<rowv[0][k]<<" ";
cout<<"\n\nEnter Elements of Column Vector: \n";
for(int m=0;m<i;++m)
cin>>colv[m][0];
cout<<"\nColumn Vector: \n";
for(int m=0;m<i;++m)
cout<<colv[m][0]<<" \n";
res[0][0] = 0;
for(int n=0;n<i;++n)
res[0][0]+=rowv[0][n]*colv[n][0];
cout<<"\nResult: "<<res[0][0];
}
int main() {
int c1,r2;
while(1) {
cout<<"Enter Number of Columns of Row Vector: ";
cin>>c1;
cout<<"Enter Number of Rows of Column Vector: ";
cin>>r2;
if(c1==r2)
break;
cout<<"\nNumber of Columns of Row Vector must be equal to number of Rows
of Column Vector!\n\n"; }
VecMul(c1,r2);
return 0;
}
Output:
2. Transpose: Write a C++ program to transpose a row vector into a column
vector.
Code
#include <iostream>
using namespace std;
int main() {
int size;
cout<<"Enter Number of Columns of Row Vector: ";
cin>>size;
float rowv[1][size],colv[size][1];
cout<<"\nEnter Elements of Row Vector: \n";
for(int k=0;k<size;++k)
cin>>rowv[0][k];
cout<<"\nRow Vector: ";
for(int k=0;k<size;++k)
cout<<rowv[0][k]<<" ";
cout<<"\n\nTranspose: \n";
for(int m=0;m<size;++m)
colv[m][0]=rowv[0][m];
for(int k=0;k<size;++k)
cout<<colv[k][0]<<endl;
return 0;
}
Output:
3. Count the number of words: Write a C++ program to read a character array in
main( ) which could embed blank spaces (use cin.get( ) function). Write a
function countWords( ) to which array is passed and it will return the number of
words in the character array
Code
#include <iostream>
#include <cstring>
using namespace std;
int countWords(char Arr[],int size) {
int wrd=0;
for(int i=0;i<size;++i)
if(Arr[i] == ' ')
++wrd;
return ++wrd;
}
int main() {
int MAX=100;
char str[MAX];
cout<<"Enter a String: ";
cin.get(str,MAX);
cout<<"\nNumber of Words: "<<countWords(str,strlen(str));
return 0;
}
Output:
4. Reading multiple lines of text: Write a C++ program that could read multiple
lines of text and terminates at $ sign.
Code
#include <iostream>
using namespace std;
int main() {
int MAX = 200;
char str[MAX];
cout << "\nEnter a string(Enter '$' to terminate input):\n";
cin.get(str, MAX, '$');
cout << "\nYou entered:\n" << str << endl;
return 0;
}
Output:
5. Removing an Element from an Array: Write a C++ program to remove specific
element from an array.
Code
#include <iostream>
using namespace std;
void Del(int Array[],int& size,int index);
void InputArr(int Array[],int size) {
for(int i=0;i<size;++i) {
cout<<"Enter Element "<<i+1<<" : ";
cin>>Array[i]; }
}
void DispArr(int Array[],int size) {
for(int j=0;j<size;++j)
cout<<Array[j]<<" ";
}
int main() {
int siz,num;
cout<<"Enter number of Elements of Array: ";
cin>>siz;
int Arr[siz];
InputArr(Arr,siz);
cout<<"\nEnetered Array:\n";
DispArr(Arr,siz);
cout<<"\nEnter Number of Element to remove: ";
cin>>num;
--num;
Del(Arr,siz,num);
cout<<"\nModified Array:\n";
DispArr(Arr,siz);
return 0;
}
void Del(int Array[],int& size,int index) {
if (index<size)
{
for(index;index<size;++index)
Array[index] = Array[index+1];
--size;
}
else
cout<<"Error! Number not found inside the array!";
}
Output:
6. Common Elements: Write a C++ program to find the common elements
between two arrays
Code:
#include<iostream>
using namespace std;
void InputArr(int Array[],int size) {
for(int i=0;i<size;++i) {
cout<<"Enter Element "<<i+1<<" : ";
cin>>Array[i]; }
}
void DispArr(int Array[],int size) {
for(int j=0;j<size;++j)
cout<<Array[j]<<" ";
}
int main() {
int size1,size2,size;
cout << "Enter Number of Elements of First Array: ";
cin >>size1;
int array1[size1];
InputArr(array1,size1);
cout<<"\n\nArray 1:\n";
DispArr(array1,size1);
cout << "\n\nEnter Number of Elements of First Array: ";
cin >>size2;
int array2[size2];
InputArr(array2,size2);
cout<<"\n\nArray 2:\n";
DispArr(array2,size2);
int z = 0;
size1 > size2 ? size=size1 : size=size2;
for (int i = 0;i < size;++i)
for (int j = 0;j < size;++j)
if (array1[i] == array2[j])
++z;
if(z!=1)
cout << "\nThere are " << z << " common elements in both arrays. ";
else
cout << "\n\nThere is " << z << " common element in both arrays. ";
return 0;
}
Output
5-B
1. Matrix Multiplication: Write a C++ program to multiply two matrices.
Code
#include <iostream>
using namespace std;
void InputArr(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 DispArr(int mult[][10], int rowFirst, int columnSecond);
int main()
{
int Matrix1[10][10], Matrix2[10][10], mult[10][10], r1, c1, r2, c2;
cout << "Enter rows and column for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and column for second matrix: ";
cin >> r2 >> c2;
while (c1 != r2)
{
cout << "Error! column of first matrix not equal to row of
second." << endl;
cout << "Enter rows and column for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and column for second matrix: ";
cin >> c2 >> c2;
}
InputArr(Matrix1, Matrix2, r1, c1, c2, c2);
multiplyMatrices(Matrix1,Matrix2, mult, r1, c1, c2, c2);
DispArr(mult, r1, c2);
return 0;
}
void InputArr(int firstMatrix[][10], int secondMatrix[][10], int rowFirst,
int columnFirst, int rowSecond, int columnSecond)
{
cout << endl << "Enter elements of matrix 1:" << endl;
for(int i = 0; i < rowFirst; ++i)
for(int 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(int i = 0; i < rowSecond; ++i)
for(int j = 0; j < rowSecond; ++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)
{
for(int i = 0; i < rowFirst; ++i)
for(int j = 0; j < columnSecond; ++j)
mult[i][j] = 0;
for(int i = 0; i < rowFirst; ++i)
for(int j = 0; j < columnSecond; ++j)
for(int k=0; k<columnFirst; ++k)
mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
void DispArr(int mult[][10], int rowFirst, int columnSecond)
{
cout << "Output Matrix:" << endl;
for(int i = 0; i < rowFirst; ++i)
for(int j = 0; j < columnSecond; ++j)
cout << mult[i][j] << " ";
if(j == columnSecond - 1)
cout << endl << endl;
}
Output
2. Matrix Addition: Write a C++ program to add two matrices
Code
#include <iostream>
using namespace std;
int main()
{
int r, c, Matrix1[100][100], Matrix2[100][100], sum[100][100], i, j;
cout << "Enter number of rows: ";
cin >> r;
cout << "Enter number of columns: ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> Matrix1[i][j];
}
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 >> Matrix2[i][j];
}
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = Matrix1[i][j] + Matrix2[i][j];
cout << endl << "Sum of two matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << "
if(j == c - 1)
";
cout << endl;
}
return 0;
}
Output
3. Sum of each row and column: Write a C program to find sum of each row and
column of a matrix.
Code
#include <iostream>
using namespace std;
int main()
{
int size;
int a[size][size];
int row, col, sum = 0;
cout<<"Enter Number of elements: ";
cin>>size;
for(row=0; row<size; row++)
for(col=0; col<size; col++)
{
cout<<"Enter a"<<row+1<<col+1<<": ";
cin>>a[row][col];
}
cout<<"Mtarix:\n";
for(row = 0; row<size; ++row)
for(col = 0; col<size; ++col)
{
cout << a[row][col] << " ";
if(col == size-1)
cout << endl << endl;
}
for(row=0; row<size; row++)
{
sum = 0;
for(col=0; col<size; col++)
{
sum += a[row][col];
}
cout<<"Sum of elements of Row "<< row+1<<": "<< sum<<endl;
}
for(col=0; col<size; col++)
{
sum = 0;
for(row=0; row<size; row++)
{
sum += a[row][col];
}
cout<<"Sum of elements of Column " <<col+1<<": "<<sum<<endl;
}
return 0;
}
Output:
4. Subtraction: Write a C++ program to subtract two matrices.
Code
#include <iostream>
using namespace std;
int main()
{
int r, c, Matrix1[100][100], Matrix2[100][100], diff[100][100], i, j;
cout << "Enter number of rows: ";
cin >> r;
cout << "Enter number of columns: ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> Matrix1[i][j];
}
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 >> Matrix2[i][j];
}
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
diff[i][j] = Matrix1[i][j] - Matrix2[i][j];
cout <<"\nDifference of two matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << diff[i][j] << "
if(j == c - 1)
cout << endl;
}
return 0;
}
Output:
";
5. Interchange Diagonals: Write a C++ program to interchange diagonals of a
matrix
Code
#include<iostream>
using namespace std;
int main()
{
int i, j, rows, columns, temp;
cout << "\nEnter Matrix rows and Columns to interchange Diagonals =
cin >> i >> j;
int intDiagMat[i][j];
cout << "\nPlease Enter the intDiagMat Matrix Items\n";
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < i; columns++)
{
cin >> intDiagMat[rows][columns];}
}
if(rows == columns)
{
for(rows = 0; rows < i; rows++)
{
temp = intDiagMat[rows][rows];
intDiagMat[rows][rows] = intDiagMat[rows][i - rows - 1];
intDiagMat[rows][i - rows - 1] = temp;
";
}
cout << "\nThe intDiagMat Matrix Items after Interchanging
Diagonals are:\n";
for(rows = 0; rows < j; rows++)
{
for(columns = 0; columns < i; columns++)
{
cout << intDiagMat[rows][columns] << " ";
}
cout << "\n";
}
}
else{
cout << "\nThe Matrix that you entered is Not a Square matrix" ;}
return 0;
}
Output:
6. Sum of Lower Triangular Matrix: Write a C program to find sum of lower
triangular matrix
Code
#include<iostream>
using namespace std;
int main()
{
int i, j, arr[10][10], sum, rows, cols;
cout<<"\n Enter Number of Rows(Triangular Matrix): ";
cin>>rows;
cout<<"Enter Number of Columns : ";
cin>>cols;
cout<<"Enter Elements:\n";
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
cout<<"Enter a"<<i+1<<j+1<<": ";
cin>>arr[i][j];
}
}
cout<<"\n Matrix is : \n";
for (i = 0; i < rows; i++)
{
cout<<" ";
for (j = 0; j < cols; j++)
{
cout<<" ";
cout<<arr[i][j];
}
cout<<"\n";
}
cout<<"\n Lower Triangle Elements : \n";
for (i = 0; i < rows; i++)
{
cout<<" ";
for (j = 0; j < cols; j++)
{
if (i > j)
{
cout<<" ";
cout<<arr[i][j];
}
}
cout<<"\n";
}
sum = 0;
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
{
if (i > j)
{
sum = sum + arr[i][j];
}
}
cout<<"Sum of Lower Triangle Elements : "<<sum;
return (0);
}
Output:
7. Transpose of a Matrix: Write a C++ program to find the transpose of a matrix
Code
#include <iostream>
using namespace std;
int main() {
int a[10][10], transpose[10][10], row, column;
cout << "Enter Number of Rows: ";
cin >> row;
cout << "Enter Number of Columns: ";
cin >> column;
cout << "Enter elements of matrix:\n";
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
}
cout << "\nEntered Matrix: " << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << " " << a[i][j];
if (j == column - 1)
cout << endl << endl;
}
}
for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j) {
transpose[j][i] = a[i][j];
}
cout << "\nTranspose of Matrix: " << endl;
for (int i = 0; i < column; ++i)
for (int j = 0; j < row; ++j) {
cout << " " << transpose[i][j];
if (j == row - 1)
cout << endl << endl;
}
return 0;
}
Output:
8. Determinant of a Matrix: Write a C++ program to find determinant of a
matrix.
Code
#include<iostream>
#include<cmath>
using namespace std;
int determinant( int matrix[10][10], int n) {
int det = 0;
int submatrix[10][10];
if (n == 2)
return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
else {
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x)
continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n 1 ));
}
}
return det;
}
int main() {
int n, i, j;
int matrix[10][10];
cout << "Enter the size of the matrix: ";
cin >> n;
cout << "Enter the elements of the matrix:\n";
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> matrix[i][j];
cout<<"The entered matrix is:"<<endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
cout << matrix[i][j] <<" ";
cout<<endl;
}
cout<<"Determinant of the matrix is "<< determinant(matrix, n);
return 0;
}
Output:
Download