Question

Using Matlab Write a program which will: a. Accept two numbers from the user m and...

Using Matlab

Write a program which will:
a. Accept two numbers from the user m and n
b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive.
c. Provide the user the following menu from which he can select:
1. Sum of all elements in the array. Print the result.
2. Sum of the element in a row that he’ll specify. Print the result.
3. Sum of the element in a column that he’ll specify. Print the result.
4. Sum of the elements in the right diagonal. Print the result.
5. Sum of the elements in the left diagonal. Print the result.
6. The element with the minimum value in the array. Print the result.
7. The element with the maximum value in the array. Print the result.
8. The element with the minimum value in a row that he’ll specify. Print the result.
9. The element with the maximum value in a row that he’ll specify. Print the result.
10. The element with the minimum value in a column that he’ll specify. Print the result.
11. The element with the maximum value in a column that he’ll specify. Print the result.
12. Print the menu again
13. Exit from the program
d. The user can elect from the menu as any times as he wants without leaving the program.

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

Code Explanation:

  • Initially input number of rows and columns as n and m
  • create a matrix with size n,m and fill it with zeros by using zeros(n,m)
  • Run a for loop from i=1 to n and assign each row of matrix with random integers
  • display the newly created matrix with random integers
  • Call the print_menu() function to print the menu.This function also returns the choice we input
  • use while until choice is not equal to 13. If choice is 13 then loop breaks and program ends
  • Calculate and display the appropriate results which were asked
  • If choice is 1 then print sum of array. To sum alla elements in an arrya use sum(sum(A)). The inner sum(A) calculates sum of every column and forms a row vector. The outer sum calculates the sum of elements in row vector which gives sum of array elements.
  • If choice is 2 we shuould calculate the sum of specified row elements . We can calculate it by using sum(A(r,:)). Here A(r,:) gives the rth row.
  • If choice is 3 we should calculate the sum of specified column elements. sum(A(:,c))
  • If choice is 4 sum of right diagonal is calculated sum(diag(A)). We can get right diagonal by using diag(A).
  • If choice is 5 we calculate the sum of left diagonal. left diagonal is diag(flip(A))
  • If choice is 6 to calculate min value in array. min(min(A)) . inner min(A) gives min of every column and forms a row vector. Then outer min() gives the min of array
  • If choice is 7 Similar to 6 we use max in place of min
  • If choice is 8 then calculate min of specified row. min(A(r,:))
  • If choice is 9 then calculate max of specified row. max(A(r,:))
  • If choice is 10 calculate min of specified column. min(A(:,c))
  • If choice is 11 calculate max off specified column max(A(:,c))
  • If choice is 12 print menu
  • If choice is 13 exit the program

Code:

n =input("Enter n");

m =input("Enter m");

A =zeros(n,m);

for i=1:n

A(i,:)= randi([-100 100],1,m);

end

disp("The generated matrix with random integers is:\n");

disp(A);

choice = print_menu();

while(choice ~= 13)

if(choice == 1)

disp(sum(sum(A)));

elseif(choice == 2)

r = input("Enter row number");

disp(sum(A(r,:)));

elseif(choice ==3)

c = input("Enter column number :");

disp(sum(A(:,c)));

elseif(choice == 4)

disp(sum(diag(A)));

elseif(choice == 5)

disp(sum(diag(flip(A))));

elseif(choice == 6)

disp(min(min(A)));

elseif(choice == 7)

disp(max(max(A)));

elseif(choice == 8)

r = input("Enter a row number: ");

disp(min(A(r,:)));

elseif(choice == 9)

r = input("Enter a row number: ");

disp(max(A(r,:)));

elseif(choice == 10)

c = input("Enter a column number: ");

disp(min(A(:,c)));

elseif(choice == 11)

c = input("Enter a column number: ");

disp(max(A(:,c)));

elseif(choice == 12)

choice = print_menu();

elseif(choice ==13)

break

else

disp("Please enter a valid choice");

end

choice = input("select an option from menu");

end

function choice = print_menu()

disp("1.Sum of elements in the array\n");

disp("2.Sum of elements in a row\n");

disp("3.Sum of elements in a column\n");

disp("4.Sum of elements in right diagonal\n");

disp("5.Sum of elements in left diagonal\n");

disp("6.The element with minimum value in the array\n");

disp("7.The element with maximum value in the array\n");

disp("8.The element with minimum value in a row\n");

disp("9.The element with maximum value in a row\n");

disp("10.The element with minimum value in a column\n");

disp("11.The element with maximum value in a column\n");

disp("12.print the menu again\n");

disp("13.Exit from the program\n");

choice = input("Select an option from menu");

end

O/P:

Enter n

4

Enter m

5

The generated matrix with random integers is:\n
-30 66 17 10 84
-43 52 51 -24 14
-85 -90 6 56 87
-74 14 -6 -98 -33

