| | |
Please help with algorithm
![]() |
•
•
Join Date: Jun 2004
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: May 2004
Posts: 82
Reputation:
Solved Threads: 4
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:
Expanding loops this way and then going through each step by hand should help.
--sg
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:
C Syntax (Toggle Plain Text)
for(<initial>; <condition>; <action>) { // loop body here }
C Syntax (Toggle Plain Text)
<initial>; while(<condition>) { // loop body here <action>; }
--sg
•
•
Join Date: May 2004
Posts: 82
Reputation:
Solved Threads: 4
•
•
•
•
Originally Posted by marceta
also it could be
do
{
// action
}
while( condition )
Example--the second printf statement will never be reached. A clever compiler might toss up a warning:
C Syntax (Toggle Plain Text)
#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"); }
•
•
Join Date: May 2004
Posts: 82
Reputation:
Solved Threads: 4
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.
If you want responses that are helpful, take the time to make sure what you've posted is what you're really asking about.
•
•
Join Date: Jun 2004
Posts: 3
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Help with LR parser algorithm (Java)
- round robin algorithm (Computer Science)
- Round Robin Algorithm Simulation (C++)
- Eigenface Algorithm (Computer Science)
- Algorithm for checking contiguous blocks (C)
Other Threads in the C Forum
- Previous Thread: Return multidimension array
- Next Thread: sorting an array of string
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





