-
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!
-
Well one problem is that you're stepping off the end of your array.
-
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.
-
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
-
It is not possible to help more without you giving more information about the assignment.
-
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.