Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By grumpy

C++ divisible by 10

This is a discussion on C++ divisible by 10 within the C++ Programming forums, part of the General Programming Boards category; can u help me? I have problem in my program a little bit only! . . . Code: #include <iostream> ...

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    C++ divisible by 10

    can u help me? I have problem in my program a little bit only! . . .

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    main ()
    {
         float num;
         cout<<"Enter #: ";
         cin>>num;
         
         float ans=num/10;
         float a[10];
         
         for(int i=0; i<=10; i++)
         {
                a[i]=ceil(ans*i);
                 
         }
         for(int x=10; x>=0; x--)
         {
                 cout<<a[x]<<endl;
         }
    
    system("pause");
    }
    if I enter 23 the output must be

    23
    21
    19
    17
    15
    13
    11
    9
    7
    5
    3

    but in my program its output is

    23
    21
    19
    17
    14
    12
    10
    7
    5
    3
    0

    can u help me to fix this i don't have an idea how to do it!
    thanks advance!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Well one problem is that you're stepping off the end of your array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5,855
    Indexing of arrays starts at zero. So your array a has elements a[i] through a[9]. In C++, it is illegal to be lazy and leave the return type off main().

    Your problem is one of expectations. To understand what happens, when i is 4, 2.3*i is 9.2. ceil(2.3*i) is 10, but you are expecting it to be 9.

    Without knowing why you expect that result to be 9 when it is not, nobody can help you. Similarly with other values that differ from your expectation.
    iMalc likes this.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    I'm done with that, but nothing change, its the same! , help me please!

    see the difference

    correct wrong
    23 23
    21 21
    19 19
    17 17
    15 14
    13 12
    11 10
    9 7
    7 5
    5 3
    3

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    It is not possible to help more without you giving more information about the assignment.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    5,855
    The code you have given is behaving as it should. Your expectations are wrong, but you have not deigned to explain why you expect the code to give different results than it does.


    This is yet another contender. The challenges are running thick and fast.
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with program number divisible by 10? using macros
    By MarjunC in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2010, 05:43 AM
  2. Divisible Program
    By Ericalvusa91 in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2010, 05:25 PM
  3. Displaying Numbers divisible by user's desired number
    By DaniiChris in forum C Programming
    Replies: 9
    Last Post: 07-07-2008, 02:06 PM
  4. Simple C program displaying all numbers divisible by 6
    By DaniiChris in forum C Programming
    Replies: 25
    Last Post: 07-06-2008, 12:25 PM
  5. Divisible by 2
    By guillermoh in forum C Programming
    Replies: 22
    Last Post: 03-12-2008, 10:19 AM

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