How do you come up with the Big O notation?
Code:float sum = 0 ; for ( int i = 1; i < n ; i++) { sum + = A[i]; } cout << sum;
How do you come up with the Big O notation?
Code:float sum = 0 ; for ( int i = 1; i < n ; i++) { sum + = A[i]; } cout << sum;
This is kind of the simplest problem in this field so I can't give you much help without simply giving the answer. Here are some questions to think about:
How long does the operation inside the loop ('sum += A[i];') take? (Is it constant time or non-constant time)
How many times do you think the loop will run?
If something takes 10 seconds and I do it 20 times then my total time is 200 seconds. The ever-improving speed of computers means that measuring in any unit of actual time is pretty useless though. We measure in growth rates instead. For every extra element of the array we have, how much longer will our program take to run?