-2

I'm running into some coding troubles.

I am reading serial input into my program, and it saves the numbers into an array.

Now I want to convert this array into an integer to do calculations, where Array index 0 represents the one digits, index 1 the ten digits etc.

Array[0] represents 1 digit
Array[1] represents 10 digit
Array[2] represents 100 digit
Array[3] represents 1000 digit
Array[4] represents 10000 digit
Array[5] represents 100000 digit

The array is dynamic, so could have only 2 indexes.

DowinskiField
  • 133
  • 2
  • 15

4 Answers4

1

Use a loop. You must know the length of the number, or the length of the array. Unused positions in the array should be 0.

int i;
long result = 0; // Choose a suitable integer type. 
for (i = length; i <= 0; --i) {
    result = result * 10 + Array[i];
}
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • Thats actually a pretty elegant solution compared to the one I came up with below. +1 for that :) – Magisch Oct 26 '16 at 07:45
0

Given if all the array entries are 0-9, and given they are in a data type that can be converted to int without a cast, this could work for you:

#include <stdio.h>

int main (void) {

int lenght = 6;
int Array[lenght];
Array[0] = 9;
Array[1] = 4;
Array[2] = 5;
Array[3] = 8;
Array[4] = 1;
Array[5] = 0;
int pow = 1;
int val = 0;

for (int i = 0; i < lenght; i++) {
    val += Array[lenght-i-1] * pow;
    pow *= 10;
}

printf("Val is %d", val);
return 0;
}

Note: This is a primitive, non error handled approach. It may not work in all cases.

Reason for not using pow:

pow does not return a integer, leading to inaccuracies with implicit integer casts. E.G 100 could be 99.9999 instead and then get truncated to 99.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • That is a bit funny. Maybe you should mention you mean the function `pow` in `math.h`. Also, what sort of system has a floating point number representation that cannot represent 100 accurately? I guess c doesn't always define it, so it could be possible that your precision is less than 7 bits, but your example is still a bit far fetched. – matt Oct 26 '16 at 08:19
  • @matt Floating point accuracy and what it can't/can accurately represent isn't defined by the C standard. And implementations vary wildly. So it is absolutely not safe to assume that they're always going to be accurate. – Magisch Oct 26 '16 at 08:22
  • 3
    It is defined in the floating point standard which should be used by c. There you have a guarantee that integers to a given point are always totally accurate. – Kami Kaze Oct 26 '16 at 08:37
  • @KamiKaze Should =/= Guaranteed. Its not safe. Floating point math in general in C is absolutely not safe. – Magisch Oct 26 '16 at 08:54
  • 1
    This was not about calculation it was about accuracy. Depending on the given algorithm you can not be sure to hit an integer (i.e 0.9*10), but entering 9 you can be sure that it is 9. and not 8.99999. That's what I wanted to make clear. – Kami Kaze Oct 28 '16 at 06:38
0

I'm not 100% sure what you are trying to do, but is this what you want to do:

#include <stdio.h>

int main ( void )
{

    int numbers[] = { 1, 2, 3, 4, 5, 6 };
    int multiplier = 1;

    printf ( "First printout\n" );
    for ( int i = 0; i < 5; ++i )
        printf ( "Numbers[%i] = %i\n", i, numbers[i] );

    //this is where the numbers in the array are made into the 1, 10s, 100s etc
    for ( int i = 0; i < 5; ++i )
    {
        numbers[i] *= multiplier;
        multiplier *= 10;
    }

    printf ( "Second printout\n" );
    for ( int i = 0; i < 5; ++i )
        printf ( "Numbers[%i] = %i\n", i, numbers[i] );

    return 0;

}

I just started learning C a couple of months ago. I'm sure there are better ways to do this, but this is a solution that seems to solve your problem. I hope this helps!

-1

The digits represent the value in the tens place.

Array[0]*1 + Array[1]*10 + Array[2]*100 ... etc.

You could make a loop to do it.

int value = 0;
int pow = 1;
for(int i = 0; i<length; i++){

   value += Array[i]*pow;
   pow *=10;
}
matt
  • 10,892
  • 3
  • 22
  • 34
  • pow ? Never use this function when you want integer, because pow(10,2) may return 99.9999, not 100. Juste have a variable "pow" initialized to 1, and pow *= 10 in the for. – Tom's Oct 26 '16 at 07:37
  • True, it is also going to be a double type and not an `int`. – matt Oct 26 '16 at 07:39