Untitled

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

int * array2D(int r, int c);
void arrayPrint(int r, int c, int ptr);

int main(void)
{
    int stu_id = 70357003;

    int row, column;
    printf("Enter the size of row: ");
    scanf("%d", &row);
    printf("Enter the size of column: ");
    scanf("%d", &column);

    int *ptr, k = 0;
    ptr = array2D(row, column);
    arrayPrint(row, column, ptr);

//    for(int i = 0; i < row; i++){
//        for(int j = 0; j < column; j++){
//            printf("Array[%d][%d] = %d\n", i, j, *(ptr + i*100 + j));
//            k++;
//        }
//    }

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

int * array2D(int r, int c){
    static int matrix_2d[100][100];
    int element;
    printf("Please enter the elements row-by-row: \n");

    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            printf("Matrix [%d][%d]: ", i, j);
            scanf("%d", &element);
            matrix_2d[i][j] = element;
        }
    }
    return matrix_2d;
}

void arrayPrint(int r, int c, int ptr){
    for(int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            printf("Array[%d][%d] = %d\n", i, j, *(ptr + i*100 + j));
            if(j == c - 1){
                printf("\n");
            }
        }
    }
}