473,320 Members | 2,164 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,320 software developers and data experts.

nested for loop problem

Would someone be able to tell me why this isn't working. The nested
for loops seem correctly coded to me. I would appreciate any input.

#include <iostream.h>
#include <math.h>

int main ()
{
int a, b, c, d;

cout << "Enter two integers ";
cin >a >b;

for (c=a; c==b; c++)
{
for(d=2; d<=sqrt(c); d++)
{
if(c%d==0)
continue;

else cout << c;
}
}
cout << " are the prime numbers in the range " << a << " to " << b <<
endl;

return 0;
}
Jun 27 '08 #1
9 1857
notahipee wrote:
Would someone be able to tell me why this isn't working. The nested
for loops seem correctly coded to me. I would appreciate any input.

#include <iostream.h>
#include <math.h>

int main ()
{
int a, b, c, d;

cout << "Enter two integers ";
cin >a >b;

for (c=a; c==b; c++)
I believe you wanted 'c<b' or 'c!=b'.
{
for(d=2; d<=sqrt(c); d++)
{
if(c%d==0)
continue;

else cout << c;
}
}
cout << " are the prime numbers in the range " << a << " to " << b <<
endl;

return 0;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
notahipee wrote:
Would someone be able to tell me why this isn't working. The nested
Please read the FAQ 5.8
(http://www.parashift.com/c++-faq-lit....html#faq-5.8). Be a
bit more specific as to "this isn't working". What are your inputs?
What is your output? What is your *expected* output?

Note that from a cursory examination of the code, I could figure out
what "isn't working" means, but still...
for loops seem correctly coded to me. I would appreciate any input.

#include <iostream.h>
#include <math.h>

int main ()
{
int a, b, c, d;

cout << "Enter two integers ";
cin >a >b;

for (c=a; c==b; c++)
are you sure this is what you want? The loop will only execute as long
as the second statement is true. So if a and b are different, well...
nothing happens (which I assume is your complaint).
{
for(d=2; d<=sqrt(c); d++)
{
if(c%d==0)
continue;

else cout << c;
}
}
cout << " are the prime numbers in the range " << a << " to " << b <<
endl;

return 0;
}
Jun 27 '08 #3
On May 5, 1:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
I believe you wanted 'c<b' or 'c!=b'.
Additionally, you should be using the standard C++ headers:

#include <iostream>
#include <cmath>

using namespace std;

or qualify:
std::cin
std::cout
etc.

Jun 27 '08 #4
On May 5, 11:50*am, red floyd <no.s...@here.dudewrote:
notahipee wrote:
Would someone be able to tell me why this isn't working. The nested

Please read the FAQ 5.8
(http://www.parashift.com/c++-faq-lit....html#faq-5.8). *Be a
bit more specific as to "this isn't working". *What are your inputs?
What is your output? *What is your *expected* output?

Note that from a cursory examination of the code, I could figure out
what "isn't working" means, but still...
for loops seem correctly coded to me. I would appreciate any input.
#include <iostream.h>
#include <math.h>
int main ()
{
* *int a, b, c, d;
* *cout << "Enter two integers ";
* * * * * *cin >a >b;
* *for (c=a; c==b; c++)

are you sure this is what you want? *The loop will only execute as long
as the second statement is true. *So if a and b are different, well...
nothing happens (which I assume is your complaint).
* *{
* * * * * *for(d=2; d<=sqrt(c); d++)
* * * * * *{
* * * * * * * * * *if(c%d==0)
* * * * * * * * * * * * * *continue;
* * * * * * * * * *else cout << c;
* * * * * *}
* *}
* *cout << " are the prime numbers in the range " << a << " to " << b <<
endl;
* *return 0;
}- Hide quoted text -

- Show quoted text -
I am trying to get the first loop to cycle from integer a to integer
b.
The second loop is supposed to test each integer for prime. The output
I intend is to print out only the primes. My opening cout and closing
cout work but the loops produced nothing.
Jun 27 '08 #5
On May 5, 11:53*am, Christopher <cp...@austin.rr.comwrote:
On May 5, 1:47 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
I believe you wanted 'c<b' or 'c!=b'.

Additionally, you should be using the standard C++ headers:

#include <iostream>
#include <cmath>

using namespace std;

or qualify:
std::cin
std::cout
etc.
I tried c<=b and it works. However, it is couting duplicate,
triplicate, etc. in stead of just once each.
Jun 27 '08 #6
I tried c<=b and it works. However, it is couting duplicate,
triplicate, etc. in stead of just once each.
For example if I enter 12 and 79 for a and b.
output reads 131314151617171718191919202121.....
Jun 27 '08 #7
On May 5, 12:05*pm, notahipee <werldpe...@hotmail.comwrote:
I tried c<=b and it works. However, it is couting duplicate,
triplicate, etc. in stead of just once each.

For example if I enter 12 and 79 for a and b.
output reads 131314151617171718191919202121.....
Oh never mind I just read the FAQ. Thank you for your help.
Jun 27 '08 #8
On May 5, 2:42*pm, notahipee <werldpe...@hotmail.comwrote:
Would someone be able to tell me why this isn't working. The nested
for loops seem correctly coded to me. I would appreciate any input.
[snip]

There are way better algorithms. Without even trying:

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

That has pseudo code, and a gif animation of the process.
Socks
Jun 27 '08 #9
MiB
On 5 Mai, 20:42, notahipee <werldpe...@hotmail.comwrote:
Would someone be able to tell me why this isn't working. The nested
for loops seem correctly coded to me. I would appreciate any input.
The algorithm you use for generating the primes is very inefficient.
However, bug fixing your code with minimal changes only, try this one.
It still misidentifies 0 and 1 as prime numbers (enter 0 as start
value...).

#include <iostream>

int main ()
{
unsigned int a, b;

std::cout << "Enter two integers ";
std::cin >a >b;

for ( unsigned int c = a; c <= b; c++ ) {
bool isprime = true;
for( unsigned int d = 2; d * d <= c; d++ ) {
if ( c % d == 0 ) {
isprime = false;
break;
}
}
if ( isprime ) std::cout << c << ' ';
}
std::cout << "are the prime numbers in the range " << a << " to " <<
b << std::endl;

return 0;
}
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: mark | last post by:
My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater to work just fine, as long as it's not...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
2
by: mark | last post by:
(not sure if this is the correct group) My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
5
by: Candace | last post by:
I have to write a program to find all Pythagorean triples for a right triangle. I know that the squares of two sides of the triangle must equal the square of the third (longest) side. However, I...
2
by: th3dude | last post by:
I am trying to pull out some nested XML using C# and XMLReader. Can't seem to extract the "Items" for each "Product" when i loop through file, i can loop over the "Product" notes just fine but...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.