473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How can I calculate the complexity of a c++ code?

108 100+
Calculate the complexity of the following C++ code fragment: [3]
count = 0;
for (i = 1; i <= n; i *= 2)
for (j = 1; j <= i; j++)
2
count++;
Note: You are required to show all your workings.
May 8 '10 #1
5 6141
weaknessforcats
9,208 Expert Mod 8TB
Start here http://en.wikipedia.org/wiki/Cyclomatic_complexity.
May 8 '10 #2
phpuser123
108 100+
I managed to get the answer


Outer loop executes log n times.
Inner loop has cost k which doubles each time.


This is a clue,some1 gave me but I can't understand how he arrived to log n for the outer loop.......
Can any1 give me any indication hw,log n ws obtained ???

Thanks
May 8 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
Read this: http://en.wikipedia.org/wiki/Big_O_notation.
May 9 '10 #4
donbock
2,426 Expert 2GB
There are many measures of complexity. Which one are you being asked to provide?
May 10 '10 #5
jkmyoung
2,057 Expert 2GB
Aside: Did you calculate the final answer?
1 + 2 + 4 + 8 + ... + 2^m = 2^(m+1) - 1
May 10 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Xenophobe | last post by:
I have successfully converted the ASP code included in the following article to PHP: http://www.4guysfromrolla.com/webtech/040100-1.shtml As described the high and low latitudes and longitudes...
28
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates:...
5
by: junky_fellow | last post by:
How do we calculate the complexity of an algorithm? Am i right if i say the complexity of an algorithm is the number of comparisons done in that algorithm? thanx in advance .......
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
2
by: codergem | last post by:
Hi, I dont know how to calculate time complexities of a recursive function. Can anyone explain me with some thorough and also by showing step by step. Or do any of you know abour a good link or...
26
by: Lionel B | last post by:
Hi, Anyone know if the Standard has anything to say about the time complexity of size() for std::set? I need to access a set's size (/not/ to know if it is empty!) heavily during an algorithm...
1
by: jchimanzi | last post by:
How can i rewrite the following code correcting all problems found void calculatevalues (const int &a, float *b, int &c) { d = 20.4; if (*a =0) *a += 1
36
by: ashu | last post by:
there is question : What is the smallest value of n such that an algorithm whose running time is 100n2 runs faster than an algorithm whose running time is 2n on the same machine. i can`t...
7
by: humsafar | last post by:
#include<iostream> #include<stdlib.h> using namespace std; int main(){ int i, j, n; for(i=0;i<n; i++){ for(j=0; j<n; j++){ cout<<"my time complexity is =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.