Thread: Not sure why this failed

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    12

    Not sure why this failed

    Write a program which calculates the sum of natural numbers which are divisible by 3 and 5 less than the number given as input.

    for ex.
    1) If user input 10 then output will be 23.
    2) If user input 20 then output will be 78.

    so i figure 10 = 3,6,9,5
    20 = 3,6,9,12,15,18,5,10 (no duplicate 15)

    I failed. 0/3 Response: Your logic is incorrect !

    I fail to see the problem in my logic. Can you?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    
    {
       int max = 0, sum = 0;
       cout << "Enter the maximum No.:";
       cin >> max;
    
    /*
    Your Logic goes here
    */
    
    //three
    int three = max/3;
    // remove itself i.e 10 and 20 from the equation
    if (max%3 == 0)
        three--;
    
    for (int x=three;x>0;x--)
    {
        sum = sum + (3 * x);
    }
    
    //five
    int five = max/5;
    if (max%5 == 0)
        five--;
    
    for (int x=five;x>0;x--)
    {
        // only add to sum if not a duplicate (already present from three)
        // couldn't get the not equals to work so went with true
        if ((5*x)%3 == 0)
        {
    
        }
        else { sum = sum + (5 * x); }
            
    }
    
    cout<<"Total is:";
    cout << sum;
    
    return 0;
    }
    p.s. not sure why string included. I didn't need it.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you're submitting it to an automated testing system then you can't just print out anything you want. Maybe you need to remove the "Enter..." prompt and the "Total is" string.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It's possible you misunderstood the problem statement. From your example numbers:
    20 = 3,6,9,12,15,18,5,10 (no duplicate 15)
    What in the problem statement says that the sum can't include 2 fifteens or any other number? If you missed some terms in the sum and provided a wrong answer, well then, you have your explanation.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    12
    I don't think so. At least not in that respect. I have probably put you wrong by using the terminology "duplicate". It only wants the natural numbers. so you don't want to repeat a natural number. At least that's how I figure they got 78.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    What answers does you code give?

    Is there a logical reason you are multiplying by 3 or 5?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    12
    Quote Originally Posted by oogabooga View Post
    If you're submitting it to an automated testing system then you can't just print out anything you want. Maybe you need to remove the "Enter..." prompt and the "Total is" string.
    I'm hoping its something like that. Although this is the code they provide you with. I've tried returning sum, or omitting any output. no joy

    Code:
    #include <iostream> 
    #include <string> 
     
    using namespace std; 
     
    int main(int argc, char* argv[]) 
    { 
        int max=0,sum=0; 
        
        cout<<"Enter the maximum No.:"; 
        cin>>max; 
      
            /* 
             Your Logic goes here  
           */ 
     
       cout<<"Total is:"; 
     
    }

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If the examples you provided were given to you, then I suspect the wording in the problem is wrong.

    If we assume that it said "sum of natural numbers which are divisible by 3 or 5 less than the number given as input," this would be very easy to solve and would match the given examples.

    Try writing down the numbers 1 through 9. Cross off any that aren't divisible by either 3 or 5, and add up the numbers you have left. Do the same for numbers 1 - 19. I did this myself and got the results you provided in the given examples.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you do it this way
    Code:
    int test( int max ) {
        int sum = 0;
        for ( int x = 3; x < max; ++x ) {
            if ( 0 == x % 3 ) 
                sum += x;
            else if (  0 == x % 5 )
                sum += x;
        } 
        return sum;
    }
    It gives the exact same results then your code.
    If the results are wrong then it must be a missunderstanding of the problem
    Kurt

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I did some sleuthing. If this is the source, then the answer is supposed to be in Java.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by ZuK View Post
    If you do it this way...
    That's almost how I did it, except I logic "or"ed the two conditions to make it more concise. I believe, however, that you just handed the solution to the assignment to the OP.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Matticus View Post
    I believe, however, that you just handed the solution to the assignment to the OP.
    Did I ?. The op already had a solution that gives the same results.
    Kurt

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    12
    Quote Originally Posted by whiteflags View Post
    I did some sleuthing. If this is the source, then the answer is supposed to be in Java.
    that is the site and the question but you can navigate to the cpp side of things. code challenges -> cpp -> easy

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by garth View Post
    I'm hoping its something like that. Although this is the code they provide you with. I've tried returning sum, or omitting any output. no joy
    Okay. So that stuff needs to be there exactly like that.
    What about the lack of a newline after printing sum
    Code:
    cout << sum;
    Try
    Code:
    cout << sum << endl;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by garth View Post
    that is the site and the question but you can navigate to the cpp side of things. code challenges -> cpp -> easy
    I did it the way you said and I still end up at the Java challenge.

  15. #15
    Registered User
    Join Date
    Jul 2012
    Posts
    12
    Quote Originally Posted by whiteflags View Post
    I did it the way you said and I still end up at the Java challenge.
    yeah I had the same problem. you need to wait for the page to fully load. then change to cpp. then wait for it to fully load. refresh latency issue on the site.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NtSetSystemTime() failed
    By medp7060 in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2010, 02:58 AM
  2. execv failed :(
    By Andaluz in forum Linux Programming
    Replies: 6
    Last Post: 09-25-2009, 01:41 AM
  3. GetPrivateProfileString failed
    By vart in forum Windows Programming
    Replies: 3
    Last Post: 02-24-2008, 01:19 PM
  4. i tried myself for 2 hours but failed.
    By epidemic in forum C++ Programming
    Replies: 0
    Last Post: 04-02-2007, 05:46 PM
  5. Assertion Failed!
    By magic.mike in forum Windows Programming
    Replies: 3
    Last Post: 02-14-2005, 03:04 AM