Home > Enterprise >  Combining elements of an integer array into a single integer variable
Combining elements of an integer array into a single integer variable

Time:01-01

I am writing a simple C program that should combine all elements of an integer array to form one number. Eg. {4,5,6} --> should be 456. But my output is one less than the original number. i.e instead of 456, I am getting 455. Sometimes my program works fine and sometimes not. Can someone please explain to me what is causing this unpredictible behaviour? Thank You!!

Please take a look at my code:

#include <bits/stdc  .h>
#include <cmath>
using namespace std;

int main()
{
    int A[5] = {4,5,6,7,8};
    int lengthA = 5;
    int num = 0;
    
    for(int x = 0; x < lengthA; x  )
    {
        
        num = A[x]*pow(10,lengthA-1-x)   num;

    }
    printf("%d\n", num ); // My O/P is 45677 

}

CodePudding user response:

As mentioned by Bob__, pow is a function for doubles and other floating-point types. For this specific algorithm, instead, we can do this:

    int A[5] = {4,5,6,7,8};
    int lengthA = 5;
    int num = 0;
    
    for(int x = 0; x < lengthA; x  )
    {
        
        num = num*10   A[x];

    }
At each step, this multiplies the previous number by 10, and makes the digit correct at that place.
E.g. \
Step 1: num = 0*10   4 == 4 
Step 2: num = 4 * 10   5 == 40   5 == 45
Step 3: num = 45 * 10   6 == 450   6 == 456
Step 4: num = 456 * 10   7 == 4560   7 == 4567
Step 5: num == 4567 * 10   8 == 45670   8 == 45678

CodePudding user response:

From this simple problem you can already learn quite a bit to improve your C code. Example :

// #include <bits/stdc  .h>  // NO : https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h
// using namespace std // NO : https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

#include <iostream> // include only what you need for std::cout

int main()
{
    int values[]{ 4,5,6,7,8 }; // no need for an =
    int num{ 0 };

    // prefer range based for loops
    // they will not run out of bounds
    // https://en.cppreference.com/w/cpp/language/range-for
    for (const int value : values) 
    {
        num *= 10;
        num  = value;
    }

    // avoid printf, use std::cout with C  20 std::format for formatting
    // https://stackoverflow.com/questions/64042652/is-printf-unsafe-to-use-in-c
    // https://en.cppreference.com/w/cpp/utility/format/format
    std::cout << "num = " << num << "\n";

    return 0;
}

CodePudding user response:

You can do this:

#include <array>
#include <cstdio>


int main( )
{
    constexpr std::size_t arraySize { 5 };
    constexpr std::array<int, arraySize> arrayOfNums { 4, 5, 6, 7, 8 }; // use std::array
                                                                        // instead of raw
                                                                        // arrays

    int num { };

    for ( std::size_t idx { }; idx < arraySize;   idx )
    {
        std::size_t powerOfTen { 1 };

        for ( std::size_t count { }; count < arraySize - 1 - idx;   count )
        {
            powerOfTen *= 10; // this loop multiplies 10 by 10 as many times as required
                              // (equivalent to std::pow(10, arraySize - 1 - idx))
        }

        num  = arrayOfNums[ idx ] * powerOfTen; // already obvious
    }

    printf( "%d\n", num );

    return 0;
}

Note regarding constexpr: Use const and constexpr keywords wherever possible. It's a very good practice. Read about it here constexpr (C ).

  • Related