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 :D :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

Recommended Answers

All 6 Replies

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:

for(<initial>; <condition>; <action>)
{
    // loop body here
}
<initial>;
while(<condition>)
{
    // loop body here

    <action>;
}

Expanding loops this way and then going through each step by hand should help.

--sg

also it could be

do
{
// action
}
while( condition )

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:

#include <stdio.h>

void main(void)
{
	printf("before loop\n");

	for(int i = 4; i < 2; i++)
	{
		printf("    argh! in de loop!\n");
	}

	printf("after loop\n");
}

: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

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.

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 so 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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.