decimal

Pages: 12
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>

using namespace std;


void decimal2Base(int num, int base, char *result, int offset = 0);
void decimal2Base(int num, int base, char * result, int offset)
{
   if (base > 36 || base < 2)
      result = "Non Valid";
   else
   {
      int digit = num % base;
      *result = (digit < 10 ? digit + '0' : digit - 10 + 'A');

      if (num >= base)
         decimal2Base(num / base, base, result + 1, offset + 1);
      else
      {
         result[1] = '\0';
         result -= offset;
         //get result's length
         int length = 0;
         for (char *ptr = result; *ptr; ptr++)
            length++;
         //copy result to a temporary string
         char *temp = new char[length + 1];
         for (char *ptr = result, *tempPtr = temp; *ptr; ptr++, tempPtr++)
            *tempPtr = *ptr;
         //copy back to result in reversed order
         for (int i = length - 1; i >= 0; i--)
            result[length - 1 - i] = temp[i];
         //free memory of the temporary string
         delete [] temp;
      }
   }
}
int main()
{

   int num;
   int base;
   char result[20];

   while (true)
   {
      cout << "Enter number: ";
      cin >> num;
      if (num < 0)
      cout << "Enter base: ";
      cin >> base;
      if (base > 36 || base < 2)

      decimal2Base(num, base, result);
      cout << result << endl;

      cout << endl;
   }

   return 0;
}


can anyone tell me what is that mean exactly every single line especially which is in bold?
any why we are using the star * too much while we is it part of the name or it is a pointer?
*result = (digit < 10 ? digit + '0' : digit - 10 + 'A'); what is that means?

we use offset for what?


The "star" can mean the following:

1) When declaring a variable, it indicates that the type of a variable is a pointer, e.g.

1
2
3
int *iPtr;    // iPtr is a pointer to an integer
char *name; // name is a pointer to a character
StructType *data; // data is a pointer to a StructType object 


The * is actually part of the type, not the name, so I've always found it clearer to write it this way:

1
2
3
int* iPtr;    // iPtr is a pointer to an integer
char* name; // name is a pointer to a character
StructType* data; // data is a pointer to a StructType object 


That means exactly the same thing, but it's clearer when reading it what the type of the variable is, and what its name is.

2) When used immediately before a pointer in an expression, it means "de-reference the pointer". In other words, it means that you're using the value in the memory that the pointer is pointing to, rather than the memory address stored in the pointer. So, for example:

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
int i = 2;  // This is an integer
int j = 3;  // This is an integer

int* iPtr;  // This is a pointer to an integer
int* jPtr;  // This is a pointer to an integer

iPtr = &i;   // iPtr stores the address of i, in other words it points to i
jPtr = &j;   // jPtr stores the address of j, in other words it points to j

// This will print out "2, 3"
std::cout << i << ", " << j << std::endl; 

// This will also print out "2, 3", as those are the values stored at those addresses
std::cout << *iPtr << ", " << *jPtr << std::endl;

// This will print out two numbers that are memory addresses
std::cout << iPtr << ", " << jPtr << std::endl;

// This looks at the value of the memory that iPtr points to, and adds it
// to the value of the memory that jPtr points to.  In other words, it adds
// the values of i and j.
int sum1 = *iPtr + *jPtr;

std::cout << sum1 << std:: endl;  // This will print the value "5"

*iPtr = sum1;  // This puts the value 5 in the memory pointed to by iPtr

std::cout << i << std::endl;  // This will now print the value "5"

iPtr = sum;  // ERROR: This attempts to set the memory address stored by iPtr to 5.

std::cout << *iPtr << std::endl;  // THIS WILL PROBABLY CAUSE A CRASH   


Hope that helps.

Edit: And are you really asking us to explain every single line of that code?
Last edited on
oh ya it is thanku, but what i meant by explain every single line of that code i mean in general not what is that called or something like this.
i mean is it clear what the code suppose to do for freshman students?
what about this statement *result = (digit < 10 ? digit + '0' : digit - 10 + 'A');

i don't know its not a function call
not a loop
then what it is?
and can i replace it with something easier than that statement
it is '?' operator

here if digit < 10 is true then *result = digit+'0' , else( if digit is not less than 10 ) then *result = digit - 10 + 'A'

