Please design, write the code and create the requisite submission files for the
ID: 3854109 • Letter: P
Question
Please design, write the code and create the requisite submission files for the following problem:
A menu-driven program that offers the user 5 options as follows:
Menu:
1. Display array
2. Multiply each element in the array by 2.
3. Swap the values in row two with the values in row three.
4. Calculate the SUM of all elements and display that number.
5. Exit In main(), declare and initialize a 2-dimensional array that contains 5 x 5 elements. Initialize the elements to: 1,3,5,7,9 -2-4,-6, -8, -10 3,3,3,3,3 55, 77, 99, 22, 33 -15, -250, -350, -450, -550
Display the menu and then execute the user's choice. Keep displaying the menu (and executing the user's choice) until the user chooses Option 5. (See pseudocode in this lesson for design help.) Execute the chosen menu option by passing the array to a specific function which will do the work (in other words, there will be 4 array processing functions and one exit function). No global variables, although global constants are fine. Remember, you must pass the array to the functions and not do the work in main().
. Here's some pseudocode for driving a menu (see how the default part of the switch statement catches the bad menu input?)
choice = 0;
while (choice != 3) //assume 3 is the exit out
choice = functionDisplay
Menu() switch on choice case 1: functionOption1(myArray); break; case 2: functionOption2(myArray); break; case 3: functionExitCode() default: displayError("wrong menu choice") end switch end while Assignment 6&7 Clarification Posted on: Thursday, June 22, 2017 7:39:17 PM PDT Hi Everybody, For the programming assignment, option 2, please double the actual values in the array. Don't print the array doubled and leave the original value alone. Ex: 2->4->8 Same with swapping the rows: change the actual values in the array. Don't display it. Then if I want to see the array, I will choose option 1.
Explanation / Answer
#include <stdio.h>
#include<stdlib.h>
int DisplayMenu(){
int choice;
printf("Menu ");
printf("1.Display array ");
printf("2.Multiply each element in array by 2 ");
printf("3.Swap 2nd and 3rd rows of array ");
printf("4.Calculate the sum of all elements and diaplay the number ");
printf("5.Exit ");
printf("Enter the choice :");
scanf("%d",&choice);
return choice;
}
void option1(int array2[][5])
{
int i,j;
int array1[5][5];
printf("Array is ; ");
for(i=0;i<5;i++)
for(j=0;j<5;j++){
printf("%d ",array2[i][j]);
}
printf(" ");
}
void option2(int array2[][5])
{
int i,j,array1[5][5];
for(i=0;i<5;i++)
for(j=0;j<5;j++)
array2[i][j] = array2[i][j] * 2;
}
void option3(int array2[][5])
{
int i,j,array1[5][5],x;
for(i=0;i<5;i++)
for(j=0;j<5;j++){
if(i==2){
x = array2[i][j];
array2[i][j] = array2[i+1][j];
array2[i+1][j] = x;
}
}
}
void option4( int array2[][5])
{
int sum =0;
int i,j,array1[5][5];
for(i=0;i<5;i++)
for(j=0;j<5;j++)
sum = sum + array2[i][j];
printf("Sum of array elements is : %d ",sum);
}
void option5()
{
exit (0);
}
int main()
{
int array1[5][5]={{1,3,5,7,9},{-2,-4,-6, -8,-10},{3,3,3,3,3},{55,77,99,22,33},{-15,-250,-350,-450,-550}};
int choice = 0;
while(1){
choice = DisplayMenu();
switch(choice)
{
case 1:
option1(array1);
break;
case 2:
option2(array1);
break;
case 3:
option3(array1);
break;
case 4:
option4(array1);
break;
case 5:
option5();
break;
default: printf("Wrong Menu choice ");
}
}
return 0;
}