10 Replies - 838 Views - Last Post: 22 July 2012 - 11:04 AM Rate Topic: -----

#1 jcmaster2   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Removal of break in loop...

Posted 22 July 2012 - 08:40 AM

I am going back through old code that finds emirps. It works but I want to remove the break below to give me the same answer. When I have tried to negate the condition. I get an infinite loop.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

bool isPrime(int);
int reversal(int);

int main()
{
  int count = 1;

  for (int i = 2; true; i++)
  {
    // Display each number in five positions
    if (isPrime(i) && isPrime(reversal(i)))
    {
      cout << setw(6) << i;

      if (count % 10 == 0)
        cout << endl;

      if (count == 100)
		  break;         //What I am trying to remove..

      count++; // Increase count
    }
  }

  cin.get();

  return 0;
}
bool isPrime(int num)
{
  if ((num == 1) || (num == 2))
  {
    return true;
  }

  for (int i = 2; i <= num / 2; i++)
  {
    if (num % i == 0)
      return false;
  }

  return true;
}

int reversal(int num)
{
  int result = 0;

  while (num != 0)
  {
    int lastDigit = num % 10;
    result = result * 10 + lastDigit;
    num = num / 10;
  }

  return result;
}











Some second opinion would be nice.

Is This A Good Question/Topic? 0
  • +

Replies To: Removal of break in loop...

#2 aresh   User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: Removal of break in loop...

Posted 22 July 2012 - 08:50 AM

Since you break the loop when count is equal to 100, change the loop to
for (int i = 2; count < 100; i++)


Was This Post Helpful? 0
  • +
  • -

#3 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Removal of break in loop...

Posted 22 July 2012 - 08:50 AM

You just need to change the condition inside the for-loop:
for (int i = 2; true; i++)

This loop is going to be an infinite loop because the condition always evaluates to true. Just put the condition that is in the if-statement with the break, inside the for-loop's condition.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Removal of break in loop...

Posted 22 July 2012 - 08:51 AM

Quote

It works but I want to remove the break below to give me the same answer.

Why? Do you think there is something inherently wrong with the break statement. Show what you have tried.

Why are you using a for loop? Your condition statement in this loop does indicate you want an endless loop (true). If you always want to end when count == 100 you could always use count for your loop instead of i.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Removal of break in loop...

Posted 22 July 2012 - 08:52 AM

View Postaresh, on 22 July 2012 - 11:50 AM, said:

Since you break the loop when count is equal to 100, change the loop to
for (int i = 2; count < 100; i++)


This obviously is a simple program, probably homework, but this isn't a code writing service. Just explain what the person needs to do, and try to let them figure it out. If they still have problems, ask them to post what they have tried. Don't just give the user code, and especially without any explanation, that's only helping them learn to copy & paste.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Removal of break in loop...

Posted 22 July 2012 - 08:54 AM

View Postvividexstance, on 22 July 2012 - 05:50 PM, said:

Just put the condition that is in the if-statement with the break, inside the for-loop's condition.


If he changes the loop condition to count == 100, which is the if's condition, the loop won't run at all.
Was This Post Helpful? 1
  • +
  • -

#7 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Removal of break in loop...

Posted 22 July 2012 - 09:03 AM

I should've been more clear, you're right my bad. Some would argue that you just need to change it to "does-not-equal". I would suggest a "less-than" though.

*EDIT*: Atleast I didn't just post the code giving the answer. Why + vote someone for breaking the rules???

This post has been edited by vividexstance: 22 July 2012 - 09:04 AM

Was This Post Helpful? 1
  • +
  • -

#8 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Removal of break in loop...

Posted 22 July 2012 - 09:14 AM

View Postvividexstance, on 22 July 2012 - 06:03 PM, said:

*EDIT*: Atleast I didn't just post the code giving the answer. Why + vote someone for breaking the rules???


Because I did not feel that writing 9 characters of code for someone was enough to warrant a downvote.
Was This Post Helpful? 0
  • +
  • -

#9 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Removal of break in loop...

Posted 22 July 2012 - 09:17 AM

So you pick and choose when it's OK for someone to not follow the rules? What are the rules then for? Can't you see how just giving someone the code, no matter how short it is, IS NOT helping them. By trying to let them do some of the work, they actually might learn something instead of just learning, "If I just go to DIC, someone will give me some code that works and I can just copy + paste it." Do you see how that's not helping?

In my opinion if you can't explain the OP's question in English then you really shouldn't be trying to answer that question.
Was This Post Helpful? -1
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Removal of break in loop...

Posted 22 July 2012 - 09:20 AM

If you two want to argue take it to PM or the Experts/Mentors forum.

But why or when someone decides up or down votes some topic is a personal matter and does not have to be explained to anyone.

Jim

This post has been edited by jimblumberg: 22 July 2012 - 09:24 AM

Was This Post Helpful? 1
  • +
  • -

#11 jcmaster2   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: Removal of break in loop...

Posted 22 July 2012 - 11:04 AM

I have been out of school.. let's see HS for 20 years and college 15.. so I can safely tell you this is not homework. I know I can leave the break in the code but I wanted to practice the removal of the break.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1