Don't recompile the C program for every invocation!
Instead, pass the size as a command-line argument and dynamically allocate the memory.
In the bash script, if you wanted a dimension of 100 you would call the program like this:
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_DIMENSION 1000
void use_a(int *a, int n)
{
for (int i = 0; i < n; ++i) a[i] = i;
for (int i = 0; i < n; ++i) printf("%d ", a[i]);
putchar('\n');
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s DIMENSION\n", argv[0]);
exit(EXIT_FAILURE);
}
int n = atoi(argv[1]);
if (n < 1 || n > MAX_DIMENSION)
{
fprintf(stderr, "Error: bad dimension: %d\n", n);
exit(EXIT_FAILURE);
}
int *a = malloc(n * sizeof *a);
if (!a)
{
perror("Error: malloc");
exit(EXIT_FAILURE);
}
use_a(a, n);
free(a);
return 0;
}
Example bash script:
Code:
#!/bin/bash
N=(5 10 15 20)
for n in ${N[*]}; do
./myprog $n
done
Output:
Code:
0 1 2 3 4
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19