The only problem I am having then is fixing up my for loop. I can't figure out how to set it up based off of the feedback I have received. I'm trying to think of ways t rework it, but I can't seem to figure it out.
The only problem I am having then is fixing up my for loop. I can't figure out how to set it up based off of the feedback I have received. I'm trying to think of ways t rework it, but I can't seem to figure it out.
Are you saying that I should be assigning, in my case, i the value of 2 in the for loop? Reading. Or am I completely missing what you are trying to say?Code:for (int i = 2; i <= num; i++)
The result of the loop will be 3, and that is also what i am getting as my answer
Ooops, sorry, I was referring back to my original loop that I had first written. You are correct about the loop that I had just given.
I am trying to work on the closed form approach, which from what I have found you are just assigning the formula to the variable. For my code, I have,but when I try and enter in a value like 10^7 I get 0 seconds. That is also the same when I try 10^10 as the value for n. Shouldn't I actually get a time or no?Code:start = clock(); int formula = (n*(n+1))/2; end = clock();
Last edited by newbieprogram; 03-21-2014 at 12:03 PM.
It's too fast. You need to repeat it in a loop a number of times.
Your formula takes around 5 instructions, which on a 2 GHz processor is about 10 nanoseconds, excluding time for reading and writing from memory/cache.
If I repeat it in a loop, is that still using the closed form approach?
Ok, after putting the closed form approach inside of a for loop, i am now getting a result. I really do appreciate all of the help you have provided. Sorry if I was being a pain or anything like that.