943,648 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3511
  • C RSS
Jun 2nd, 2004
0

Please help with algorithm

Expand Post »
I am struggling to derive answers for this problem.
I need detail explanation. I understand the problem has to do with manipulating and trying values to derive what is n but don't understand :rolleyes:

Considering the following nested loops,

for(j = 5; j<=n; j++)
{
j =n;
while(k > 2)
{
<body>
}
}

a. what is the number of iterations of the while loop (n>6)? show your work
b. what is the number of iterations of the while loop during the third iteration of the for loop? Show your work
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fasteddie19793 is offline Offline
3 posts
since Jun 2004
Jun 3rd, 2004
0

Re: Please help with algorithm

Is that code complete/correct? The two questions don't make much sense. Both ask how many times the while loop iterates, but what is k supposed to be? And look at the for loop closely--how many times will that loop really run? (only two answers to that, depending on what n is and assuming the body of the while loop doesn't change the value of j)

You can try different values for n, but it's more of a tool for understanding how the loop works--there isn't really any "correct" value.

On the chance you haven't done this yet, you can replace any for loop with an equivalent while loop. Both of these loops accomplish the same thing:
  1. for(<initial>; <condition>; <action>)
  2. {
  3. // loop body here
  4. }
  1. <initial>;
  2. while(<condition>)
  3. {
  4. // loop body here
  5.  
  6. <action>;
  7. }
Expanding loops this way and then going through each step by hand should help.

--sg
Reputation Points: 182
Solved Threads: 71
Posting Pro in Training
gusano79 is offline Offline
475 posts
since May 2004
Jun 4th, 2004
0

Re: Please help with algorithm

also it could be

do
{
// action
}
while( condition )
Reputation Points: 13
Solved Threads: 0
Posting Whiz in Training
marceta is offline Offline
217 posts
since May 2004
Jun 7th, 2004
0

Re: Please help with algorithm

Quote originally posted by marceta ...
also it could be

do
{
// action
}
while( condition )
As long as the condition is true the first time it is tested, you're right--the loop behavior is the same. But a for loop always tests the condition before entering the loop body; the above loop tests the condition after, which guarantees that the code inside the loop will run at least once. In contrast, if the condition is false the first time around, a for loop will never execute the loop body.

Example--the second printf statement will never be reached. A clever compiler might toss up a warning:
  1. #include <stdio.h>
  2.  
  3. void main(void)
  4. {
  5. printf("before loop\n");
  6.  
  7. for(int i = 4; i < 2; i++)
  8. {
  9. printf(" argh! in de loop!\n");
  10. }
  11.  
  12. printf("after loop\n");
  13. }
Reputation Points: 182
Solved Threads: 71
Posting Pro in Training
gusano79 is offline Offline
475 posts
since May 2004
Jun 8th, 2004
0

Re: Please help with algorithm

:eek: The answer is this the while loop executes
n -2 times and the for loop executes n -4 times
together they execute (n-2) times (n-4) this is from my algorithm class
to find a for loop you use this formula
(condition - initial value) + 1
(n -5) +1 = n -4 times for the for loopundefinedundefined
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fasteddie19793 is offline Offline
3 posts
since Jun 2004
Jun 10th, 2004
0

Re: Please help with algorithm

The for loop without any body will execute (n - 2) times, but the first line in your loop above is j = n; which, as written, alters the value of j and thus jacks around with the loop's normal execution. It's clear now that it was mistyped and should have been k = n; though that doesn't quite fix the while loop. As long as you don't specify the loop and just put <body> instead, there's no way to tell what happens to k. As you wrote it, that while loop will never terminate (assuming k > 2) because the value of k never changes. A line like k = k - 1; in the loop would get it to run (n - 2) times.

If you want responses that are helpful, take the time to make sure what you've posted is what you're really asking about.
Reputation Points: 182
Solved Threads: 71
Posting Pro in Training
gusano79 is offline Offline
475 posts
since May 2004
Jun 16th, 2004
0

Re: Please help with algorithm

Well my professor had a typo for few of the questions

for(j =5; j <=n; j++)
{
k = n;
while(k>2)
{
<body of the loop>
k--;
}
}
Questions was
What is the number of iterations of the for loop (n>6)? Show your work.
Well, this question doesn't matter for while and we are only concerned with the for loop.
The for loop condition is <= which means we use this formula:
final value which is n minus initial value which is 5 + 1.
The answer is n - 4
But if the condition was just < (less than) then we would first take one less then n which is n -1 - initial + 1

The while loop follows this since k is greater then 2
we can put values in for it
let just say we say k is 5
5 >2 we have 2 values left that is 1 and 2 because 2 is not included because not equal.

so we say the while loop is n -2

correct answers according to my professor Namipiour co-wrote Introduction to The Theory of Algorithms
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fasteddie19793 is offline Offline
3 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Return multidimension array
Next Thread in C Forum Timeline: New Type





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC