why my int only can accept total 11 integer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include< math.h>

using namespace std;

int main(){

	int firstNumber = 0;
	int secondNumber = 0;

	cout << "Enter first number  : ";
	cin  >> firstNumber;

	cout << "Enter second number : ";
	cin  >> secondNumber;

	int *firstarray;
	firstarray = new int;
	int firstRemain = 0;
	int i = 0;

	while( firstNumber > 0 ){
		firstRemain = firstNumber % 100;
		firstNumber = firstNumber / 100;
		firstarray[i] = firstRemain;
		cout << firstarray[i] << endl;
		i++;
	}
	cout << "First array size : " << i << endl;

	int *secondarray;
	secondarray = new int;
	int secondRemain = 0;
	int j = 0;

	while( secondNumber > 0 ){
		secondRemain = secondNumber % 100;
		secondNumber = secondNumber / 100;
		secondarray[j] = secondRemain;
		cout << secondarray[j] << endl;
		j++;
	}
	cout << "Second array size : " << j << endl;

	int sum = 0;
	double multiply = 100;

	for( int number = 0 ; number < i ; number++ ){
		for( int number1 = 0 ; number1 < j ; number1++ ){
			if( number > 0 ){
				if( number1 > 0 ){
					sum += ( (firstarray[number] * secondarray[number1]) * pow( multiply , number) * pow( multiply , number1 ));
				}
				else{
					sum += ( (firstarray[number] * secondarray[number1]) * pow( multiply , number) );
				}
			}
			else{
				if( number1 > 0 ){
					sum += ( ( firstarray[number] * secondarray[number1] ) * pow( multiply , number1) );
				}
				else{
					sum += ( firstarray[number] * secondarray[number1] );
				}
			}
		}
	}

	cout << "Total is : " << sum << endl;

	system( "pause" );//Pause window
	return 0;
}


this is divide and conquer method for me .
i done it all already. but it only can run 5 integer times 5 integer .
example

 
12345 * 12345 = 152399025


it's still accept if
 
123456 * 12345 = 1524064320



but if over 6 integer on either 1 variable. all negative value out
 
123456 * 123456 = -213234412321// will give me this sign 


mean totally

 
input1 + input2 <=11 // maximum receive total 11 input . cannot accept 12 input else get -sign 


isnt because im using int integer?
Last edited on
1
2
	int *firstarray;
	firstarray = new int;

you have only allocated memory for one int. that means that i[1] and greater will cause access to random memory area. This will cause unintended behavior later.

123456*123456 cannot be represented by int. It is too big. you can use long long instead.
you mean where should i put the long long variable on?
the pointer there?
int x can hold values from -2,147,483,648 to 2,147,483,647
unsigned int x can hold values from 0 to 4,294,967,295
long long x can hold values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
123456*123456 = 15,241,383,936
Think about it.

your firstarray = new int is equivalent to firstarray = new int[1] and is almost equivalent to int firstarray[1]. You shold write firstarray = new int[ARRAY_SIZE] where ARRAY_SIZE is desired size of array. If you don't know your array size, you might use vector type
You can use std::numeric_limits to see the smallest and biggest value that the different types can hold. The exact values can differ between compilers and platforms.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <limits>

int main()
{	
	std::cout << "unsigned char can hold values between " 
	          << (int) std::numeric_limits<unsigned char>::min() << " and " 
	          << (int) std::numeric_limits<unsigned char>::max() << ".\n";
	std::cout << "signed char can hold values between " 
	          << (int) std::numeric_limits<signed char>::min() << " and " 
	          << (int) std::numeric_limits<signed char>::max() << ".\n";
	
	std::cout << "unsigned short can hold values between " 
	          << std::numeric_limits<unsigned short>::min() << " and " 
	          << std::numeric_limits<unsigned short>::max() << ".\n";
	std::cout << "signed short can hold values between " 
	          << std::numeric_limits<signed short>::min() << " and " 
	          << std::numeric_limits<signed short>::max() << ".\n";
	
	std::cout << "unsigned int can hold values between " 
	          << std::numeric_limits<unsigned int>::min() << " and " 
	          << std::numeric_limits<unsigned int>::max() << ".\n";
	std::cout << "signed int can hold values between " 
	          << std::numeric_limits<signed int>::min() << " and " 
	          << std::numeric_limits<signed int>::max() << ".\n";
	
	std::cout << "unsigned long can hold values between " 
	          << std::numeric_limits<unsigned long>::min() << " and " 
	          << std::numeric_limits<unsigned long>::max() << ".\n";
	std::cout << "signed long can hold values between " 
	          << std::numeric_limits<signed long>::min() << " and " 
	          << std::numeric_limits<signed long>::max() << ".\n";
	
	std::cout << "unsigned long long can hold values between " 
	          << std::numeric_limits<unsigned long long>::min() << " and " 
	          << std::numeric_limits<unsigned long long>::max() << ".\n";
	std::cout << "signed long long can hold values between " 
	          << std::numeric_limits<signed long long>::min() << " and " 
	          << std::numeric_limits<signed long long>::max() << ".\n";
}
Last edited on
Topic archived. No new replies allowed.