Jumping into C++ problems

This is a discussion on Jumping into C++ problems within the C++ Programming forums, part of the General Programming Boards category; Hi, I have written the code of the question "Design a program that finds all numbers from 1 to 1000 ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    Jumping into C++ problems

    Hi, I have written the code of the question "Design a program that finds all numbers from 1 to 1000 whose prime factors, when addedtogether, sum up to a prime number (for example, 12 has prime factors of 2, 2, and 3, which
    sum to 7, which is prime). Implement the code for that algorithm."
    the code is
    Code:
    
    
    #include <iostream>
    using namespace std;
    
    
    bool isdivisible(int z,int i)
    {
        return z%i==0;
    }
    bool (isprime (int z))
    {
      for (int h=2;h<z;h++)
      {
    
    
    
    
      if (isdivisible(z,h))
        {
    
    
          return false;
    
    
        }
      }
    
    
    
    
        return true;
    
    
    }
    
    
    
    
    
    
    int main()
    {
    
    
        for ( int k = 2;k<10; k++)
    {
        int i=2;
        int y=0;
        int x;
        x=k;
    
    
            while(i<=x)
        {
            if (x%i==0)
            {
               x=x/i;
               y=y+i;
    
    
            }
            else
            {i++;}
         }
    
    
        int z;
        z=x+y-1;
    
    
        if (isprime(z))
        {
            cout<<k<<endl;
        }
    
    
        }
    }
    the problem is that the console window cant display all the results. It omits the 1st few result. How can I increse the space so that it can display all the results?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    I gave you a link that describes how to write to a file in this thread: A simple C++ problem

    Read it.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by Shakti View Post
    I gave you a link that describes how to write to a file in this thread: A simple C++ problem

    Read it.
    I have done what it says like this, nothing happens,

    Code:
    #include <fstream>
    #include <iostream>
    
    
    using namespace std;
    
    
    bool isdivisible(int z,int i)
    {
        return z%i==0;
    }
    bool (isprime (int z))
    {
      for (int h=2;h<z;h++)
      {
    
    
    
    
      if (isdivisible(z,h))
        {
    
    
          return false;
    
    
        }
      }
    
    
    
    
        return true;
    
    
    }
    
    
    
    
    
    
    int main()
    {
    
    
        for ( int k = 2;k<10; k++)
    {
        int i=2;
        int y=0;
        int x;
        x=k;
    
    
            while(i<=x)
        {
            if (x%i==0)
            {
               x=x/i;
               y=y+i;
    
    
            }
            else
            {i++;}
         }
    
    
        int z;
        z=x+y-1;
    
    
        if (isprime(z))
        {   char str[10];
            ofstream file ("example");
            file<<z;
            file.close();
        }
    
    
        }
    }

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    Quote Originally Posted by Tamim Ad Dari View Post
    the problem is that the console window cant display all the results. It omits the 1st few result. How can I increse the space so that it can display all the results?
    You can pipe the output to a pager, like "more" or "less" as in:
    $program | more
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    110
    I think the problem lies in your algorithm,,,the console window is capable of displaying thousands of values,..let me check it out

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,331
    Quote Originally Posted by Tamim Ad Dari View Post
    I have done what it says like this, nothing happens,
    Did you read the link in post #2 thoroughly?

    You should open/create the file before the main loop, and close the file after the main loop. As of now, the file is opened/closed each time "if(isprime(z))" is true. And, according to the link, "The default mode for opening a file with ofstream's constructor is to create it if it does not exist, or delete everything in it if something does exist in it."

    Also, create the file name with an extension that's easy to view (i.e. "example.txt").

  7. #7
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    110
    The algorithm is perfect.
    Maybe you can avoid writing every number to on a new line,,...instead you could just add a space between numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping into C++
    By Tamim Ad Dari in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2013, 07:29 PM
  2. Jumping into C++
    By Tamim Ad Dari in forum C++ Programming
    Replies: 9
    Last Post: 01-11-2013, 08:45 AM
  3. Jumping into C++ problems
    By Tamim Ad Dari in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2013, 08:04 AM
  4. character jumping
    By colourfulbanana in forum Game Programming
    Replies: 2
    Last Post: 06-30-2012, 05:37 PM
  5. Jumping to OOP in C++ from C
    By meadhikari in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2010, 05:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21