Function getColumn

Function getColumnThe function/method getColumn accepts four arguments – RCmatrix and K. The integer R represents the number of rows in the matrix, the integer C represents the number of columns in the matrix, a two dimensional array of integers matrix and the integer K represents the position of a column in the matrix.

The function/method getColumn must return an array of integers containing all the integers in the Kth column of the given matrix.

Your task is to implement the function getColumn so that it passes all the test cases.

The following structure is used to represent the boundedArray and is already defined in the default code (Do not write this definition again in your code).

typedef struct BoundedArray
{
    int SIZE;
    int *arr;
} boundedArray;

IMPORTANT: Do not write the main() function as it is already defined.

Example Input/Output 1:
Input:
5 4
98 62 65 28
37 47 14 89
19 66 48 43
84 16 75 35
62 88 77 94
3

Output:
Column 3: 65 14 48 75 77

Explanation:
Here K = 3, the integers in the 3rd column are 65144875 and 77.
Hence the output is
Column 3: 65 14 48 75 77

Example Input/Output 2:
Input:
4 7
87 51 76 54 16 32 63
51 86 57 48 91 32 93
44 32 69 22 38 49 94
63 40 23 70 88 13 87
2

Output:
Column 2: 51 86 32 40

#include <stdio.h>
#include <stdlib.h>
typedef struct BoundedArray
{
    int SIZE;
    int *arr;
} boundedArray;
boundedArray* getColumn(int R, int C, int matrix[][C], int K)
{
    typedef boundedArray *p;
    p ptr;
    ptr=malloc(sizeof(boundedArray));
    ptr->arr=malloc(R*sizeof(int));
    ptr->SIZE=0;
    for (int i=0;i<R;i++){
        for(int j=0;j<C;j++){
            if(j==K-1){
            ptr->arr[ptr->SIZE]=matrix[i][j];
            ptr->SIZE++;
            }
        }
    }
    return ptr;
}
int main()
{
    int R, C, K;
    scanf("%d %d", &R, &C);
    int matrix[R][C];
    for(int row = 0; row < R; row++)
    {
        for(int col = 0; col < C; col++)
        {
            scanf("%d", &matrix[row][col]);
        }
    }
    scanf("%d", &K);
    boundedArray *bArr = getColumn(R, C, matrix, K);
    printf("Column %d: ", K);
    for(int index = 0; index < bArr->SIZE; index++)
    {
        printf("%d ", bArr->arr[index]);
    }
    return 0;
}
Previous Article

Area of Circle

Next Article

Find Hundredth Digit

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *