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

***NOTE: ALL IN C LANGUAGE Create a two dimensional array (e.g. int A2D [ M j[N]

ID: 3855355 • Letter: #

Question

***NOTE: ALL IN C LANGUAGE

Create a two dimensional array (e.g. int A2D [ M j[N];) of size M x N to store integer values. Use #de fine M 6 and N 5 to start. (Using symbolic constants instead of hard coding the array sizes improves scalability). 1. 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: 4 8 32 56 17 becomes: 8325 6 174 6. Print the shifted array.

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#define M 6

#define N 5

void PrintArray2D(int arr[M][N])

{

int i, j;

for (i = 0; i < M; i++)

{

for (j = 0; j < N; j++)

{

printf("%d ", arr[i][j]);

}

printf(" ");

}

}

void PopulateRandom2D(int arr[M][N])

{

int i,j;

for (i = 0; i < M; i++)

for (j = 0; j < N; j++)

//arr[i][j]=rand() % 100 + 1;

arr[i][j]=j + rand() / (RAND_MAX / (M*N - i) + 1);

}

int LinearSearch2D(int arr[M][N],int key)

{

int i,j,found;

for (i = 0; i < M; i++)

{

for (j = 0; j < N; j++)

{

if (key == arr[i][j] )

{

found = 1;

break;

}

}

}

if (found == 1)

return 1;

else

return 0;

}

void LeftShift2D(int arr[M][N])

{

int *f = (int *)arr;

int temp = f[0];

int i;

for (i = 1; i < M*N; ++i )

f[i-1] = f[i];

f[M*N-1] = temp;

}

int main()

{

int i, j,result,arr[M][N],key;

for (i=0; i<M; i++)

for(j=0;j<N;j++)

arr[i][j] = 0;

PopulateRandom2D(arr);

PrintArray2D(arr);

printf("Enter the element to be searched ");

scanf("%d", &key);

  

result=LinearSearch2D(arr,key);

if (result == 1)

printf("Element is present in the matrix ");

else

printf("Element is not present in the matrix ");

LeftShift2D(arr);

  

PrintArray2D(arr);

return 0;

}