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;
}