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

#include <stdio.h> #include<stdlib.h> int DisplayMenu(){ int choice; printf(\"Me

ID: 3854193 • Letter: #

Question

#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;
}

Part 1:

Rewrite your program from last week to utilize a pointer instead of an array as follows:

1. Declare the int array and initialize it as before. (You can't point at a 2D array in C++, so create an 1-dim array with 25 elements.)

2. Declare an int pointer and point to the array. From then on in the program, you may only use the pointer. You may not use the array or any array subscripting in any other part of your program. You must only use pointer addressing. HOWEVER, you must always display the array and manipulate it as if it were still a 2-dim array of 5x5.

3. Make sure your code is well-organized and well-documented.

Here's a smaller example of what I mean about setting up the pointer and not using array subscripts:

   int balance[4] = {100, 2, 3, 17}; //here I am declaring my array

   int *point; // declaring the pointer

   point = balance; //pointing to the array

   for ( int i = 0; i < 3; i++ )

        cout << *(point + i) << endl;   //using the pointer to access array elements

PROVIDED CODE:

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 *p)
{

int i;
printf("Array is : ");
for(i=0;i<25;i++)
printf("%d ",*(p+i));

printf(" ");
}
void option2(int *p)
{

int i;
for(i=0;i<25;i++)
*(p+i) = *(p+i) * 2;
}


void option3(int *p)
{

int i,x;
for(i=0;i<25;i++)
{
if(i>=5 && i<=9) // elements of 2nd and 3rd rows are swapped
{
x = *(p+i);
*(p+i) = *(p+i+5);
*(p+i+5) = x;
}
}
}


void option4( int *p)
{
int sum =0;
int i;
for(i=0;i<25;i++)
sum = sum + *(p+i);
printf("Sum of array elements is : %d ",sum);
}


void option5()
{
exit (0);
}


int main()
{

int array[25] ={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 *p = array;
int choice = 0;
while(1){

choice = DisplayMenu();
switch(choice)
{

case 1:
option1(p);
break;
case 2:
option2(p);
break;
case 3:
option3(p);
break;
case 4:
option4(p);
break;
case 5:
option5();
break;
default: printf("Wrong Menu choice ");
}
}
return 0;
}

Output:

Menu
1.Display array
2.Multiply each element in array by 2
3.Swap 2nd and 3rd rows of array
4.Calculate the sum of all elements and diaplay the number
5.Exit
Enter the choice : 1

Array is :
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
Menu
1.Display array
2.Multiply each element in array by 2
3.Swap 2nd and 3rd rows of array
4.Calculate the sum of all elements and diaplay the number
5.Exit
Enter the choice : 3

Menu
1.Display array
2.Multiply each element in array by 2
3.Swap 2nd and 3rd rows of array
4.Calculate the sum of all elements and diaplay the number
5.Exit
Enter the choice :1

Array is :
1 3 5 7 9 3 3 3 3 3 -2 -4 -6 -8 -10 55 77 99 22 33 -15 -250 -350 -450 -550
Menu
1.Display array
2.Multiply each element in array by 2
3.Swap 2nd and 3rd rows of array
4.Calculate the sum of all elements and diaplay the number
5.Exit
Enter the choice :2

Menu
1.Display array
2.Multiply each element in array by 2
3.Swap 2nd and 3rd rows of array
4.Calculate the sum of all elements and diaplay the number
5.Exit
Enter the choice : 1

Array is :
2 6 10 14 18 6 6 6 6 6 -4 -8 -12 -16 -20 110 154 198 44 66 -30 -500 -700 -900 -1100
Menu
1.Display array
2.Multiply each element in array by 2
3.Swap 2nd and 3rd rows of array
4.Calculate the sum of all elements and diaplay the number
5.Exit
Enter the choice :4

Sum of array elements is : -2638
Menu
1.Display array
2.Multiply each element in array by 2
3.Swap 2nd and 3rd rows of array
4.Calculate the sum of all elements and diaplay the number
5.Exit
Enter the choice :1

Array is :
2 6 10 14 18 6 6 6 6 6 -4 -8 -12 -16 -20 110 154 198 44 66 -30 -500 -700 -900 -1100
Menu
1.Display array
2.Multiply each element in array by 2
3.Swap 2nd and 3rd rows of array
4.Calculate the sum of all elements and diaplay the number
5.Exit
Enter the choice :5