Functions to add, subtract and multiply

Dec 21, 2012 at 12:16am
I'm a beginner with C program, I'm confused when it comes up to functions I tried to understand all the basics but still I couldn't get it.


I spent 2 hours trying to solve this question using stdio.h:

Produce a program which uses functions to add, subtract and multiply two numbers. Remember that a function should do only one job.

_____________________________________________________________


I did the adding part only, but I don't know how can I also let the program subtract and multiply at the same time


#include <stdio.h>

int addTwoNumbers(int, int);


int main()
{
int num1, num2;

printf("\nEnter the first number: ");
scanf("%d" , &num1);
printf("\nEnter the second number: ");
scanf("%d" , &num2);

printf("\nThe result is %d\n", addTwoNumbers(num1, num2));


}

int addTwoNumbers(int operand1, int operand2)
{
return operand1 + operand2;
}





Dec 21, 2012 at 12:31am
closed account (D80DSL3A)
Maybe it should do each separately?
1
2
3
printf("\n %d + %d =  %d\n", num1, num2, addTwoNumbers(num1, num2));
printf("\n %d - %d =  %d\n", num1, num2, subtractTwoNumbers(num1, num2));
printf("\n %d * %d =  %d\n", num1, num2, multiplyTwoNumbers(num1, num2));
Dec 21, 2012 at 1:01am
do you mean like this ::


#include <stdio.h>

int addTwoNumbers(int, int);
int subtractTwoNumbers(int, int);
int multiplyTwoNumbers(int, int);


int main()
{
int num1, num2;

printf("\n %d + %d = %d\n", num1, num2, addTwoNumbers(num1, num2));
printf("\n %d - %d = %d\n", num1, num2, subtractTwoNumbers(num1, num2));
printf("\n %d * %d = %d\n", num1, num2, multiplyTwoNumbers(num1, num2));


}

int addTwoNumbers(int operand1, int operand2)
{
return operand1 + operand2;
}


it didn't work
Dec 21, 2012 at 1:43am
closed account (D80DSL3A)
Here's a more C++ oriented solution.
Your prof will be impressed!
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
#include <iostream>
using namespace std;

struct mathOp {// abstract base class for our math functions
    static float x, y;
    char op;
    virtual float operator()(void)const = 0;
    mathOp(char Op):op(Op) {}
};

struct add: mathOp {
    add():mathOp('+') {}
    float operator()(void)const { return x+y; }
};
struct subtract: mathOp {
    subtract():mathOp('-') {}
    float operator()(void)const { return x-y; }
};
struct multiply: mathOp {
    multiply():mathOp('*') {}
    float operator()(void)const { return x*y; }
};
float mathOp::x = 1.0f;
float mathOp::y = 1.0f;

ostream& operator<<( ostream& os, const mathOp& rf ) {
    os << mathOp::x << rf.op << mathOp::y << " = " << rf() << '\n';
    return os;
}

int main()
{
    mathOp* pf[] = {new add, new subtract, new multiply};

    cout << "Enter x: "; cin >> mathOp::x;
    cout << "Enter y: "; cin >> mathOp::y;
    for(int i = 0; i < 3; ++i )
        cout << *pf[i];

    for(int i = 0; i < 3; ++i )
        delete pf[i];

    cout << endl;
    return 0;
}
Dec 21, 2012 at 1:50am
I need the answer using stdio.h please
Dec 21, 2012 at 2:12am
closed account (D80DSL3A)
No problem at all...
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
#include <stdio.h>

struct mathOp {
    static int x, y;
    char op;
    virtual int operator()(void)const = 0;
    mathOp(char Op):op(Op) {}
};
struct add: mathOp {
    add():mathOp('+') {}
    int operator()(void)const { return x+y; }
};
struct subtract: mathOp {
    subtract():mathOp('-') {}
    int operator()(void)const { return x-y; }
};
struct multiply: mathOp {
    multiply():mathOp('*') {}
    int operator()(void)const { return x*y; }
};
int mathOp::x = 1;
int mathOp::y = 1;

