Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

College year :2023/2024

1st Mathematics and Computer Science


Module: Algorithmics and Data structures 2

Exercises sheet N°7 – PW

Exercise n° 1 :
int main() {
int N, M;
Consider a matrix of integers A(N, M) printf("Enter the number of rows (N <= 50): ");
(N<=50,M<=100) • Write a PA that calculates scanf("%d", &N);
the sum of the elements of a line i. • Write a printf("Enter the number of columns (M <= 100): ");
PA that calculates the product of the elements scanf("%d", &M);
of a column j.
• Using the previous actions, write a program int A[MAX_ROWS][MAX_COLS];
which for a given matrix A displays the sum
of the elements of each row and the product printf("Enter the elements of the matrix A:\n");
of the elements of each column. for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
Solution : scanf("%d", &A[i][j]);
#include <stdio.h> }
}
#define MAX_ROWS 50
#define MAX_COLS 100 printf("Sum of elements of each row:\n");
for (int i = 0; i < N; i++) {
int rowSum(int A[MAX_ROWS][MAX_COLS], int printf("Row %d: %d\n", i+1, rowSum(A, N, M,
N, int M, int i) { i));
int sum = 0; }
for (int j = 0; j < M; j++) {
sum += A[i][j]; printf("Product of elements of each column:\n");
} for (int j = 0; j < M; j++) {
return sum; printf("Column %d: %lld\n", j+1,
} columnProduct(A, N, M, j));
}
long long columnProduct(int
A[MAX_ROWS][MAX_COLS], int N, int M, int j) { return 0;
long long product = 1; }
for (int i = 0; i < N; i++) {
product *= A[i][j];
}
return product;
}

A.Y

You might also like