Still working through my primer book, it's asking me to make a program that takes input to fill a 3x5 array with values and then perform various tasks on those values. Right now I'm stuck at filling it. Basically I can't figure out the proper way either to pass the pointer to the array through the function or the proper way to use scanf inside the function. It seems to take the values, and it prints the array fine, but the value never make it into the outside array, they seem to only get stored in the temporary arr. Thanks for your help
I've tried passing it as double *arr, double arr[rows][cols], double arr[][5], and a few other things I'm just not sure where I'm going wrong in the setup. Without functions this would be easy, but the book is asking for function based (understandably).
Code:
#include <stdio.h>
void copy_arr(int rows, int cols, double * arr[3][5]);
void copy_prnt(int rows, int cols, double tgt[rows][cols]);
int main(void)
{
double orig[3][5];
copy_arr(3, 5, orig);
copy_prnt(3, 5, orig);
return 0;
}
void copy_arr(int rows, int cols, double * arr[3][5])
{
int i;
for (i = 0; i < rows; i++)
{
printf("Enter five values:\n");
scanf("%f %f %f %f %f", &arr[i][0], &arr[i][1], &arr[i][2], &arr[i][3], &arr[i][4]);
}
}
void copy_prnt(int rows, int cols, double tgt[rows][cols])
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
printf("%.2f ", tgt[i][j]);
printf("\n");
}
}