Lab – 3 Arrays – dealing with 2D arrays using C programming
60-141 â Introduction to Programming II
Objectives: Practice dealing with 2D arrays.
Pre-requisite(s):
– Read and review chapter 6.
Manipulating a 2D array:
1. Create a two dimensional array (e.g. int A2D [ M ][ N ] đ of size M x N to store integer values. Use #define M 6 and N 5 to start. (Using symbolic constants instead of hard coding the array sizes improves scalability).
2. Populate the array pseudo-randomly, with integer numbers between 1 and M x N, inclusive, so that every array element is unique (no duplicates).
3. Print the array in a table format (use formatting codes to achieve this).
4. Use Linear Search to find if a number n is found in the array, where n is an integer between 1 and M x N (inclusive) entered by the user.
5. Apply a single LEFT shift operation to the array. LEFT shift means move every element one position to the LEFT; the first element becomes the last one, and the first element in each row moves up to become the last element in the previous row.
Example: Left shift of a 2 x 4 array: becomes:
4 8 3 2 8 3 2 5
5 6 1 7 6 1 7 4
6. Print the shifted array.
Summary of the lab requirements: You must 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, left shift the array, print the array and either repeat a menu item or quit.
Design and document the following functions (REQUIRED):
– PrintArray2D() — to print the array.
– PopulateRandom2D() — to populate the array with pseudo-random numbers.
– LinearSearch2D() — to search the array for a value, return true if found.
– LeftShift2D() — to left shift the array.
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.
Important Note: When passing 2D arrays as parameters to functions, it is necessary to specify the number of columns explicitly. Hence: int foo( int A[][N] ); is valid and permits references to all elements within A, such as A[i][j] for 0<=i and 0<=j
/*
Title: Lab #3: Arrays
Objective: This Program is with choices to fill the array with random numbers, search the array, left shift the array,
print the array and either repeat a menu item or quit.
*/
//Includes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Definitions
//Number of rows of the array
#define M 6
//Number of columns of the array
#define N 5
//Function prototypes
void PopulateRandom2D(int[][N]);//Function to populate a random array
void LinearSearch2D(int[][N]);//Function for linear search
void LeftShift2D(int [][N]);//Function to shift the array to the left
void PrintArray2D(int[][N]);//Function to print the array on the screen
//main function
void main(){
int a, b, select;
int loop = 1;
int sort[M][N];
//Interactive menu for choosing which function to use
while (loop){
//menu to make a selection
printf("Please select a function\n"
"1 - PopulateRandom2D\n"
"2 - LinearSearch2D\n"
"3 - LeftShift2D\n"
"4 - PrintArray2D\n"
"0 - Quit\n");
//Store the selected value by the user
scanf("%d", &select);
//Run the appropriate function based on the user's selection
switch (select){
case 0: loop = 0;
break;
case 1: PopulateRandom2D(sort);
break;
case 2: LinearSearch2D(sort);
break;
case 3: LeftShift2D(sort);
break;
case 4: PrintArray2D(sort);
break;
default: printf("Invalid selection, please choose a number between 1 and 4, or enter 0 to Quit\n");
}
}
printf("Thank you for using this program , please come back again\n");
}
/*
Objective: To Print an array on the screen
Input: Integer array
Output: Printed array on the screen
*/
void PrintArray2D(int sort[][N]){
//Declare variables
int a, b;
//To Print an array
for (a = 0; a < M; a++){
for (b = 0; b < N; b++)
printf("%02d ", sort[a][b]);
printf("\n");
}
printf("\n");
}
/*
Objective: Changes values in array to random numbers from 1 to 100
Input: Array of integers
Output: Array with random values from 1 to 100
*/
void PopulateRandom2D(int sort[][N]){
//Declare variables
int a, b;
//To get different random numbers
srand(time(NULL));
//Puts random numbers from 1 to 100 in array
for (a = 0; a < M; a++)
for (b = 0; b < N; b++)
sort[a][b] = rand() % 100 + 1;
PrintArray2D(sort);
}
/*
Objective: Searches array for a certain number
Input: Integer array
Output: Whether or not number is in array
*/
void LinearSearch2D(int sort[][N]){
//Declare variables
int a, b, n;
int found = 0;
printf("What number from 1 to 100 would you like to look for? ");
scanf("%d", &n);
//Search for input number
for (a = 0; a < M; a++)
for (b = 0; b < N; b++)
if (sort[a][b] == n)
found = 1;
//Whether number is found or not, say so
if (found)
printf("%d is found in array\n", n);
else
printf("%d is not found in array\n", n);
printf("\n");
}
/*
Objective: Shifts all number in array to the left
Input: Array of integers
Output: Array of integers with all numbers moved one space left
*/
void LeftShift2D(int sort[][N]){
int a, b;
int new[M][N];
//Puts sorted array into new array with everything shifted left
for (a = 0; a < M; a++){
for (b = 0; b < N; b++){
if (b == N-1){
if (a == M-1)
new[a][b] = sort[0][0];
else
new[a][b] = sort[a+1][0];
}
else{
new[a][b] = sort[a][b+1];
}
}
}
//Puts everything back into first array for consistency in main
for (a = 0; a < M; a++)
for (b = 0; b < N; b++)
sort[a][b] = new[a][b];
PrintArray2D(sort);
}
Leave a reply