Can you append digits to a variable in c++ or python

  • Context: C/C++ 
  • Thread starter Thread starter OrbitalPower
  • Start date Start date
  • Tags Tags
    C++ Python Variable
Click For Summary

Discussion Overview

The discussion revolves around the methods for appending digits to a variable in C++ and Python, focusing on both integer manipulation and string conversion techniques. Participants explore various approaches, including loops for constructing numbers and considerations for integer overflow.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • Some participants propose using loops to append digits to an integer by multiplying by 10 and adding a new digit.
  • Others suggest using string manipulation methods to avoid integer overflow, advocating for the use of std::string in C++.
  • A method for converting between strings and integers is presented, utilizing string streams in C++.
  • One participant mentions the possibility of appending digits at both the low and high ends of a number, with specific arithmetic methods for each case.
  • Concerns about the limitations of integer size in C++ are raised, with a suggestion to use arbitrary precision arithmetic libraries for larger numbers.
  • In Python, participants note the language's support for arbitrary length integers, allowing for more extensive appending without overflow issues.
  • Clarifications about code snippets and corrections to earlier posts are provided, indicating ongoing refinement of ideas.

Areas of Agreement / Disagreement

Participants express multiple competing views on the best approach to appending digits, with no consensus on a single method. There is agreement on the utility of both arithmetic and string manipulation techniques, but preferences vary.

Contextual Notes

Some methods discussed may depend on specific definitions of integer size and the handling of overflow, particularly in C++. The discussion also highlights the differences in handling large numbers between C++ and Python.

Who May Find This Useful

Readers interested in programming techniques for number manipulation in C++ and Python, particularly those dealing with integer overflow and string conversion methods.

OrbitalPower
Say you have a variable x, that is of type int.

x = 1;

I just want to create a while or for loop so that each time it runs it adds another digit to it, such as:

12
123
1234

or 1, 11, 111, 1111, and so on.
 
Technology news on Phys.org
I know you can do this:

char y;
y = '1';

char test[2];

test[0] = y;
test[1] = y;
test[2] = '\0';

But I wouldn't know how to get it back to int in C++
 
Code:
int num=1;
for (int i=2; i<len; ++i)
{
   num*=10;
   num+=i;
}

Obviously though, you'd have to watch out for integer overflow.

You could also go with the string method, and if you don't need to actually perform operations on the number then it would work fine and you wouldn't have to worry about overflow. I'd use std::string instead of a char array, though.
 
Also, since you might need to convert a string to an integer or visa versa at some point:
Code:
#include <sstream>

int str_to_int(std::string str)
{
  int num=0;
  std::istringstream is(str);
  is>>num;
  return num;
}

std::string int_to_str(int num)
{
  std::ostringstream os;
  os<<num;
  return os.str();
}

These can be easily modified for use with other numeric types as well (for all the types std::*stringstream objects can handle anyways) You could even write a generic template function if you felt like it.
 
OrbitalPower said:
Say you have a variable x, that is of type int.

x = 1;

I just want to create a while or for loop so that each time it runs it adds another digit to it, such as:

12
123
1234

or 1, 11, 111, 1111, and so on.

To append at the low end, multiply by 10 and add [digit].
To append at the high end, add [digit] * 10 ^ floor(log_10(n)).

You can compute the log relatively quickly using shifts and a constant for 1/log_2(10).
 
Yes, thank you both JaWiB and Strilanc. I guess to do the 1111+ I'd so something like:

Code:
	int x;
	x = 1;
	
   int num=1;
for (int i=2; i<8; i++)
{
   num = num * 10;
   num = num + 1;
   
}


I guess I was so busy thinking about trying to convert between types I didn't think of the simple arithmetic solution.

I also like the append at the high end method.
 
I don't know how big you want your numbers, but if you want them really big you will need to download an arbitrary precision arithmetic library. There are a bunch available for C++, but I don't know about Python.
 
In Python, the above would become:
Code:
maxappends = 8   # actually its max appends -2
num = 1
for i in range(2,maxappends):
  num = num*10 + 1
You get the added benefit of arbitrary length integers: maxappends could be 1001 or more.

To add digits to the LHS (only makes sense if the digits are different, and if you are not prepending a zero):
Code:
maxappends = 20; num = 0
for i in range(maxappends):
  num = num + ((i % 9) + 1) * 10**(i)
  print i,num

- Have fun, Paddy :smile:
 
paddy3118 said:
In Python, the above would become:
Code:
maxappends = 8   # actually its max appends -2
num = 1
for i in range(2,maxappends):
  num = num*10 + 1
You get the added benefit of arbitrary length integers: maxappends could be 1001 or more.

This is built in natively? What I was doing was constructing a program that return the remainder in a number composed entirely of ones with 50 or so digits. The idea was to determine the remainder by using the divisor only a few times into the dividend, as it was also composed entirely of ones. Obviously, to do that in C++ I'd perhaps need one of those libraries needed above. So, basically I just tested my method with smaller numbers.

*Also fixed an error I made, should be:

char test[3];

test[0] = y;
test[1] = y;
test[2] = '\0';

and not char test[2].
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
Replies
55
Views
7K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
Replies
9
Views
4K
Replies
1
Views
2K