Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Instructions: C++ a) Use scanf function to accept the input from the user. b) Us

ID: 3704390 • Letter: I

Question

Instructions: C++ a) Use scanf function to accept the input from the user. b) Use floating point inputs wherever possible. c) All programs must use arrays and functions. d) Tabulate your results, e) Need hard copy of (i) source code and (ii) output for each program 1. Write a program that can multiply an n x m matrix and m x n matrix together: The input specifications are these: Read n and m using scanf function. Read two matrices using a for loop inside the main function. The output (product) matrix must be computed in a function named matrix mult which will have the two input matrices as arguments 2. Write a program that asks a user to roll a single die twice to get a sum value of 7. If the sum is 11, the user loses. If the sum is neither 7 nor 11, the user neither wins nor loses, meaning that there is no decision. The computations have to be made inside a function named it is rolled. . The input arguments are the numbers that show up in each die when

Explanation / Answer

#include<iostream>
#include<cstdio>
#include<iomanip>

using namespace std;

void InputMatrixData(int firstMatrix[][10], int secondMatrix[][10], int firstRow, int firstColumn, int secondRow, int secondColumn);
void MatrixMultiply(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int firstRow, int firstColumn, int secondRow, int secondColumn);
void DisplayMatrix(int mult[][10], int firstRow, int secondColumn);

int main()
{
   int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], firstRow, firstColumn, secondRow, secondColumn, i, j, k;

   cout << "Enter rows and column for first matrix: ";
   scanf("%d %d", &firstRow, &firstColumn);

   cout << "Enter rows and column for second matrix: ";
   scanf("%d %d", &secondRow, &secondColumn);

   // If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again.
   while (firstColumn != secondRow)
   {
   cout << "Error! column of first matrix not equal to row of second." << endl;
   cout << "Enter rows and column for first matrix: ";
   scanf("%d %d", &firstRow, &firstColumn);
   cout << "Enter rows and column for second matrix: ";
   scanf("%d %d", &secondRow, &secondColumn);
   }

   // Function to take matrices data
   InputMatrixData(firstMatrix, secondMatrix, firstRow, firstColumn, secondRow, secondColumn);

   // Function to multiply two matrices.
   MatrixMultiply(firstMatrix, secondMatrix, mult, firstRow, firstColumn, secondRow, secondColumn);

   // Function to DisplayMatrix resultant matrix after multiplication.
   DisplayMatrix(mult, firstRow, secondColumn);

   return 0;
}

void InputMatrixData(int firstMatrix[][10], int secondMatrix[][10], int firstRow, int firstColumn, int secondRow, int secondColumn)
{
   int i, j;
   cout << endl << "Enter elements of matrix 1:" << endl;
   for(i = 0; i < firstRow; ++i)
   {
   for(j = 0; j < firstColumn; ++j)
   {
   cout << "Enter elements a"<< i + 1 << j + 1 << ": ";
   scanf("%d", &firstMatrix[i][j]);
   }
   }

   cout << endl << "Enter elements of matrix 2:" << endl;
   for(i = 0; i < secondRow; ++i)
   {
   for(j = 0; j < secondColumn; ++j)
   {
   cout << "Enter elements b" << i + 1 << j + 1 << ": ";
   scanf("%d", &secondMatrix[i][j]);
   }
   }
}

void MatrixMultiply(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int firstRow, int firstColumn, int secondRow, int secondColumn)
{
   int i, j, k;

   // Initializing elements of matrix mult to 0.
   for(i = 0; i < firstRow; ++i)
   {
   for(j = 0; j < secondColumn; ++j)
   {
   mult[i][j] = 0;
   }
   }

   // Multiplying matrix firstMatrix and secondMatrix and storing in array mult.
   for(i = 0; i < firstRow; ++i)
   {
   for(j = 0; j < secondColumn; ++j)
   {
   for(k=0; k<firstColumn; ++k)
   {
   mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
   }
   }
   }
}

void DisplayMatrix(int mult[][10], int firstRow, int secondColumn)
{
   int i, j;

   cout << endl <<"Output Matrix:" << endl;
   for(i = 0; i < firstRow; ++i)
   {
   for(j = 0; j < secondColumn; ++j)
   {
   cout << setw(4) << mult[i][j] << " ";
   if(j == secondColumn - 1)
   cout << endl << endl;
   }
   }
}*/

/****************** OUTPUT OF PROGRAM *******************
>> g++ matrix_multiplication.cpp
>> ./a.out
Enter rows and column for first matrix: 3 3
Enter rows and column for second matrix: 3 3

Enter elements of matrix 1:
Enter elements a11: 1
Enter elements a12: 2
Enter elements a13: 3
Enter elements a21: 4
Enter elements a22: 5
Enter elements a23: 6
Enter elements a31: 7
Enter elements a32: 8
Enter elements a33: 9

Enter elements of matrix 2:
Enter elements b11: 9
Enter elements b12: 8
Enter elements b13: 7
Enter elements b21: 6
Enter elements b22: 5
Enter elements b23: 4
Enter elements b31: 3
Enter elements b32: 2
Enter elements b33: 1

Output Matrix:
  30 24 18

  84 69 54

138 114 90

********************************************************/


#include<iostream>
#include<cstdio>
using namespace std;

void roll_die_twice (int num1, int num2);
int main () {
   int num1, num2, die = 0;
   for (int i = 0 ; i < 2 ; i++) {
   die++;
   if (die == 1) {
   cout << " Roll the die for the first time" << endl;
   cout << "Enter the number for the first roll(less than 7) :: ";
   scanf("%d", &num1);
   } else {
   cout << " Roll the die for the second time" << endl;
   cout << "Enter the number for the second roll(less than 7) :: ";
   scanf("%d", &num2);
   }
   }
   roll_die_twice (num1, num2);
   return 0;
}


void roll_die_twice (int num1, int num2)
{
   int total = num1 + num2;
   cout << endl;
   if (total == 7)
   cout << "******* You won the game **********" << endl;
   else if (total == 11)
   cout << "********* You lost the game *********" << endl;
   else
   cout << "******** You neither won neither lost the game *******" << endl;
}

/****************** OUTPUT OF PROGRAM *******************
>> g++ dual_die.cpp
>> ./a.out

Roll the die for the first time
Enter the number for the first roll(less than 7) :: 5

Roll the die for the second time
Enter the number for the second roll(less than 7) :: 6

********* You lost the game *********

>> ./a.out

Roll the die for the first time
Enter the number for the first roll(less than 7) :: 4

Roll the die for the second time
Enter the number for the second roll(less than 7) :: 3

******* You won the game **********

>> ./a.out

Roll the die for the first time
Enter the number for the first roll(less than 7) :: 3

Roll the die for the second time
Enter the number for the second roll(less than 7) :: 5

******** You neither won neither lost the game *******

********************************************************/