Question

Language: C++ Course: Computer Programming for Engineers Write a program that creates two 3x4 arrays of...

Language: C++

Course: Computer Programming for Engineers

Write a program that creates two 3x4 arrays of type "int". Populate each array with random integers between 1 and 9. Also ask the user to choose whether they want to add the arrays or subtract the second array from the first array. Output all three arrays in tabular form.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans:

#include<cstdlib>//for random numbers generation
#include<iostream>//for basic output and input related operations
using namespace std;
int main(){
   //declaring three 3 X 4 matrices
   //with matrix1,matrix2,matrix3 names respectively
   int matrix1[3][4];
   int matrix2[3][4];
   int matrix3[3][4];
   //integer variables i and j for iteration purpose
   //and option is to choose between addition and subtraction
   int i,j,option;
   //using two nested for loops(because we are working with two dimensional arrays;
   //and filling with random value between 1 to 9(rand() % 9 +1))
   for(i=0;i<3;i++){
       for(j=0;j<4;j++){
           //assigning randome values into the matrices
           matrix1[i][j]=rand() % 9 + 1;
           matrix2[i][j]=rand() % 9 + 1;
       }
   }
   //printing first matrix
   cout<<"\n3 X 4 Matrix1\n";
   for(i=0;i<3;i++){
       for(j=0;j<4;j++){
               cout<<matrix2[i][j]<<"\t";
       }
       cout<<endl;
   }
   //printing second matrix
   cout<<"\n3 X 4 Matrix2\n";
   for(i=0;i<3;i++){
       for(j=0;j<4;j++){
           cout<<matrix2[i][j]<<"\t";
       }
       cout<<endl;
   }
   //small menu with two options
   //asking user to select one operation
   cout<<"----------------------";
   cout<<"\n1.matrix addition ";
   cout<<"\n2.matrix subtraction ";
   cout<<"\nchoose one operation :";
   cin>>option;
   //using switch case statements ,executing only selected case
   switch(option){
   //case 1 is for addition of two matrices 1 and two
   case 1:
       //addition code goes here
       cout<<"Addition of Matrix1 and Matrix2\n";
       for(i=0;i<3;i++){
       for(j=0;j<4;j++){
           cout<<matrix1[i][j]+matrix2[i][j]<<"\t";
       }
           cout<<endl;
       }
       break;
   //case 2 is for subtraction of two matrices
   case 2:
       //
       printf("subtraction of Matrix1 and Matrix2\n");
       for(i=0;i<3;i++){
       for(j=0;j<4;j++){
           cout<<matrix1[i][j]-matrix2[i][j]<<"\t";
       }
       cout<<endl;
       }
       break;      
   }
}

-----------------output--------------

Add a comment
Know the answer?
Add Answer to:
Language: C++ Course: Computer Programming for Engineers Write a program that creates two 3x4 arrays of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ help Write a program that: Creates two finite arrays of size 10 These arrays will...

    c++ help Write a program that: Creates two finite arrays of size 10 These arrays will serve as a storing mechanism for student grades in Classes A and B Uses a loop to populate the arrays with randomly generated numbers from 0 to 100.These numbers will represent student grades. Compute average grade of each class. Finally, output the two arrays and the message with two averages and best class. ("Class A average is: 65.43; class B average is: 68.90. Class...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps....

    Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps. This is a basic course and the furthest we have gone is BASIC arrays. Write the code for the following problems using arrays. Write what would go in the “int main()” part of the program. For these problems, the entire program and program output is not needed. Just include what goes in the int main() part of the program. 1. Declare an integer array...

  • Please do in Java with the code available for copy :) Write a computer program that...

    Please do in Java with the code available for copy :) Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 different arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times. In the body of...

  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • Write a computer program that prompts the user for one number, n for the number of...

    Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 different arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times. In the body of the loop, Create an array of n random integers (Make sure...

  • using C programming Compose a program that declares two string arrays that are 81 characters in...

    using C programming Compose a program that declares two string arrays that are 81 characters in size. Prompt the user to enter in a string and store it in array 1. Construct a function that returns a void and takes both arrays as parameters and copies array 1 into array 2 using sizeof() to ensure that the size does not exceed the destination array. Back in the caller print the second array to show that the function worked. NOTE: We...

  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT