Untitled

 avatar
unknown
plain_text
4 years ago
1.4 kB
2
Indexable
#include <stdio.h>
#include <stdlib.h>

int countOdd(int *matrix, int row, int column);

int main(void)
{
    int stu_id = 70357003;

    int mat[100][100];
    int r, c, element;
    printf("Please input the size of row: ");
    scanf("%d", &r);
    printf("Please input the size of column: ");
    scanf("%d", &c);

    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            printf("Please enter the value for Matrix[%d][%d]: ", i+1, j+1);
            scanf("%d", &element);
            mat[i][j] = element;
        }
    }

    // Print Matrix
    printf("Matrix in 2D:\n");
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            printf("%d ", mat[i][j]);
            if(j == c-1){
                printf("\n");
            }
        }
    }

    // Count odd elements
    int odd_num;
    odd_num = countOdd(&mat, r, c);

    printf("The number of odd number in the matrix is: %d", odd_num);

    printf("\n\nStudent ID: %d\n\n", stu_id);
    return 0;
}

int countOdd(int *matrix, int row, int column){
    int odd = 0, count = 0;
    for(int i = 0; i < row; i++){
        for(int j = 0; j < column; j++){
            if(*(matrix + count) %2 != 0){
                odd++;
            }
            count++;
        }
    }

    return odd;
}
Editor is loading...