C language 2D array sum of first n elements

Viewed 121

I'm trying to get the sum of n elements of the pr[a][a] matrix.

What my code does is create 2 2D arrays, ar is for sorting the elements in it (see this as an excel board) and pr is for the "price" attributed to that specific place.

What I need to do, is to sum the first N elements of that matrix. I'm quite troubled about what should I change to my suma() function, to be more precise, I have no idea how to do this correctly.

I do need to use pointers.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int ar[100][100], pr[100][100];
void create_mat(int a)
{
    srand(time(NULL));
    int count = 1;
    for (int i = 0; i < a; i++)
    {
        printf("\n");
        for (int j = 0; j < a; j++)
        {
            ar[i][j] = count;
            count++;
            pr[i][j] = (rand() % 500) + 100;
            printf("%d\t", ar[i][j]);
            printf("%d\t", pr[i][j]);
        }
    }
}

void suma(int *sum, size_t n, int a, int arp[][n])
{
    *sum = 0;
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < a; j++)
        {
            *sum += arp[i][j];
        }
    }
}
int main()
{
    int sum;
    int a, n;
    int (*create_ptr)(int);
    create_ptr = &create_mat;
    printf("Nr of rows and columns: ");
    scanf("%d", &a);
    printf("\nNr of product and its price:");
    create_ptr(a);
    printf("\nFor how many products do you want to sum the price: ");
    scanf("%d", &n);
    suma(&sum, n, a, pr);
    printf("Sum:%d", sum);
    return 0;
}
2 Answers

I assume you are using function pointers to practise so I'll leave that be.

As for the issue at hand, you already pass the number of values to be summed through your n variable so you just need to make sure only n number of sums are performed, for instance:

void suma(int *sum, size_t n, int a, int arp[][100]) // arp[][n], why?
{
    *sum = 0;
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < a; j++)
        {
            *sum += arp[i][j];
            n--;
            // when n reaches 0 the cycle ends and the function returns
            if(n == 0){
                return;
            }
        }
    }
}

Live demo


Note that in create_ptr = &create_mat; there is a type mismatch, one is void* the other is int*, you should match them.

Since you don't return any value from the function, declaring:

void (*create_ptr)(int);

Is the recommended fix.


Footnotes:

There are issues of code correctness that should be addressed, namely:

  • Value checks, making sure the value of a does not allow for out-of-bounds access errors i.e. it should be larger than 0 and smaller than 100, n should also be larger than 0 and smaller than, or equal to, the total number of elements in the array.

  • Checking the return values of scanf, consider doing this with fgets to parse the input and strtol or sscanf to convert it to the desired type.

  • Another thing to note is that you should not declare the arrays as global variables unless it's unavoidable.

I don't know if you've already know, but your suma is just adding the members of the first line! I dont know much about the function you used: create_ptr = &create_mat; create_ptr(a);, but I think you should create the matrix (2D Array) in the main function and then change the values and print them in create_mat() using pointers. Then you can send that matrix to the suma().

Related