1.Sum of elements in the array\n
2.Sum of elements in a row\n
3.Sum of elements in a column\n
4.Sum of elements in right diagonal\n
5.Sum of elements in left diagonal\n
6.The element with minimum value in the array\n
7.The element with maximum value in the array\n
8.The element with minimum value in a row\n
9.The element with maximum value in a row\n
10.The element with minimum value in a column\n
11.The element with maximum value in a column\n
12.print the menu again\n
13.Exit from the program\n
Select an option from menu

1

-26

select an option from menu

5

-103

select an option from menu

6

-98

select an option from menu

8

Enter a row number:

2

-43

select an option from menu

9

Enter a row number:

3

87

select an option from menu

12

1.Sum of elements in the array\n
2.Sum of elements in a row\n
3.Sum of elements in a column\n
4.Sum of elements in right diagonal\n
5.Sum of elements in left diagonal\n
6.The element with minimum value in the array\n
7.The element with maximum value in the array\n
8.The element with minimum value in a row\n
9.The element with maximum value in a row\n
10.The element with minimum value in a column\n
11.The element with maximum value in a column\n
12.print the menu again\n
13.Exit from the program\n
Select an option from menu

11

select an option from menu

13

>>

Code screenshot:

1 - 2 3- 4 - 5- 6 7- disp(sum(AC:,0))); untitled.mx + n =input(Enter n); m =input(Enter m); A =zeros(n,m); for i=1:n Ali,27 - 28 - 29 30 31 - 32 - - 33 - 34 35 - 36 - 37 - 38 - 39 elseif (choice == 8) r = input(Enter a row number: ); disp(min(A53 - 54 - 55 56 - 57 - disp(5.Sum of elements in left diagonal\n); disp(6. The element with minimum value in the array\n)

O/P screenshot:

Enter n 4 Enter m 5 The generated matrix with random integers is:\n -30 66 17 19 84 -43 52 51 - 24 14 -85 -90 6 56 87 - 74 1413. Exit from the programın Select an option from menu 1 -26 select an option from menu 5 -103 select an option from menu 6 -3 87 select an option from menu 12 1. Sum of elements in the array\n 2.Sum of elements in a row\n 3.Sum of elements in a colu

(Please comment If you still have any doubts. I will definitely help)

Add a comment
Know the answer?
Add Answer to:
Using Matlab Write a program which will: a. Accept two numbers from the user m and...
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
  • Using MATLAB 15. Write a program that: a. b. c. Accept two numbers n and m...

    Using MATLAB 15. Write a program that: a. b. c. Accept two numbers n and m in the range of 1-6 Define three arrays A, B and Cof size n by m. (n-# of rows, m-H of columns) Fill the elements of array A by A(ij)-itj i-5 d. Fill the elements of array B by B(ij)ij e. Fill the elements of array C with the average of the corresponding elements of A f. g. h. and B Print the results...

  • 2. Write a program that reads N integer numbers of array from the user and then...

    2. Write a program that reads N integer numbers of array from the user and then displays the sum, max number and the numbers of the array use four subroutines ( Read_Array(), Calculate_Sum(), Find_Max() and Display_Array()). Here is sample output Please enter number of elements: 4 Please enter the required numbers: 50 100 150 20 The entered numbers are: 50 100 150 20 Sum is : 320 Max is 150 by Mpsi ,sum and max to call subroutine and retrieve...

  • Write a program which will take a list of integer number from the user as input...

    Write a program which will take a list of integer number from the user as input and find the maximum and minimum number from the list. First, ask the user for the size of the array and then populate the array. Create a user define function for finding the minimum and maximum numbers. You have to use pointer variables for solving the problem. Finally, you need to print the entire list along with the max and min numbers ALSO NEED:...

  • Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load...

    Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load them into an array. Add up all the values and store the answer in a variable. Find the largest and smallest number in the array. Put each into its own variable. Print to the screen the sum, largest value, and smallest value. Copy the array to a second array in reverse order. Print out the second array. Read in 20 integer numbers, all in...

  • Done in C++ using visual studio 1. Write a program with one additional function called int[]...

    Done in C++ using visual studio 1. Write a program with one additional function called int[] reverseArray(int array). This function will receive an array from main and then reverse and should return the reversed array to the main to print out. Use a single array and a single loop, you’re are neither allowed to print out just in reverse order nor allowed to use another array variable to store the original array in reverse order. 2. Write a program which...

  • Write a C Language Program that will ask the user how many elements the "maxminarray" will...

    Write a C Language Program that will ask the user how many elements the "maxminarray" will have. Read the elements from user and fill the "maxminarray” with it. Find maximum value and minimum value in maxminarray See the output below: Input number of elements of the array :6 Input 6 elements for the maxminarray : element [0]: 23 element [1] : 56 element [2] : 11 element [3]: 78 element [4]: 21 element [5]: 400 The Maximum element is :...

  • Write a program that will accept two numbers from the user. The program will then print...

    Write a program that will accept two numbers from the user. The program will then print all the numbers from the lowest number to the highest number. Make sure to include both numbers in the count. Also, it should not matter what order you input the two numbers. c++  

  • Using Java In this assignment we are working with arrays. You have to ask the user...

    Using Java In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in -long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; -But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two...

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
ADVERTISEMENT