Me and my friends are trying to understand an assignment about time complexity in c++. Can someone help us to understand which one of us is thinking correctly?
The assignment is this:
int doing(int n)
{
int k = 0;
int result = 0;
while (k<n*n)
{
for (int i = 10; i<20; i++)
result +=n;
k++;
}
return result;
}
We have to decide O(n) and T(n) for this function.
I have come up with the results:
#include <iostream> // provides: cout, cin
#include <iomanip> // provides: setw
#include <limits>
// setting up the environment
usingnamespace std;
int count = 0; // This counts the number of iterations
int doing(int n)
{
int k = 0;
int result = 0;
while (k<n*n)
{
for (int i = 10; i<20; i++)
{
count++;
result +=n;
}
k++;
}
return result;
}
int main()
{
doing(100);
cout << count;
return 0;
}