ie it is the same as if you write

1
2
3
4
5
if( digit < 10 )
  *result = digit + '0';

else
  *result = digit - 10 + 'A';



See here about conditional operator
http://www.cplusplus.com/doc/tutorial/operators/
Last edited on
That's the most tricky line of code there. Whoever wrote that (I'm assuming it's not you) has crammed a lot into one line.

It's making use of a C construct that I personally hate, the conditional operator. It's basically an irritating way of packing an if... else... block into one statement. Have a look here for more info:

http://www.cplusplus.com/forum/articles/14631/

It also involves some mixed-type arithmetic for good measure.

Other than that, though, the code is pretty straightforward. To answer your question, I would say anyone with a grasp of the basics of C++, including pointers, should be able to understand it.
Last edited on
And yes, * is not part of the type, so if you write

int* iPtr, a;

then 'iPtr' is a pointer, but 'a' is not
here * is only part of iPtr type
So it is likely to write

int *iPtr, a;
Last edited on

And yes, * is not part of the type, so if you write

int* iPtr, a;

then iPtr is a pointer, but a is not


Fair point. I tend to avoid declaring multiple variables on one line anyway, to make the code clearer.
well i do understand pointers BUT my question was we use to to initialize for example int* ptr;
then after this step we use to write ptr which a pointer we don't use *ptr all the time m i right??
I had hoped that my example would demonstrate how and when to use ptr and *ptr.

If you're interested in the address of the memory, use ptr.

If you're interested in the value stored in that memory, use *ptr.

Edit: the most important thing is that you think clearly and logically about which of those things you're using in any given expression.
Last edited on
can anyone convert that code into c++ only i mean remove some c language from it and replace it to c++ in order to understand it cuz i dont know c language
and i have the Curiosity to know how its work and how they wrote that. it is not a homework just for my own knowledge
Um... C is part of C++. All C is also C++. So all the code you posted is already C++.
closed account (z05DSL3A)
All C is also C++.
Not quite.
Well, OK... but near enough.
can anyone convert that code into c++ only i mean remove some c language from it and replace it to c++ in order to understand it


The OP has a point here, I think.

Newbies to C++ often write C code except for the use of cin and cout. I did that too at the start, because I knew C. It took me a while to learn the C++ ways of doing things - which is rather different than C. I am very sure there are lots more things for me to learn - it's a gradual process.

One problem is that people mix up C and C++ thinking - like using an iterator to count chars, when they could just use the string's size function.

The other thing is teachers get their students to write their own sort functions say, because using the STL is way too easy - the student won't learn anything.

Some C++ ideas and comparisons with C (for some of them), not necessarily in order:

Use strings with their built in operators & functionality, instead of array of char with C functions like strcpy;
Use STL containers, like vector or list instead of arrays;
Use other containers like map & set where appropriate, instead of trying to implement these yourself;
Use STL algorithms like find, sort plus many others;
Use iterators to traverse through containers, instead of pointer arithmetic;
Learn function overloading;
Learn to use classes;
Learn Class design - quite involved;
Learn about virtual functions & how to use them;
Learn how to use templates.

There you go, there is a good list for starters!! Probably need a good book from Bjarne Stroustrup who invented C++. There are all kinds of resources on the internet - videos and all.

So for your code, you have char arrays and pointer arithmetic, so change these to strings and make use of the STL string functions.

Look in the reference / documentation section link at the top left of this page - there is a wealth of information there - have a good read, especially the example code.

Don't forget Google.

thanku appreciate it..

can anyone tell me what is that means in c++ *result =digit + '0';
and *result = digit - 10 + 'A';
is it if the user assign 10 to A? or what?

and the same here result[1] = '\0';
*result dereferences the result pointer - that is gets the value that result points to in memory.

digit - 10 + 'A'

This uses math on the values of chars. Chars are just small ints, so one can do this. 'A' has the value 65, so this could have been written like this:

*result = digit + 55

Although it is better to use the chars like 'A' instead of magic numbers.

HTH
Last edited on
yea i c.. but in that code they didn't mention other than the A while the code i guess it is suppose to display A instead of 10 and b for 11 till 15 in order convert decimal to hexadecimal !
any idea?
Pages: 12