The jist of the code is that a user is prompted to input a max_x, max_y value, number of points and total files to create. From here a file is created, the code for what is inputted within the file works correctly.
This is what I am trying to achieve:
Assume the user inputs are:
Generating random instances ...
Enter the circuit board size MAX_X MAX_Y: 100 200
Enter the number of points NUM_PT: 10
Enter the number of random instances to be generated: 7
Where I am stuck is that the program won't iterate 7 times over and instead only creates 1 file with the name instance7_007.txt
code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[]) {
FILE *fp;
int max_x, max_y, num_pt, random_inst;
int MAX_LEN = 200;
int *x_coordinate, *y_coordinate;
int inputfile = 0, outputfile = 0;
int i;
char name[MAX_LEN];
if (argc == 1) {
/* to generate random instances, accepting parameters from stdin */
printf("Generating random instance . . .\n");
printf("Enter the circuit board size MAX_X MAX_Y: ");
scanf("%d%d", &max_x, &max_y);
printf("Enter the number of points NUM_PT: ");
scanf("%d", &num_pt);
printf("Enter the number of random instances to be generated: ");
scanf("%d", &random_inst);
for (i=1; i < random_inst; i++);
{
snprintf(name, MAX_LEN, "instance%d_%03d.txt", num_pt, i);
fp = fopen(name, "w");
printf("%s generated", &name);
fprintf(fp, "#%s\n", argv[inputfile]);
fprintf(fp, "area [0, MAX_X] x [0, MAX_Y]\n");
fprintf(fp, "%d\t%d\n", max_x, max_y);
fprintf(fp, "%d\n", num_pt);
fprintf(fp, "#coordinates\n");
for (i=0; i < num_pt; i++)
{
fprintf(fp, "%d\t%d\n", rand()%max_x, rand()%max_y);
}
fprintf(fp, "#end of instance\n");
fclose(fp);
}
return 1;
}
}
The reason why your program iterates only once is because you overwrite you looping variable in the inner loop. In both the outer and inner loop you use the same variable that has a scope of main
- i
. One fix is to add another looping variable (i.e. int i, j
initially) and change the inner loop to:
for (j=0; j < num_pt; j++)
{
fprintf(fp, "%d\t%d\n", rand()%max_x, rand()%max_y);
}
Alternatively you can reduce the scope of i
s to their respective loops by declaring them in the loop statement,
for (int i=1; i < random_inst; i++)
and
for (int i=0; i < num_pt; i++)
This approach is actually useful and lets you avoid certain bugs.
Another problem is as @user501138 pointed out, your outer loop has an ;
which causes the loop body to be empty. Also, this line
printf("%s generated", name);
is incorrect in the original code as it has &name
instead of name
which represents a pointer to array of char arrays rather than a pointer to char array. I recommend you enable compiler warnings, as both of these things were pointed out by my compiler.
Finally, there is no need for argc==1
check, at least not one obvious to me - argc
will always be at least 1 - argv
will always have one argument - the name of the program you're running. You can of course keep it there, with the above changes your program works as intended.