Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.4 kB
1
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

int main(void)
{
    int stuId = 70357001;
    printf("Student ID: %d\n\n",stuId);

    int row,column;
    printf("Enter the number of rows: ");
    scanf("%d",&row);
    printf("Enter the number of columns: ");
    scanf("%d",&column);
    int matrix[row][column];
    for (int i=0;i<row;i++)
    {
        for (int j=0;j<column;j++)
        {
            printf("Enter the element in Mat[%d][%d]: ",i+1,j+1);
            scanf("%d",&matrix[i][j]);
        }
    }
    int odd = countOdd(&matrix,row,column);
    putchar('\n');
    display(&matrix,row,column);
    putchar('\n');
    printf("The number of odd numbers in Mat A is %d\n",odd);
    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 == 1)odd++;
            count++;
        }
    }
    return odd;
}

void display (int *matrix, int row, int column)
{
    int count=0;
    for (int i=0;i<row;i++)
    {
        if (i==0)printf("Mat A =");
        else printf("       ");
        for (int j=0;j<column;j++)
        {
            printf("%3d",*(matrix + count));
            count++;
        }
        putchar('\n');
    }
}