# ARRAY problem

• (2 Pages)
• 1
• 2

## 20 Replies - 2639 Views - Last Post: 06 January 2008 - 11:39 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=40385&amp;s=2a6ddf05de2b798f069aebb94c86fd68&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 43
• Joined: 27-September 07

# ARRAY problem

Posted 05 January 2008 - 01:46 AM

problem:

christopher, a high school computer whiz kid, has been challenged by his cousin Thea, a math genius, to compute for integer using the four basic arithmetic operations using a computer program. however, thea, wanting his dear cousin to fail, deliberately increased the complexity on how it will be done. from her explanation, integers entered in the console by users should be temporarily stored into a variable and later copied to an array, digit by digit.

she then added that the arithmetic operation, except division, must be done digit by digit in the array, starting from its last element progressing to the first. just like the normal way.

```#include <iostream>
using namespace std;

}
void subtract(int a1[], int a2[]){

}
void multiply(int a1[], int a2[]){

}
void divide(int a1[], int a2[]){

}

int main (){

int num1, num2, x;
char again;

do{
cout << "Type 2 integers seperated by space: ";
cin>>num1>>num2;
cout << "[1] Add [2] Subtract [3] Multiply [4] Divide" <<endl;
cout << "Please choose operation: ";
cin>>x;

if (x==1){
}
else if (x==2){
subtract(num1, num2);
}
else if (x==3){
multiply(num1, num2);
}
else if (x==4){
divide(num1, num2);
}
else{
cout << "Error: Input not recognized!" <<endl;
}

cout << "Try Again? [y/n]: ";
cin>>again;
}while (again != 'n');

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: ARRAY problem

### #2 jjhaag

• me editor am smartastic

Reputation: 46
• Posts: 1,789
• Joined: 18-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 01:55 AM

Okay...

It looks like you haven't attempted to finish this code skeleton. We aren't going to just write your functions for you - do you have a question on this?

Reputation: 0
• Posts: 43
• Joined: 27-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 02:18 AM

jjhaag, on 5 Jan, 2008 - 01:55 AM, said:

Okay...

It looks like you haven't attempted to finish this code skeleton. We aren't going to just write your functions for you - do you have a question on this?

How will i going to store the inputed numbers in the array?

i mean like this:
```standard addition process										addition process using
using variables														using arrays

123456789				   >										|1|2|3|4|5|6|7|8|9|
+	  987				   >									   +			|9|8|7|
____________														  _________________
1234567776														  |1|2|3|4|5|6|7|7|7|6|

```

### #4 jjhaag

• me editor am smartastic

Reputation: 46
• Posts: 1,789
• Joined: 18-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 02:39 AM

Well, you can try using the modulus operator and division by 10.

As you work through an integer, the next least significant digit can be found by taking the remainder after division by 10 i.e. modulus 10. Store this remainder as the current digit. Then divide the original number by 10 and repeat.

To get the remainder after division with integers in C/C++, you use the % operator. So using the number 123456789 as an example:
```int num=123456789;
int array[10];   //empty array for storing digits

array[9]=num%10;  //last element of array now contains the digit 9

num=num/10;   //num is now equal to 12345678
```

Following the last division by ten step, you would then repeat until the variable num is equal to zero.

This post has been edited by jjhaag: 05 January 2008 - 02:39 AM

Reputation: 0
• Posts: 43
• Joined: 27-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 02:59 AM

in my first code above, how can i pass and store num1 and num2 to void add(int a1[], int a2[]) in the function? , and then perform the operation

Reputation: 0
• Posts: 43
• Joined: 27-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 03:06 AM

```for example like this;

int input;
input = 1234;

int array [];

how can i store input inside the array element?
like this;

array [] = {1, 2, 3, 4};
```

### #7 jjhaag

• me editor am smartastic

Reputation: 46
• Posts: 1,789
• Joined: 18-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 03:08 AM

Those functions don't accept integers, they accept arrays - if that's the format for the functiosn that you're supposed to use, you need to perform the conversion to arrays first, and then pass the arrays, not num1 and num2, to the functions when you make the calls from main().

As for actually performing the operations, you do it just like you would perform addition, subtraction, and multiplication by hand. Digit by digit, carrying to or borrowing from the next most significant digit when necessary.

Reputation: 0
• Posts: 43
• Joined: 27-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 05:15 AM

jjhaag, on 5 Jan, 2008 - 03:08 AM, said:

Those functions don't accept integers, they accept arrays - if that's the format for the functiosn that you're supposed to use, you need to perform the conversion to arrays first, and then pass the arrays, not num1 and num2, to the functions when you make the calls from main().

As for actually performing the operations, you do it just like you would perform addition, subtraction, and multiplication by hand. Digit by digit, carrying to or borrowing from the next most significant digit when necessary.

how can i perform the conversion?

can you give the actual code? thnx

### #9 jjhaag

• me editor am smartastic

Reputation: 46
• Posts: 1,789
• Joined: 18-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 05:24 AM

janandrada, on 5 Jan, 2008 - 05:15 AM, said:

how can i perform the conversion?

You were shown a short example of how to do this. If need further explanation about a particular part, please ask - and ask clear, detailed questions.

Quote

can you give the actual code? thnx

No.

As the forum rules explicitly say, we will not do your homework for you.

This post has been edited by jjhaag: 05 January 2008 - 05:26 AM

Reputation: 0
• Posts: 43
• Joined: 27-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 05:29 AM

jjhaag, on 5 Jan, 2008 - 05:24 AM, said:

janandrada, on 5 Jan, 2008 - 05:15 AM, said:

how can i perform the conversion?

You were shown a short example of how to do this. If need further explanation about a particular part, please ask - and ask clear, detailed questions.

Quote

can you give the actual code? thnx

No.

As the forum rules explicitly say, we will not do your homework for you.

ok...

can you just teach me how to convert to array so that i can pass it to the functioin..

thnx..

### #11 jjhaag

• me editor am smartastic

Reputation: 46
• Posts: 1,789
• Joined: 18-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 05:36 AM

Read back over the second post that I made in this thread. It shows you the first steps in doing so - and to get the entire number stored as an array, you just need to perform those same steps repeatedly until all the digits have been converted.

Reputation: 0
• Posts: 43
• Joined: 27-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 05:42 AM

jjhaag, on 5 Jan, 2008 - 02:39 AM, said:

Well, you can try using the modulus operator and division by 10.

As you work through an integer, the next least significant digit can be found by taking the remainder after division by 10 i.e. modulus 10. Store this remainder as the current digit. Then divide the original number by 10 and repeat.

To get the remainder after division with integers in C/C++, you use the % operator. So using the number 123456789 as an example:
```int num=123456789;
int array[10];   //empty array for storing digits

array[9]=num%10;  //last element of array now contains the digit 9

num=num/10;   //num is now equal to 12345678
```

Following the last division by ten step, you would then repeat until the variable num is equal to zero.

What if the size of array in not known?

### #13 jjhaag

• me editor am smartastic

Reputation: 46
• Posts: 1,789
• Joined: 18-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 05:53 AM

There are a bunch of ways, but the simplest would be to choose a size sufficiently large to hold the biggest int your program can handle. For an unsigned 4-byte int, that's 4294967296 - so that's 10 elements. For an unsigned long long int on my machine (8-bytes), that's 18 446 744 073 709 551 616 - 20 elements.

Leading zeros won't affect your calculations in any way, so you could even do something like 100 elements and it wouldn't change things, as long as you fill the extra leading elements with zeros. But doing the calculations on something that big would be a waste of processor time.

Reputation: 0
• Posts: 43
• Joined: 27-September 07

## Re: ARRAY problem

Posted 05 January 2008 - 08:11 PM

I'm done in 'determining the digits of numbers in an integer' and 'storing integers into array elements'.
Now, my problem is passing the array in the function...

```#include <iostream>
using namespace std;

}
void subtract(int a1[], int a2[]){

}
void multiply(int a1[], int a2[]){

}
void divide(int a1[], int a2[]){

}

int main (){

int num1, num2, x;
char again;

do{
cout << "Type 2 integers seperated by space: ";
cin>>num1>>num2;

int count1 = 0;
while (num1 > 0)//counts the number of digits in num1
{
num1 = num1/10;
count1++;
}
int count2 = 0;
while (num2 > 0)//counts the number of digits in num2
{
num2 = num2/10;
count2++;
}

int array1[count1];
int array2[count2];

for(int i=0;i<count1;i++){ //store num1 in array1[]
array1[count1--]=num1%10;
num1=num1/10;
}
for(int i=0;i<count2;i++){ //store num2 in array2[]
array2[count1--]=num2%10;
num2=num2/10;
}

cout << "[1] Add [2] Subtract [3] Multiply [4] Divide" <<endl;
cout << "Please choose operation: ";
cin>>x;

if (x==1){
}
else if (x==2){
subtract(array1, array2);
}
else if (x==3){
multiply(array1, array2);
}
else if (x==4){
divide(array1, array2);
}
else{
cout << "Error: Input not recognized!" <<endl;
}

cout << "Try Again? [y/n]: ";
cin>>again;
}while (again != 'n');

return 0;
}

```

### #15 Tom9729

• Segmentation fault

Reputation: 180
• Posts: 2,641
• Joined: 30-December 07

## Re: ARRAY problem

Posted 05 January 2008 - 08:25 PM

You have to pass a pointer (pointing to the array) to the function.

I don't think I'm qualified to explain pointers, they STILL confuse me at times, and they were probably the hardest thing to grasp when I was learning C.

This tutorial looks alright, you should give that a try.

If not, I'm sure someone who is better at explaining will help you out.