WEEK 5 ASSIGNMENT 5 QUESTION 02 SOLUTION
QUESTION :
The matrix M is divided into four quadrants by halving the rows and columns. If row/column is an odd number, divide them in such a way that the first half of the row/column should be one smaller than the second half.
The four quadrants are numbered from 1 to 4 in the structure shown below: Q1 | Q2
---+---
Q3 | Q4
INPUT FORMAT: M is a matrix of integers. You would be given two numbers m and n specifying the number of rows and columns. This would be followed by m lines of n integers each specifying the data of the m*n matrix M.
n and m will be greater than 1 and less than 12. OUTPUT FORMAT: Print in a separate line, the quadrant number with the smallest minimum-element.
SOLUTION :
#include<stdio.h>
int smallel(int *ptr, int len);
int main()
{
int m, n, p, q, x, y, i,k, j, index;
int M[100][100];
int quad1[10], quad2[10], quad3[10], quad4[10];
int s[4], small;
scanf("%d %d", &m, &n);
M[m][n];
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &M[i][j]);
}
}
// Finding the quadrants -->
// 1 quadrant.
k=0;
p=0;
for(i=0; i<m/2; i++)
{
for(j=0; j<n/2; j++)
{
quad1[k] = M[i][j];
k++;
p++;
}
}
// 2nd quadrant.
k=0;
q=0;
for(i=0; i<m/2; i++)
{
for(j=n/2; j<n; j++)
{
quad2[k] = M[i][j];
k++;
q++;
}
}
// 3rd quadrant.
k=0;
x = 0;
for(i=m/2; i<m; i++)
{
for(j=0; j<n/2; j++)
{
quad3[k] = M[i][j];
k++;
x++;
}
}
// 4 quadrant.
k=0;
y = 0;
for(i=m/2; i<m; i++)
{
for(j=n/2; j<n; j++)
{
quad4[k] = M[i][j];
k++;
y++;
}
}
// Find the small element in every quadrant.
s[0] = smallel(quad1, p);
s[1] = smallel(quad2, q);
s[2] = smallel(quad3, x);
s[3] = smallel(quad4, y);
// Find the small element in s array.
small = s[0];
index = 1;
for(i=0; i<4; i++)
{
if(small > s[i])
{
small = s[i];
index = i+1;
}
}
printf("%d", index);
return 0;
}
int smallel(int *ptr, int len)
{
int small = *ptr;
for(int i=0; i<len; i++)
{
if(small > *(ptr+i))
{
small = *(ptr+i);
}
}
return small;
}
< iCodebuzz />
No comments:
Post a Comment