In: Computer Science
1COMP1410 - Lab Exercises #3 (Due at the end of the lab period or beginning of the next) Start Date: Oct. 1st, 2019
Objectives: Practice dealing with 2D arrays.
Create a two dimensional array (e.g. int A2D[M][N];) of size MxNto store integer values. Use #define M 4and N 3to start. (Using symbolic constants instead of hard coding the array sizes improves scalability). Create an interactive menu within main() for this program (call it Lab3.c) with choices to fill the array with random numbers, search the array, right shift the array, print the array and quit.
Your program must code and document the following functions:
printArray2D()to print the array in a table format (use formatting codes to achieve this).
populateRandom2D()to populate the array with pseudo-random numbers between minand max numbers entered by the user.
linearSearch2D()to search the array for a value.
rightShift2D()to right shift the array. RIGHT shift means move every element one position to the RIGHT; the last element becomes the first one, and the last element in each row moves down to become the first element in the next row.
Each one of the functions above accepts a 2D array as a parameter, along with any additional parameters that you may find necessary. The return types of the functions are also your choice. Do NOT use global (i.e. file scope) variables in this program.
Sample Interaction
Lab3 ------
1 - Fill the array
2 - Search the array
3 - Right shift the array
0 - QUIT
Please enter a selection: 1
Enter min number in the array: 2
Enter max number in the array: 20
4 17 10
9 8 19
11 18 8
5 19 7
Lab3 ------
1 - Fill the array
2 - Search the array
3 - Right shift the array
0 - QUIT
Please enter a selection: 2
Please enter a value to search for: 19
Value 19 was found at (2,3).
Value 19 was found at (4,2).
Lab3 ------
1 - Fill the array
2 - Search the array
3 - Right shift the array
0 - QUIT
Please enter a selection: 2
Please enter a value to search for: 21
Value 21 was not found.
Lab3 ------
1 - Fill the array
2 - Search the array
3 - Right shift the array
0 - QUIT
Please enter a selection: 3
7 4 17
10 9 8
19 11 18
8 5 19
Lab3 ------
1 - Fill the array
2 - Search the array
3 - Right shift the array
0 - QUIT
Please enter a selection: 0
Goodbye!
// C program to perform operations of a 2D array
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define M 4
#define N 3
// function declaration
void printArray2D(int array[M][N]);
void populateRandom2D(int array[M][N], int min, int max);
void linearSearch2D(int array[M][N], int key);
void rightShift2D(int array[M][N]);
int main(void) {
srand(time(0));
int array[M][N];
int min, max, key, choice;
// loop that continues till the user doesn't
quit
do
{
// display the menu options
printf("\n1 - Fill the
array");
printf("\n2 - Search the
array");
printf("\n3 - Right shift the
array");
printf("\n0 - QUIT");
printf("\nPlease enter a selection:
");
fflush(stdout);
scanf("%d",&choice);
switch(choice)
{
case 1: // populate the array
// input of
minimum number
printf("Enter
min number in the array: ");
fflush(stdout);
scanf("%d",&min);
// input of
maximum number
printf("Enter
max number in the array: ");
fflush(stdout);
scanf("%d",&max);
populateRandom2D(array,min,max);
printArray2D(array);
break;
case 2: // search for a
value
// input of key
value to search
printf("Please
enter a value to search for:");
fflush(stdout);
scanf("%d",&key);
linearSearch2D(array,key);
break;
case 3: // right shift the
array
rightShift2D(array);
printArray2D(array);
break;
case 0: // quit
break;
default: // invalid choice
printf("\nWrong
choice");
}
}while(choice != 0);
printf("Goodbye!");
return EXIT_SUCCESS;
}
// function to print the 2D array
void printArray2D(int array[M][N])
{
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
printf("%d
",array[i][j]);
printf("\n");
}
}
// function to populate the 2D array with random values between
[min,max)
void populateRandom2D(int array[M][N], int min, int max)
{
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
array[i][j] =
rand()%(max-min)+min;
}
}
// function to search for a key value in the array
void linearSearch2D(int array[M][N], int key)
{
int found = 0;
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
if(array[i][j]
== key)
{
printf("Value %d was found at
(%d,%d)\n",key,i,j);
found = 1;
}
}
if(found == 0)
printf("Value %d was not
found.\n",key);
}
// function to right shift the 2D array
void rightShift2D(int array[M][N])
{
int i,j,temp,temp2;
temp = array[0][0];
// loop over the rows
for(i=0;i<M;i++)
{
// loop over the columns
for(j=0;j<N;j++)
{
// check if this
element is last element of row
if((j+1) ==
N)
{
if((i+1) == M) // check if this element is last
element of row
array[0][0] = temp; // move
this element to first row first column
else
{
// move this element to next
row's first element
temp2 = array[i+1][0];
array[i+1][0] = temp;
temp = temp2;
}
}else
{
// move this element to next column
temp2 = array[i][j+1];
array[i][j+1] = temp;
temp = temp2;
}
}
}
}
//end of program
Output: