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?

4
  • 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 Commented Nov 2, 2020 at 14:21
  • 1
    @MikeCAT using std::to_string would be simpler than std::ostringstream, and std::string has erase_if Commented Nov 2, 2020 at 14:23
  • I am a beginner in programming so I tried to use this code and it gave only the sum of numbers Commented Nov 2, 2020 at 14:52
  • int integer(int A[],int k) { int a=0,temp=0; for(int i=0;i<k;i++) { temp =A[i]; a=a+temp; } return a; } Commented Nov 2, 2020 at 14:52

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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 Numbers are multiplied by 10 because this code move digits one by one (e.g.: <t1 = 607, t2 = 0>, <t1 = 60, t2 = 7>, <t1 = 6, t2 = 7>, <t1 = 0, t2 = 76>). However, the number is reversed, that's why there are two loops.
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"

6 Comments

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
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
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
I added some additional info on the erase remove idiom @David
is there a way how to give you rep or thank you in here,man?
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.