An error

I've wrote a code which converts denary number in another scale of notation.
Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  //---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <math.h>

#include "Unitperevod.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CCALENDR"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------






void __fastcall TForm1::Button1Click(TObject *Sender)
{
float a;
long d[100000], counter=0, counterm=0,e, b, c;
a=StrToFloat (LabeledEdit1->Text);
b=StrToFloat (LabeledEdit2->Text);
c=a;
while(c>=b)
{
d[counter]=c%b;
c=floor(c/b);
counter++;
}
while(e>=counter)
{
LabeledEdit3->Text=LabeledEdit3->Text+FloatToStr (d[counterm]);
e++;
counterm++;
}
}
//--------------------------------------------------------------------------- 

But this code doesn't work! It prints nothing, but I wanted elements of array d[counterm] . Help me to fix it!

Last edited on
Your code contains essentially:
1
2
3
4
5
6
7
8
long e; // uninitialized
long counter = 42; // initialized

while ( counter <= e )
{
  ++e;
  std::cout << "Do you feel lucky?\n";
}

We have no idea what the value of 'e' is initially, and thus the loop condition on the first time is either true or false. You have been (un)lucky to get false there.

If the condition is true, then incrementing 'e' does keep it true for quite long. As by-product, the counterm grows too and you would dereference the array 'd' out-of-range.


Why do you have both e and counterm?
I thought your 'while' condition will be true 42 times. 'e' and 'counterm' aren't both, but I have 'counter' and 'counterm'. 'e' is initially equals 0.
Last edited on
I've fixed the problem like this:
 
while(e<=counter)

but I have another problem. Does C++ have function that gives the array elements backwards?
Last edited on
'e' is initially equals 0.

The 'e' is initially 0 only if you explicitly set it 0. Your code did not do that.

Does C++ have function that gives the array elements backwards?

I don't think that you actually want to "get reverse array". You can iterate a range "backwards".

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
// A
int e = counter;
while ( 0 < e ) {
  --e;
  std::cout << e << '\n';
}

// B
int e = 1;
while ( e <= counter ) {
  std::cout << (counter - e) << '\n';
  ++e;
}
Last edited on
I don't think that you actually want to "get reverse array". You can iterate a range "backwards".

I want this:
We've got an array with elements:1612. So, I need the code which does this with the array:2161.
Last edited on
Do you want:

*) To show the elements of the array in reverse order.

Or

*) Create a new array and copy elements in reverse order to it (and then show the new array in "normal" order).

Or

*) Change the order of the elements in the array (and then show the array in "normal" order).
Last edited on
I want to:
To show the elements of the array in reverse order.
My latest code example has two loops. What do those print out?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
    const int n = sizeof(array) / sizeof( array[0] ) ;

    // normal order
    for( int i = 0 ; i < n ; ++i ) std::cout << array[i] ;
    std::cout << '\n' ;

    // reverse order
    for( int i = 0 ; i < n ; ++i ) std::cout << array[ (n-1) - i ] ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/7225588c80a278e1
My latest code example has two loops. What do those print out?
If counter equals 10, it prints "9876543210"
If counter equals 10, it prints "9876543210"

You did miss the newline. Lets set counter=13 for more impact:
12
11
10
9
8
7
6
5
4
3
2
1
0

One does use index to access an element of the array. What are the indices of an array that have 13 elements? Integer values from 0 to 12. In reverse order that means values from 12 to 0.

The code by JLBorges produces the reverse indices in a third way (and accesses elements from an array too).


@JLBorges:
IMHO, the contents of your example array can cause confusion, because they happen to be the same digits as their array positions.
I need first way.
You did miss the newline.
Ah, yeah, I've forgot about '/n'!
Last edited on
The @JLBorges's code isn't working.
Please explain the "isn't working".
Please explain the "isn't working".
It doesn't print the reverse 'd'!
Last edited on
The program of JLBorges does print its array in both directions in my system. It does not contain 'd' though, so it is best that you show your current code.
So, what it means? I haven't got any working code for me?
Oh no, I need hex symbols. How to attach them?
Last edited on
Topic archived. No new replies allowed.