hi i have a very large array that i want to populate but i don want to run the for loops every time so i thought i would right a program to output text to fill the array and was wondering if this would work.
the array is int array[100][12][31];

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <time.h>

using namespace std;

#define getrandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))

int main()
{
	int temp, counter;
	srand(time(NULL));
	ofstream fout("MyFile.txt");
	fout << "int array[100][12][31] =" << "\n" << "{";
	for (int i = 0; i < 100; i++)
	{
		for (int j = 0; j < 12; j++)
		{
			for (int k = 0; k < 31; k++)
			{
				if ((counter % 15 == 0) && (counter != 0))
					fout << "\n";					// after 15 numbers go to a new line in the file
				temp = getrandom(1000, 9999);		// used to get a number for the array
				fout << temp << ", ";				// seperate each element with a "," and a space
				counter++;
			}
		}
	}
	fout << "};";									// ends the array with "};"
	fout.close();
	return 0;
}

i know this is kinda rough but i was hoping it would help, im not anywhere near being done with this program and i may need to increase the array by a factor of 100 before the end so im not sure how much time it will take to populate the array every time. i do realize that i will have to go back into the .txt to delete the last", " or the compiler will flag an error. if anyone has any ideas or sugestions it would be very much aperciated

...and was wondering if this would work.

Just compile and run it, and if there are problems: post them (and in case of an unexpected result please tell also what output you did expect) :)

i have and it works just fine i was just wondering if this would be a good way to populate an array the you always want to have the same values but would take forever to write it out and and on a machine like mine takes about 10 seconds to process or longer if i need to make the array bigger

1. Populating the array in memory is going to be a lot quicker than reading it from a file. File I/O is orders of magnitude slower than memory.

2. If you want the same "random" data each time, then call srand() with a constant, and not the result of time.

Using these two ideas, the only thing you need to store in the file is the seed you used to create the data with in the first place.

Use binary output (write member function) to save/get an array. It takes less than 1 second for ~1 million array elements (and probably ~1 milliseconds to populate this array as Salem said).

i realize that i want to have the array to be the same but i just wanted a random starting number in srand() for the first initialization. the file that the array would be stored in would be a .cpp file and i would compile it into the program so upon startup it would be loaded into the memory. sorry if i didn't make that clear. i was just wondering if there was a point where having such a large for loop to populate the array would take longer then having a program write it out once into a file and then compiled into the program.

Populate the array + write it + compile it + read it
is ALWAYS going to take longer than just
Populate the array

thank you for the info salem. i just didn't know if having precompiled and loaded during startup was faster or slower during runtime but i guess i see your point because when you start the application it will have to pull all that information off the hard drive and then load it into memory. doing it during runtime allows it to just be created in the memory thus bypassing the file I/O time.

This question has already been answered. Start a new discussion instead.