Welcome to Dream.In.Code
Getting C++ Help is Easy!

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




Help with math operations

 
Reply to this topicStart new topic

Help with math operations, Math operations over an array in C++

bert
post 4 Sep, 2008 - 07:26 PM
Post #1


New D.I.C Head

*
Joined: 4 Sep, 2008
Posts: 3

Computer Whiz versus Math Genius!
Christopher, a high school computer whiz kid, has been challenged by his cousin Thea, a math Genius, to compute for
integers 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 broadened the details by
providing an illustration.
Standard Addition Process Thea’s Addition Process
Using Variables Using Arrays

She then added that the arithmetic operations, 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. Christopher was startled! He never expected a challenge like this.
As his buddy, your task is to help him design and implement an algorithm that will perform the four (4) basic arithmetic
operations, namely: addition, subtraction, multiplication, and division. To follow good programming standard, you both
decided to have four (4) user-defined functions to complement the said operations. Here are the function prototypes:
void add(int a1[], int a2[]);
void subtract(int a1[], int a2[]);
void multiply(int a1[], int a2[]);
void divide(int a1[], int a2[]);
Christopher also added that for clarity, it is a must to define a void-type function display(int result[]) in main()
to display the elements of any array passed to it.
Sample Input/Output:
Type 2 integers separated by a space: 123 59
[1] Add [2] Subtract [3] Multiply [4] Divide
Please choose operation -> 1
123 + 59 is 182
Try Again? [y/n] -> y
Type 2 integers separated by a space: 123 59
[1] Add [2] Subtract [3] Multiply [4] Divide
Please choose operation -> 6
Error: Input not recognized!
Try Again? [y/n] -> n
Bye!
User is offlineProfile CardPM

Go to the top of the page

NickDMax
post 4 Sep, 2008 - 08:20 PM
Post #2


2B||!2B

Group Icon
Joined: 18 Feb, 2007
Posts: 2,858



Thanked 47 times

Dream Kudos: 550
My Contributions


To help you we need to see the code that you are trying to get working.

The rules state that we can not just provide you with code you have to attempt the problem and then we can help you get your code working.
User is offlineProfile CardPM

Go to the top of the page

bert
post 4 Sep, 2008 - 08:25 PM
Post #3


New D.I.C Head

*
Joined: 4 Sep, 2008
Posts: 3

cpp

#include<iostream>
using namespace std;

void add(int a1[], int a2[])
{
int sum;
sum = a1[1] + a2[1];
cout<< "The sum of the two integers is "<< sum <<endl;

}
void subtract(int a1[], int a2[])
{
int difference;
difference = a1[1] - a2[1];
cout<< "The difference of the two integers is "<< difference <<endl;

}
void multiply(int a1[], int a2[])
{
int product;
product = a1[1] * a2[1];
cout<< "The product of the two integers is "<< product <<endl;

}
void devide(int a1[], int a2[])
{
int quotient;
quotient = a1[1] / a2[1];
cout<< "The quotient of the two integers is "<< quotient <<endl;

}
int main(void)
{
int opt;
int num1[1], num2[1];
char ch;
do{
cout<< "Enter two integers separated by a space: ";
cin>> num1[1];
cin>> num2[1];


add(num1, num2);


subtract(num1, num2);



multiply(num1, num2);



devide(num1, num2);


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


return 1;
}
User is offlineProfile CardPM

Go to the top of the page

bert
post 4 Sep, 2008 - 08:26 PM
Post #4


New D.I.C Head

*
Joined: 4 Sep, 2008
Posts: 3

cpp

#include<iostream>
using namespace std;

void add(int a1[], int a2[])
{
int sum;
sum = a1[1] + a2[1];
cout<< "The sum of the two integers is "<< sum <<endl;

}
void subtract(int a1[], int a2[])
{
int difference;
difference = a1[1] - a2[1];
cout<< "The difference of the two integers is "<< difference <<endl;

}
void multiply(int a1[], int a2[])
{
int product;
product = a1[1] * a2[1];
cout<< "The product of the two integers is "<< product <<endl;

}
void devide(int a1[], int a2[])
{
int quotient;
quotient = a1[1] / a2[1];
cout<< "The quotient of the two integers is "<< quotient <<endl;

}
int main(void)
{
int opt;
int num1[1], num2[1];
char ch;
do{
cout<< "Enter two integers separated by a space: ";
cin>> num1[1];
cin>> num2[1];


add(num1, num2);


subtract(num1, num2);



multiply(num1, num2);



devide(num1, num2);


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


return 1;
}


Mod Edit: Please use code tags when posting your code. Code tags are used like so => code.gif

Thanks,
PsychoCoder smile.gif

This post has been edited by PsychoCoder: 4 Sep, 2008 - 08:33 PM
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 4 Sep, 2008 - 08:34 PM
Post #5


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,933



Thanked 118 times

Dream Kudos: 8525

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:

  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do


Please don't start duplicate topics. Topics merged smile.gif
User is offlineProfile CardPM

Go to the top of the page

joske
post 5 Sep, 2008 - 04:52 PM
Post #6


D.I.C Head

**
Joined: 4 Sep, 2007
Posts: 157



Thanked 11 times
My Contributions


You have to change
CODE
cin>> num1[1];  
cin>> num2[1];

into

CODE
cin>> num1[0];  
cin>> num2[0];

as both num1 and num2 are arrays with one entry (you defined int num1[1], num2[1];), and array entries are zero based (you access the first element in an array by referring to index 0).
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 5 Sep, 2008 - 05:19 PM
Post #7


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 508



Thanked 45 times

Dream Kudos: 25
My Contributions


I'm not clear why num1 and num2 need to be arrays at all, actually.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:36AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month