given a matrix of size m×n with values ranges from 100 to 150 . write a C program to generate
0-1 Matrix based on threshold value of 125 . If the matrix element is greater than the threshold set the element to 1 else set the element to 0. print the input Matrix and generate the 0-1 Matrix (use function with 2D array and threshold as arguments)
Sample input:
Input matrix: 120 117 136
110 150 128
135 114 149
Sample output:
0 0 1
0 1 1
1 0 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MX 55
int main()
{
int m, n;//size 2d array
printf("Please enter count rows and cols: (m,n)\n");
scanf("%i%i", &m, &n);
int a[MX][MX];//define matrix
//input matrix element
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
scanf("%i", &a[i][j]);
}
//Generate 0-1 array
int gen[MX][MX];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
if (a[i][j] > 125)//if greater than threshold
gen[i][j] = 1;
else
gen[i][j] = 0;
}
//out genenarted matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
printf("%3d", gen[i][j]);
printf("\n");
}
return 0;
}
Comments
Leave a comment