I want to allow the user of my program to load a dataset from a text file. The data will be stored in an array of doubles.
I also want the user to specify how many datasets they wish to load into the program in total and then define the corresponding number of arrays.
My problem is that I don't know how many arrays will be needed in advance (or their size). Does anyone know how to declare an unknown/arbitrary number of variables?
Here is a simplified example of what I want to achieve, where I've used integers instead of arrays. Obviously I want to declare a distinct integer each time inside the loop.
Code:#include <stdio.h> int main() { int no_of_integers, i; char buffer[100]; printf("Enter the number of integers you want to input\n"); fgets(buffer, sizeof(buffer), stdin); sscanf(buffer, "%d", &no_of_integers); for (i = 1; i <= no_of_integers; i++){ int integer_i; printf("Enter integer %d\n", i); fgets(buffer, sizeof(buffer), stdin); sscanf(buffer, "%d", &integer_i); } }
(I'm actually not using arrays, but instead matrices as explained in Numerical Recipes in C 2nd Edition, Section 1.2. Although this doesn't change my problem)



LinkBack URL
About LinkBacks


