Thread: A simple C++ problem

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

    A simple C++ problem

    Hi, I am trying to write a program that will print out the factors of a number user inputted and than print the sum of the factors. I came up with this
    Code:
    #include <iostream>using namespace std;
    
    
    int main()
    {   int x;
        cin>>x;
        int i=2;
        int y=0;
        while(i<x)
        {
            if (x%i==0)
            {
                cout<<i<<"\n";
                x=x/i;
                y=y+i;
                cout<<y<<"\n";
            }
            else
            {i++;}
         }
        cout<<x;
        int z;
        z=x+y;
        cout<<z;
    
    
    }
    , everything works fine except the last output is way much bigger than the actual sum,, where am I wrong??

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You have forgotten the newline after cout<<x in line 21.
    There is endl; that acts like the newline character.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by std10093 View Post
    You have forgotten the newline after cout<<x in line 21.
    There is endl; that acts like the newline character.
    thanx ,that work.Actually I dont understand the function of the expression "endl;"

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    cout<<"output"<<"\n";
    is equivalent and more appropriate in cpp to be written as this
    Code:
    cout<<"output"<<endl;
    endl is, as cout, part of the namespace std.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    Code:
    cout<<"output"<<"\n";
    is equivalent and more appropriate in cpp to be written as this
    Code:
    cout<<"output"<<endl;
    endl is, as cout, part of the namespace std.
    That is not true: std::endl also flushes the output stream whereas printing a newline character does not necessarily do so.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You had two couts with no endlines.

    As I said in post number 4, endl works as newline (and as laserlight said, it flushes the buffer).

    So, you had two couts. One was giving a number X and the other a number Y, but because nothing was between X and Y, in the screen you would see XY.
    Now, with the endl (newline), you see this
    Code:
    X
    Y
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    And here is the proof that laserlight was right.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    sorry to bother, but I have modified my program so that it displays the sum of the factors of all the numbers upto 1000. here it is acting really weird!
    Code:
    #include <iostream>using namespace std;
    
    
    int main()
    {
    
    
        for ( int k = 2; k < 1000; 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;
        cout<<z<<endl;
    
    
        }
    }
    what is the wrong here??

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What do you mean? Please be more specific. It runned fine for me.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by std10093 View Post
    What do you mean? Please be more specific. It runned fine for me.
    Well, I want this program to print the sum of factors of all numbers from 2 to 1000. such as,for k=2 it is2,k=3 it is 3,k=4 the factors are 2 and 2 and sum is 4,k=5 it is 5,k=12 factors are 3,2,2 and sum is 7. like this upto k=1000. the program surely didnt print these values.rather it prints 23,55..... which makes no sense!

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You said that once you find the prime factorization of one number, you would add all of them to another number which is the answer.I told you in a previous thread that this would only work if you divide by prime numbers. Well to do that you need to have a prime number test, the simplest of which is trial division: repeatedly divide the number you think is prime by smaller integers, and if you can find a number that divides without a remainder, then the number is composite. Or, you can make a list of primes. This is absolutely required if you want to do this right.

    Once you know enough primes, you can start dividing to see which ones are in your numbers. The sum of the prime factors of 42 is 13, every factor appears only once. The sum of the prime factors of 16 is 8, though, so watch out for primes that appear more than once.

    If you don't know enough math to do them I would suggest you steer clear of the Project Euler site.

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by whiteflags View Post
    You said that once you find the prime factorization of one number, you would add all of them to another number which is the answer.I told you in a previous thread that this would only work if you divide by prime numbers. Well to do that you need to have a prime number test, the simplest of which is trial division: repeatedly divide the number you think is prime by smaller integers, and if you can find a number that divides without a remainder, then the number is composite. Or, you can make a list of primes. This is absolutely required if you want to do this right.

    Once you know enough primes, you can start dividing to see which ones are in your numbers. The sum of the prime factors of 42 is 13, every factor appears only once. The sum of the prime factors of 16 is 8, though, so watch out for primes that appear more than once.

    If you don't know enough math to do them I would suggest you steer clear of the Project Euler site.
    I certainly know how to find the prime factors of a number.In fact that is the way I used in the program where user gives an input and sum of the prime factors are outputted. there is no problem in that fraction. problem is, when I want to find the 'prime factor sum' of the all numbers from upto 1000 the output is different. the problem is in the "for loop" I have created later and I dont know what is causing the problem. So far I see, it should work fine...

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I certainly know how to find the prime factors of a number.In fact that is the way I used in the program where user gives an input and sum of the prime factors are outputted. there is no problem in that fraction.
    I disagree with you completely. As written, the computer will use any divisor, not just the primes.

    Once again, if you make sure to divide by primes, the only other thing you need to do is process each number in turn. Add together each number's prime factors, using the same variable.

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You have an error when computing the y factor. When you find an i that x is evenly dividable by you add this to y, but this is wrong. You need to multiply y with i and set your new y to this.

    Also y should be 1 at the start.

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by Shakti View Post
    You have an error when computing the y factor. When you find an i that x is evenly dividable by you add this to y, but this is wrong. You need to multiply y with i and set your new y to this.

    Also y should be 1 at the start.
    I have done as you said,but yet it outs 75,62 which does not make any sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  3. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  4. A simple problem?
    By Volair in forum C Programming
    Replies: 3
    Last Post: 11-19-2006, 03:14 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM