Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,943 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,414 people online right now. Registration is fast and FREE... Join Now!




ARRAY problem

2 Pages V  1 2 >  
Reply to this topicStart new topic

ARRAY problem, array

janandrada
5 Jan, 2008 - 12:46 AM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
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.

CODE
#include <iostream>
using namespace std;

void add(int a1[], int a2[]){
      
     }
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){      
       add(num1, num2);
    }
    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;  
}

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: ARRAY Problem
5 Jan, 2008 - 12:55 AM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
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?
User is offlineProfile CardPM
+Quote Post

janandrada
RE: ARRAY Problem
5 Jan, 2008 - 01:18 AM
Post #3

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
QUOTE(jjhaag @ 5 Jan, 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?


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

i mean like this:
CODE

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|

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: ARRAY Problem
5 Jan, 2008 - 01:39 AM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
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:
CODE

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: 5 Jan, 2008 - 01:39 AM
User is offlineProfile CardPM
+Quote Post

janandrada
RE: ARRAY Problem
5 Jan, 2008 - 01:59 AM
Post #5

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
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
User is offlineProfile CardPM
+Quote Post

janandrada
RE: ARRAY Problem
5 Jan, 2008 - 02:06 AM
Post #6

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
CODE
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};

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: ARRAY Problem
5 Jan, 2008 - 02:08 AM
Post #7

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

janandrada
RE: ARRAY Problem
5 Jan, 2008 - 04:15 AM
Post #8

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
QUOTE(jjhaag @ 5 Jan, 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.


how can i perform the conversion?

can you give the actual code? thnx
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: ARRAY Problem
5 Jan, 2008 - 04:24 AM
Post #9

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
QUOTE(janandrada @ 5 Jan, 2008 - 05:15 AM) *

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: 5 Jan, 2008 - 04:26 AM
User is offlineProfile CardPM
+Quote Post

janandrada
RE: ARRAY Problem
5 Jan, 2008 - 04:29 AM
Post #10

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
QUOTE(jjhaag @ 5 Jan, 2008 - 05:24 AM) *

QUOTE(janandrada @ 5 Jan, 2008 - 05:15 AM) *

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..
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: ARRAY Problem
5 Jan, 2008 - 04:36 AM
Post #11

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

janandrada
RE: ARRAY Problem
5 Jan, 2008 - 04:42 AM
Post #12

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 37


My Contributions
QUOTE(jjhaag @ 5 Jan, 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:
CODE

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?
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 1/8/09 04:52PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month