Im using dev c++ and Im trying to make a program that uses pointers to add, subtract, multiply and divide. So far here is what I've got:
/*
*/
/*
Function: This program asks the user to input two number in which it will
calculate sum, difference, multiplication and division using basic
arithmetic (+,-,*,/) It will use pointers to pass the values inputed
by the user by reference.
*/
#include <stdio.h>
#include <stdlib.h>
void add( int *num1, int *num2); /* prototype with pointer definition */
void subtract(int *num1, int *num2); /* prototype with pointer definition */
void multip(int *num1, int *num2);/* prototype with pointer definition */
void division(int *num1, int *num2);/* prototype with pointer definition */
int main( void )
{ /*begins the main function */
int firstNum; /* this will be the first number the user will input */
int secNum; /* this will be the second number the user will input */
/* We first ask the user to input the first and second number */
printf("please enter the first number: "); /* Prompt */
scanf("%d", &firstNum); /*reads an integer*/
printf("please enter the second number: ");
scanf("%d", &secNum); /*reads an integer*/
/* Loop WHILE, user enters 0, keep asking user to enter an integer greater than ZERO*/
while(secNum == 0){ /* begins the while loop */
printf("please enter an integer number greater than 0 : ");
scanf("%d", &secNum); /*reads an integer */
} /* ends the while loop */
/*Pass address of firstNum and secNum to add */
add( &firstNum, &secNum);
printf("The Sum of the two numbers is: %d \n",add);
/*Pass address of firstNum and secNum to subtract */
subtract( &firstNum, &secNum);
printf("The Difference of the two numbers is: %d \n",subtract);
/*Pass address of firstNum and secNum to multip */
multip( &firstNum, &secNum);
printf("The Product of the two numbers is: %d \n",multip);
/*Pass address of firstNum and secNum to division */
division( &firstNum, &secNum);
printf("The Quotient of the two numbers is: %d \n",division);
system("PAUSE");
return 0; /* indicates succeful termination*/
}/* terminates the main function */
/*definitiion of the function add*/
void add(int *num1, int *num2)
{
*add = (*num1)+(*num2); /* Will return the sum */
}
/*definitiion of the function subtract*/
void subtract(int *num1, int *num2, int *answer)
{
*subtract = (*num1)-(*num2); /* Will return the difference */
}
/*definitiion of the function multip*/
void multip(int *num1, int *num2, int *answer)
{
*multip = (*num1)*(*num2); /* Will return the Product */
}
/*definitiion of the function division*/
void division(int *num1, int *num2, int *answer)
{
*division = (*num1)/(*num2); /*Will return the quotient*/
}
Note: I defined a result variable to place the answer in . But This is not what I was suppose to do.. Apparently I can only have two pointer prototypes.
If any one knows what Im missing I could really use somehelp. Thanks!
In pascal, the way you return values from a function is to "set the function name equal
to the return value." For example,
{ Ok, my pascal syntax is rusty. I can't remember where the semicolons go or if there
should be commas separating the parameters...}
function add( a: integer; b : integer ) : integer;
begin
add := a + b;
end;
hey , I have already tried to use this but Im still not getting any results.
this is what I did:
/*
Function: This program asks the user to input two number in which it will
calculate sum, difference, multiplication and division using basic
arithmetic (+,-,*,/) It will use pointers to pass the values inputed
by the user by reference.
*/
#include <stdio.h>
#include <stdlib.h>
void add( int *num1, int *num2); /* prototype with pointer definition */
void subtract(int *num1, int *num2); /* prototype with pointer definition */
void multip(int *num1, int *num2);/* prototype with pointer definition */
void division(int *num1, int *num2);/* prototype with pointer definition */
int main( void )
{ /*begins the main function */
int firstNum; /* this will be the first number the user will input */
int secNum; /* this will be the second number the user will input */
/* We first ask the user to input the first and second number */
printf("please enter the first number: "); /* Prompt */
scanf("%d", &firstNum); /*reads an integer*/
printf("please enter the second number: ");
scanf("%d", &secNum); /*reads an integer*/
/* Loop WHILE, user enters 0, keep asking user to enter an integer greater than ZERO*/
while(secNum == 0){ /* begins the while loop */
printf("please enter an integer number greater than 0 : ");
scanf("%d", &secNum); /*reads an integer */
} /* ends the while loop */
/*Pass address of firstNum and secNum to add */
add( &firstNum, &secNum);
printf("The Sum of the two numbers is: %d \n",add );
/*Pass address of firstNum and secNum to subtract */
subtract( &firstNum, &secNum);
printf("The Difference of the two numbers is: %d \n",subtract);
/*Pass address of firstNum and secNum to multip */
multip( &firstNum, &secNum);
printf("The Product of the two numbers is: %d \n",multip);
/*Pass address of firstNum and secNum to division */
division( &firstNum, &secNum);
printf("The Quotient of the two numbers is: %d \n",division );
system("PAUSE");
return 0; /* indicates succeful termination*/
}/* terminates the main function */
/*definitiion of the function add*/
void add(int *num1, int *num2)
{
return = *num1 + *num2; /* Will return the sum */
}
/*definitiion of the function subtract*/
void subtract(int *num1, int *num2, int *answer)
{
return = (*num1)-(*num2); /* Will return the difference */
}
/*definitiion of the function multip*/
void multip(int *num1, int *num2, int *answer)
{
return = (*num1)*(*num2); /* Will return the Product */
}
/*definitiion of the function division*/
void division(int *num1, int *num2, int *answer)
{
return = (*num1)/(*num2); /*Will return the quotient*/
}
You need to be able to return a value. I'm not sure if int* add will work, maybe it will.
Please use code tags in future. All you have to do is click the # button on the right of the screen and paste your code in between the [code] and [/code] tags...