Question

In: Computer Science

1COMP1410 - Lab Exercises #3 (Due at the end of the lab period or beginning of...

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!

Solutions

Expert Solution

// 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:


Related Solutions

In case the stock amounts at the beginning and end of the period are different, how...
In case the stock amounts at the beginning and end of the period are different, how are the profits calculated according to the variable and full cost method? Please explain
The beginning Common Stock balance is corrected due to an error in a previous accounting period...
The beginning Common Stock balance is corrected due to an error in a previous accounting period D. The beginning Retained Earnings balance is corrected due to an error in a previous accounting period When a company announces a​ 2-for-1 stock split ​(check all that​ apply) A. The market price of the stock is typically cut in half B. There is no effect on​ assets, liabilities, or total​ stockholders' equity C. A journal entry is recorded where​ paid-in capital is increased...
An adjusting entry is completed ________. a.at the beginning of the accounting period b.at the end...
An adjusting entry is completed ________. a.at the beginning of the accounting period b.at the end of the accounting period c.when the balance sheet is prepared d.when accounts need to be balanced in the ledger
Jaime owes Eddie $12,000 at the end of 3 years and $18,000 due  at the end of...
Jaime owes Eddie $12,000 at the end of 3 years and $18,000 due  at the end of 7 years with accumulated interest from today at 10% compounded quarterly. Jaime decided to settle his obligations by making a payment of $8,000 at the end of year 1, $5,000 at the end of year 5 and another payment at the end of year 9. if they agree that money is worth 14% compounded semi-annually, find the size of the payment at the end...
This lab assignment will correspond to Elasticities and Secondary Axis Graphs. There are two lab exercises...
This lab assignment will correspond to Elasticities and Secondary Axis Graphs. There are two lab exercises listed below. Please complete this assignment in one Excel workbook in separate sheets for each exercise. You may work with one other individual (no more than two people per team) or by yourself. In Blackboard, open Lab3_Workbook. This will become the Lab Set that you turn in. Exercise 1. Taco Del Mar has completed a study of weekly demand for its tacos in Washington...
Week 3: Supply and Demand: Price Elasticity Problem #2 Due Date: Due by the end of...
Week 3: Supply and Demand: Price Elasticity Problem #2 Due Date: Due by the end of Week 3 at 11:59 pm, ET. Price plays a significant role in quantity demanded and quantity supplied. Consumers and sellers respond differently to changes in the price of different types of goods and the situation. To measure the responsiveness of consumers and sellers to changes in the price of a good, some rules of thumb are given. These rules of thumb are said to...
An annuity pays 3 at the beginning of each 3 year period for 30 years. Find...
An annuity pays 3 at the beginning of each 3 year period for 30 years. Find the accumulated value of the annuity just after the final payment, using i (2) = 0.06.
Emman is indebted to Lisa the following obligations (a) $14,000 due at the end of 3...
Emman is indebted to Lisa the following obligations (a) $14,000 due at the end of 3 years with accumulated Interest at 4% compounded quarterly and (b) $16,000 due at the end of 6 years with accumulated Interest at 7% annually. Emman is allowed to replace these obligations by two payments. The first payment due at the end of 4 years and the second payment due at the end of 5 years. If the second payment is twice as much as...
Like all our lab exercises, the waves and sound lab will use a PhET simulator. Some...
Like all our lab exercises, the waves and sound lab will use a PhET simulator. Some of these simulators require you to enable Java or Flash. You can download and use the simulator on your desktop, or for some labs you can run it in your browser. Preferred browser settings and system requirements can be found at the “Running Sims” FAQ:  https://phet.colorado.edu/en/help-center/running-sims (Links to an external site.) For this lab, we’ll use the “Sound Waves” simulator: https://phet.colorado.edu/en/simulation/legacy/sound (Links to an external...
Lab Project 3: Lab Safety
Lab Project 3: Lab Safety 1. List the items of proper lab dress. 2. Describe a how you would use your cell phone in the microbiology lab. 3. List the safety equipment found in labs. 4. Identify the reason you would not use carbon dioxide extinguisher on a person 5. List personal habits that should be avoided in the lab.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT