How to write this C program pass by pointers and pass by value? I created this program that generates a 10x10
array from -100
to 100
and the main diagonal is read from bottom to top, but I do not know how to create two other programs that are pass by pointers and value
I have to write two more programs with the same input and output only through these two methods. I am a beginner in C and I do not know how to make this.
#include <stdio.h>
#include <stdlib.h>
#define N 10
#define M 10
int my_rand(int max, int min)
{
return (min + rand() / (RAND_MAX / (max - min + 1) + 1));
}
void generate_array(int(*array)[M])
{
printf("Table:");
for (int i = 0; i < N; i++)
{
printf("\n");
for (int j = 0; j < M; j++)
{
array[i][j] = my_rand(-100, 100);
printf("%4d ", array[i][j]);
}
}
}
void print_diagonal(int(*array)[M])
{
printf("\n\nThe main diagonal read from bottom to top:\n");
for (int i = N - 1; i >= 0; i--)
{
printf("%4d ", array[i][i]);
}
}
int *create_array(int(*array)[M])
{
static int new_array[M] = { 0 };
for (int i = 0; i < N; i++)
new_array[i] = array[i][i];
return new_array;
}
void reverseArray(int arr[], int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
void printArray(int arr[])
{
int i;
for (i = N; i < N; i++)
printf("%4d ", arr[i]);
printf("\n");
}
int main(void)
{
int array1[N][M] = { 0 }, *aux_array;
generate_array(array1);
print_diagonal(array1);
aux_array = create_array(array1);
reverseArray(aux_array, 0, N - 1);
printArray(aux_array);
}
Any help would be much appreciated.