Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help fixing this code in C programming, my current code just generates a fixed array with fixed values. The code should allow the

I need help fixing this code in C programming, my current code just generates a fixed array with fixed values. The code should allow the user to input/scan the row size and column size (as long as the rows and columns aren't over 20, So like the #define col and #define row should be 20, but when I try it it messes up.) then populate the array with random numbers between 1-99. Please help fix it, everything else is fine

#include #include

#define COL 3 #define ROW 3

void PopulateArray2DUnique(int A[][COL], unsigned int rowsize, unsigned int colsize, int min, int max) { int i, j; for (i = 0; i < rowsize; i++) for (j = 0; j < colsize; j++) A[i][j] = min + rand() % (max - min + 1);

}

void DisplayArray(int A[][COL], unsigned int rowsize, unsigned int colsize) { int i, j; for (i = 0; i < rowsize; i++) { for (j = 0; j < colsize; j++) { printf("%d ", A[i][j]); } printf(" "); } }

int FindLargest(int A[][COL], unsigned int rowsize, unsigned int colsize) { int max = 0; for (int i = 0; i < rowsize; i++) { for (int j = 0; j < colsize; j++) { if (A[i][j] > max) max = A[i][j]; } } return max; }

int FindColumnSum(int A[][COL], unsigned int rowsize, unsigned int colsize, unsigned int col_to_sum) { int Col_sum = 0; for (int i = 0; i < rowsize; i++) { // for (int j= col_to_sum; j < colsize; j++) { Col_sum += A[i][col_to_sum]; } } return Col_sum; }

int Sort2DArray(int A[][COL], unsigned int rowsize, unsigned int colsize, unsigned int Order) { int n = colsize; int temp[n * n]; int k = 0; if (Order == 1) { for (int i = 0; i < rowsize; i++) { for (int j = 0; j < colsize; j++) { for (int k = 0; k < rowsize; k++) { for (int p = 0; p < colsize; p++) { if (A[i][j] < A[k][p]) // key point { int t = A[i][j]; A[i][j] = A[k][p]; A[k][p] = t; }

} } } } } else { for (int i = 0; i < rowsize; i++) { for (int j = 0; j < colsize; j++) { int tmp = A[i][j]; int l = j + 1; for (k = i; k < rowsize; k++) { while (l < colsize) { if (tmp < A[k][l]) { tmp = A[k][l]; A[k][l] = A[i][j]; A[i][j] = tmp; } l++; } l = 0; } } }

} }

void CopyArray2D(int A[][COL], int B[][COL], unsigned int rowsize, unsigned int colsize) { for (int i = 0; i < rowsize; i++) { for (int j = 0; j < colsize; j++) { B[i][j] = A[i][j]; } } }

int CopyArray2DSpiral(int A[][COL], int B[][COL], unsigned int rowsize, unsigned int colsize) { int i, k = 0, l = 0; while (k < rowsize && l < colsize) { /* Print the first row from the remaining rows */ for (i = l; i < colsize; ++i) { B[l][colsize] = A[k][i]; } k++;

/* Print the last column from the remaining columns */ for (i = k; i < rowsize; ++i) { B[k][rowsize] = A[i][colsize - 1]; } colsize--;

/* Print the last row from the remaining rows */ if (k < rowsize) { for (i = colsize - 1; i >= l; --i) { B[colsize - 1][l] = A[rowsize - 1][i]; } rowsize--; }

/* Print the first column from the remaining columns */ if (l < colsize) { for (i = rowsize - 1; i >= k; --i) { B[rowsize - 1][k] = A[i][l]; } l++; } } }

int main() { int array[ROW][COL], ArrayB[ROW][COL]; PopulateArray2DUnique(array, ROW, COL, 10, 20); printf("The matrix is: "); DisplayArray(array, ROW, COL); int largest = FindLargest(array, ROW, COL); printf(" Largest Element is: %d ", largest); int sum = FindColumnSum(array, ROW, COL, 1); printf(" Sum of the column is: %d ", sum); CopyArray2D(array, ArrayB, ROW, COL); Sort2DArray(array, ROW, COL, 1); printf(" Displaying after sorting in ascending order: "); DisplayArray(array, ROW, COL); Sort2DArray(array, ROW, COL, 2); printf(" Displaying after sorting in Descending order: "); DisplayArray(array, ROW, COL); CopyArray2DSpiral(array, ArrayB, ROW, COL); printf(" New Matrix after 2D Spiral Copy: "); DisplayArray(ArrayB, ROW, COL); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain the current account view of the ER determination.

Answered: 1 week ago

Question

e. What difficulties did they encounter?

Answered: 1 week ago