haha

hahaha
 avatar
unknown
csharp
a month ago
1.2 kB
6
Indexable
#include <stdio.h>
#include <math.h>

int sumAtRange(int rows, int cols, float arry[rows][cols], int start, int end) {
    int totalElements = rows * cols;
    float sum = 0.0;
    
    if (start < 0 || end >= totalElements || start > end) {
        return 0;
    }
    
    int index = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (index >= start && index <= end) {
                sum += arry[i][j];
            }
            index++;
        }
    }
    
    return (int)floor(sum);
}

int main(){
    int rows, cols, start, end;
    
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);
    
    float arry[rows][cols];
    
    printf("Enter the elements of the array:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%f", &arry[i][j]);
        }
    }
    
    printf("Enter start index: ");
    scanf("%d", &start);
    printf("Enter end index: ");
    scanf("%d", &end);
    
    int result = sumAtRange(rows, cols, arry, start, end);
    printf("Sum: %d\n", result);
    
    return 0;
}
Editor is loading...
Leave a Comment