Could someone please help with this C program? 3.Print Matrix Trying to have a 2
ID: 3807183 • Letter: C
Question
Could someone please help with this C program?
3.Print Matrix
Trying to have a 2D array parameter in a function in C isn't as simple as in Java. Like with single
dimensional arrays, it's common to actually make the parameter a pointer to a pointer type
rather than an actual array type. Create a function to print a 2D array of characters. Write a
program that asks the user for a height and width and dynamically allocates a 2D array of those
dimensions. Fill the array with dots and then randomly put asterisks in some elements. For the
number of asterisks, it should be 10% of the width times the height cast to an int (for example,
10 * 10 * 0.1 = 10). Then, use the print function on the array. Here's the function header to get
you started:
void print_matrix(char ** matrix, int height, int width)
Sample Run:
Enter Height: 10
Enter Width: 10
Asterisk Count: 10
array (0x7f939d600000):
. . . * . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . * . . . . . .
. . . * . . . * . .
. . . . . . . . . .
. * . . . * . . . .
. . . . . . . * . *
. . * . . . . * . .
. . . . . . . . . .
Explanation / Answer
#include<stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
clrscr();
printf(“Enter the number of rows and columns of Array(2D) ”);
scanf(“%d%d”, &m, &n);
printf(“Enter the elements of first Array ”);
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second Array ");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of Arrays:- ");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d ", sum[c][d]);
}
printf(" ");
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.