0

I have no idea how to write a code which would take numbers from the array and equal them to an integer number without 0.

For example, I have an array A[size]= {12,68,45,20,10} and I need to get the answer as n= 12684521. Any ideas or hints how to make it?

Thân LƯƠNG Đình
  • 3,082
  • 2
  • 11
  • 21
David
  • 45
  • 7
  • 1
    1. Put all numbers to `std::stringstream` 2. get concatenated string 3. remove zeros via `std::remove_if` 4. convert filtered string to integer if necessary – MikeCAT Nov 02 '20 at 14:21
  • 1
    @MikeCAT using `std::to_string` would be simpler than `std::ostringstream`, and `std::string` has `erase_if` – Slava Nov 02 '20 at 14:23
  • I am a beginner in programming so I tried to use this code and it gave only the sum of numbers – David Nov 02 '20 at 14:52
  • int integer(int A[],int k) { int a=0,temp=0; for(int i=0;i – David Nov 02 '20 at 14:52

3 Answers3

1

Here is how i would do it :

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#define size 5

int main() {
  std::string ret;
  
  //your array
  int a[size]={12,68,45,20,10};
  //transform to string
  std::ostringstream out; 
  for(int i=0;i<size;i++)out << a[i];
  ret = out.str();
  //transform
  ret.erase(remove(ret.begin(), ret.end(), '0'), ret.end());
  //return string
  std::cout << ret;
  //return number
  int ret2 = std::stoi (ret);
  std::cout << ret2;
}

Console : "12684521"

GabrielT
  • 397
  • 6
  • 17
  • 2
    I really would not recommend to new devs to use for loops without braces. A new dev is going to turn that in to an error at some point. Also the transform line is a pretty complicated to parse for someone who is new. Also I think he wants the return type to be an integer – Czarking Nov 02 '20 at 15:51
  • ret.erase(remove(ret.begin(), ret.end(), '0'), ret.end()); would someone be able to explain how does this line works because it looks very complicated to me as I am 'fresh' just started programming c++ a month ago – David Nov 02 '20 at 15:55
  • I edited to return an integer also, David remove got 3 parameters First is beginning Second is End of removal Third is the term to remove – GabrielT Nov 02 '20 at 15:58
  • I added some additional info on the erase remove idiom @David – Czarking Nov 02 '20 at 16:05
  • is there a way how to give you rep or thank you in here,man? – David Nov 02 '20 at 16:05
  • @David just use the up arrow on the left of the answer and accept (green check) the most correct answer – Czarking Nov 02 '20 at 16:06
1

It is possible to do it without using strings or stringstreams. Here's the code:

int integer(int* arr, size_t length)
{
    int result = 0;
    for (size_t i = 0; i < length; i++)
    {
        int t1 = arr[i], t2 = 0;
        while (t1 != 0)
        {
            if(t1 % 10 != 0)
            {
                t2 *= 10;
                t2 += t1 % 10;
            }
            t1 /= 10;
        }
        while (t2 != 0)
        {
            if(t2 % 10 != 0)
            {
                result *= 10;
                result += t2 % 10;
            }
            t2 /= 10;
        }
    }
    return result;
}

The quirky thing is to make two inner while loops because operations in one of those loops mirror the numbers (e.g. if arr[i] is 68 then t2 becomes 86 and then 6 and 8 are appended to result).

Antoni
  • 176
  • 2
  • 6
  • Are the numbers to be multiplied by 10 or by 100? My understanding is that multiplying by 10 shifts the number by a *single* digit. – Thomas Matthews Nov 02 '20 at 16:36
  • @Thomas Numbers are multiplied by 10 because this code move digits one by one (e.g.: , , , ). However, the number is reversed, that's why there are two loops. – Antoni Nov 02 '20 at 16:51
0

As others mentioned you want to use a streaming object or if you are using C++20 you want to use remove_if.

You may want to check this out. You will need to know how to use streams to be a developer. What exactly does stringstream do?

Using @MikeCAT way

size_t concatArrrayNoZero(int[] a, size_t len){
  std::stringstream ss;
  for( auto i : a){
    //add everything to buffer    
    ss<<i;
  }
  //convert the ss into a string
  std::string str = ss.str();
  //remove the '0' from the string then erase the extra space in the string
  str.erase(std::remove(str.begin(), str.end(), '0'),
               str.end());
  //clear the buffer
  ss.clear();
  //put the nonzeros back into the buffer
  ss<<str;
  //make the return type
  size_t r;
  // get the data out of the buffer
  r<<ss;
  return r;
}

size_t is the max size unsigned integral type of your computer.

Also check out https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom for why you have erase and remove

Czarking
  • 143
  • 1
  • 10