Here is what I have right now. I created a loop that loops through 6 times. Each time it loops, it spits out a different answer at the end, then when it gets back to the top it starts at 0 and {0,0,0}.


........

double U[6]={0,0,0,0,0,0};
double g[6][3]={{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}};


for (int s=0; s<6; s++)
{

.....................

}

What I'm trying to do is to make this loop run through 3000 times instead of 6. One problem I am having with this is that I don't want to go and write in 3000 0's and 3000 {0,0,0}'s.

Any help on an efficient method of doing this would be great.

Recommended Answers

All 4 Replies

You're in luck. If you don't initialize all of the items in your array in the initialization list, the ones you didn't specify will be set to the equivalent of 0 for that type:

double U[N]={0}; // Completely filled with 0
double g[N][M]={0}; // Completely filled with 0

if its not in a class look at memset(). That will set any range of bytes (if they are in your process) to whatever you want. For example, if you've got a thousand doubles and you want that memory all set to nulls, you would call the function with the starting address and the number of bytes you want filled - here 8000.

>if its not in a class look at memset()
Using memset is unsafe even for some built-in types, and if you don't know why, you should refrain from working with memory at such a low level. Consider these two statements for a variable declared as double d; :

d = 0; // Set d to the value of 0
memset ( d, 0, sizeof d ); // ???

The first statement is well-defined and correct. The second statement, which you probably believe to be equivalent, is neither well-defined nor correct because the internal representation of 0 for double is not required to be all bits zero (which is what this memset call does).

Try std::fill instead.

Sweet!! Yeah I just did it with out using memset(). It worked fine with just putting in one zero afterward.

Thanks a lot guys. This one is solved!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.