473,507 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

loop problems/memory problems?

7 New Member
I am writing a C++ program for class, I am fairly new to C++ but have plenty of programming history. I have to split up a integer into an array. but I am having trouble just getting the count for the array!

Expand|Select|Wrap|Line Numbers
  1. #include <iomanip>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class LongInt
  7. {
  8.     public:
  9.         LongInt();
  10.         LongInt(int[], int);
  11.         void print();
  12.         void read();
  13.         LongInt add(LongInt);
  14.         void setLength(int);
  15.         void createIntArray(int);
  16.         int getLength();
  17.     private:
  18.         int *a;
  19.         int length;
  20. };
  21.  
  22. LongInt::LongInt()
  23. {
  24.  
  25. }
  26.  
  27. LongInt LongInt::add(LongInt other)
  28. {
  29.     int firstIntLength = length;
  30.     int secondIntLength = other.length;
  31.     int resultIntLength;
  32.  
  33.     if(firstIntLength> secondIntLength) 
  34.         resultIntLength = firstIntLength + 1;
  35.     else resultIntLength = secondIntLength + 1;
  36.  
  37.     LongInt result;
  38.     result.a = new int[resultIntLength];
  39.     result.length = resultIntLength;
  40.  
  41.     int carry = 0;
  42.     int p1 = firstIntLength -1;
  43.     int p2 = secondIntLength -1;
  44.     int p3 = resultIntLength -1;
  45.  
  46.     for( int i = resultIntLength - 3 ; i>0 ; i-- )
  47.     {
  48.         result.a[p3] = a[p3] + carry;
  49.         if( p1 >= 0 )
  50.         {
  51.             result.a[p3] = result.a[p3] + a[p1];
  52.             p1--;
  53.         }
  54.         if(p2>=0)
  55.         {
  56.             result.a[p3] = result.a[p3] + other.a[p2];
  57.             p2--;
  58.         }
  59.         carry = (result.a[p3]/10);
  60.         result.a[p3] = (result.a[p3])%10;
  61.         p3--;
  62.     }
  63.     result.a[p3] = carry;
  64.     return result;
  65. }
  66.  
  67. void LongInt::setLength(int intLength)
  68. {
  69.     length = intLength;
  70. }  
  71.  
  72. int LongInt::getLength()
  73. {
  74.     return length;
  75. }
  76.  
  77. void LongInt::read()
  78. {
  79.     unsigned long int number;
  80.     int remainder;
  81.     int lengthcount = 0;
  82.  
  83.     cin >> number;
  84.  
  85.     unsigned long int newnumber = number;
  86.  
  87.     while( newnumber >= 1 ) 
  88.     {            
  89.         lengthcount++;
  90.  
  91.         newnumber = newnumber/10;
  92.  
  93.         cout << newnumber << endl;
  94.     }
  95.  
  96.     cout << lengthcount << endl;
  97.  
  98.     a = new int(lengthcount);
  99.  
  100.     for( int i = lengthcount-1 ; i >= 0 ; i-- )
  101.     {
  102.         remainder = number%10;
  103.         number = number/10;
  104.  
  105.         a[i] = remainder;
  106.         cout << a[i] << endl;
  107.     }
  108.     setLength(lengthcount);
  109. }
  110.  
  111. void LongInt::print()
  112. {
  113.     for( int i = 0 ; i < getLength() ; i++)
  114.         cout << a[i];
  115.     cout << endl;
  116. }
  117. int main()
  118. {
  119.     LongInt I1, I2, I3;
  120.  
  121.     I1.read();
  122.     //I2.read();
  123.  
  124.     //I3 = I2.add(I1);
  125.  
  126.     I1.print();
  127.     //I2.print();
  128.     //I3.print();
  129.  
  130.     return 0;
  131. }
it works fine for numbers with 10 or less digits. Once I put in a digit of 11 or more (not mattering what the digit is. goes all to hell... The program isnt done, but I am stuck here and cant figure out why. Any help is much appreciated. I am on Mint Linux using G++

if I put in 1111111111 it will put out 1111111111
if I put in 11111111111 it will put out 3086772644

it will always put out that number. if i change it to an unsigned long int. it puts out the same number. long int does that same number also.
Sep 30 '07 #1
4 1334
JosAH
11,448 Recognized Expert MVP
if I put in 1111111111 it will put out 1111111111
if I put in 11111111111 it will put out 3086772644

it will always put out that number. if i change it to an unsigned long int. it puts out the same number. long int does that same number also.
I guess all your (unsigned) int types are four bytes long; 11111111111 is larger
than 2^32-1. 1111111111 (ten ones) do fit in a four byte int.

kind regards,

Jos
Sep 30 '07 #2
quakersquares
7 New Member
so its just a memory issue? not even an unsigned long int wont hold the a that large?
Sep 30 '07 #3
JosAH
11,448 Recognized Expert MVP
so its just a memory issue? not even an unsigned long int wont hold the a that large?
Yup; 32 bits can just hold numbers in the range [0, 2^32) or any equivalent range
but that's just our interpretation thereof, e.g. signed ints mean [-2^31, 2^31) to us.

kind regards,

Jos
Sep 30 '07 #4
quakersquares
7 New Member
thanks man, I appreciate it
Sep 30 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
9042
by: Steve | last post by:
Hi; I would like to read a list of tables from a temp table and then do a sql statement on each table name retrieved in a loop, ie: -- snip cursor loop where cursor contains a list of...
13
4095
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
13
2169
by: Redduck | last post by:
Hello everyone. I am frustrated, I have written the simple program below for a class and I am having problems with the DO-WHILE loop. On the first run through the loop, everything works well, the...
34
3385
by: sushant | last post by:
hi all, suppose i have 2 loops one inside the other, like this 1) for(i=0;i<=100;i++) { for(j=0;j<=10;j++) { some code; }
73
4536
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
9
5020
by: pointer noob | last post by:
Hi, I am trying to write a bit of code to iterate through memory addresses and if the address is divisable by 2 then write one byte, if not write a different byte. I am having trouble with the...
51
3879
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
10
7620
by: Steven Woody | last post by:
i have a program which always run dead after one or two days, i think somewhere a piece of the code is suspicious of involving into a infinite loop. but for some reason, it is very hard to debug....
1
1891
by: sam | last post by:
hi all, i am writing some software to model polymerisation kinetics and have created a way of doing so which involves taking ever smaller slices of time to model until some sort of convergence...
0
2190
by: mac | last post by:
I found that with memory allocating techniques used nowadays (addresses alignment, eg. on 32bit machines) one can detect loops in a tree structure very fast, without using extra memory. This is due...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.