int main()
{
    mathOp* pf[] = {new add, new subtract, new multiply};
    printf("\nEnter the first number: ");
    scanf("%d",&mathOp::x);
    printf("\nEnter the second number: ");
    scanf("%d",&mathOp::y);
    for(int i = 0; i < 3; ++i )
        printf( "\n %d %c %d = %d", mathOp::x, pf[i]->op, mathOp::y, (*pf[i])() );

    for(int i = 0; i < 3; ++i ) delete pf[i];
    return 0;
}
Dec 21, 2012 at 7:56am
i just hope the prof doesn't ask any questions :P
Dec 21, 2012 at 1:57pm
I guess those answers confused you even more.
Basically you need to write three diff funcs here and inside the main they have to be called three times.
To multiply include this :

int mulTwoNumbers(int operand1, int operand2)
{
return (operand1 * operand2);
}


and to subtract use this

int subTwoNumbers(int operand1, int operand2)
{
return (operand1 - operand2);
}

Now the writing of functions part is over.

Modify the main as :

int main()

{


int num1, num2;
printf("\nEnter the first number: ");
scanf("%d" , &num1);
printf("\nEnter the second number: ");
scanf("%d" , &num2);
printf("\nThe sum is %d\n", addTwoNumbers(num1, num2));
printf("\nThe product is %d\n", mulTwoNumbers(num1, num2));
printf("\nThe difference is %d\n", subTwoNumbers(num1, num2));

}


You could have combined these two scanfs and these printfs into one.
I guess you are clear now.
Dec 21, 2012 at 5:47pm
closed account (D80DSL3A)
@The Protostar.
I considered simply pointing out the missing function definitions, but his reply to my first post seemed so clueless that I decided to investigate his (brief) post history to help me decide on a response. It appears to me that he is here looking only for completely coded solutions. When he gets one, he nukes the content of his posts and departs the thread without thanking the code poster.

I was hoping that he might actually submit the code I gave. I think he deserves to get caught for cheating if he is submitting code that he doesn't even look at.

Oh well, maybe next time.
Last edited on Dec 21, 2012 at 5:49pm
Dec 22, 2012 at 10:07am
Since it was my first post here I did not think about it like that.
But in that case I am with you.

Dec 23, 2012 at 4:13am
O dear stop making up a story about me :|, I'm not cheating at all I'm new with all the programming stuff plus I'm an international student who's studying foundation at the moment so I was hoping to learn as much as I can about c++ and C# before going to uni any way thanks for all the help. about deleting the previous questions its because I didn't get the answers so I said whats the point of leaving the question !!

and by the way once you gave me the code I tried to understand it as much as I can, spent 3 hours on you're answer..
Dec 23, 2012 at 4:37am
closed account (D80DSL3A)
My apologies then. We do get a good number of threads seeking solutions to homework problems and it looked to me that's what it was.

I hope you got your problem solved .
Dec 23, 2012 at 5:05am
yeah I did thanks to you and all the others but I need to practice more I'm trying to understand as much as i can I just bought my self a book "C for absolute beginners" it wasn't that much helpful as I was expecting.
Dec 23, 2012 at 5:13am
closed account (D80DSL3A)
There are many good sources for learning.
The solution you had going was fine, just incomplete.

You can ignore my solution involving the array of pointers to virtual function objects. It works, but all that isn't necessary.
Dec 26, 2012 at 12:14am
I answered the question just like you said but my program still not working


#include <stdio.h>

int addTwoNumbers(int, int);
int subTwoNumbers(int, int);
int mulTwoNumbers(int, int);


int main()
{
int num1, num2;

printf("\nEnter the first number");
scanf("%d", &num1);
printf("\nEnter the second number");
scanf("%d", num2);
printf("\nThe sum is %d\n", addTwoNumbers(num1, num2));
printf("\nThe product is %d\n", mulTwoNumbers(num1, num2));
printf("\nThe difference is %d\n", subTwoNumbers(num1, num2));

}




int addTwoNumbers(int operand1, int operand2)
{
return operand1 + operand2;
}

int subtractTwoNumbers(int operand1, int operand2)
{
return operand1 - operand2;
}

int multiplyTwoNumbers(int operand1, int operand2)
{
return operand1 * operand2;
}
Dec 26, 2012 at 12:52am
You declared mulTwoNumbers, and then defined multiplyTwoNumbers.
Last edited on Dec 26, 2012 at 12:53am
Topic archived. No new replies allowed.