Click here to Skip to main content
14,878,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#include <stdio.h>
 
void getInput(int* pNum1, int* pNum2);
void calc(int num1, int num2, int* pSum, int* pQuotient, int* pRemainder,
double* pHalfNum1, double* pHalfNum2, double* pFraction, int* pAlgebraic);
int intOps(int num1, int num2, int* pSum, int* pQuotient, int* pRemainder);
double doubleOps(int num1, int num2, double* pHalfNum1, double* pHalfNum2,
double* fraction);
int algebra(int num1, int num2);
void display(int num1, int num2, int sum, int quotient, int remainder,
double halfNum1, double halfNum2, double fraction, int algebraic);
 
 
int main(void)
{
   int num1, num2;
   int sum, quotient, remainder;
   double halfNum1, halfNum2, fraction;
   int algebraic;
 
 
   getInput(&num1, &num2);
   calc(num1, num2, &sum, "ient, &remainder, &halfNum1, &halfNum2,
   &fraction, &algebraic);
   display(num1, num2, sum, quotient, remainder,halfNum1, halfNum2,
   fraction, algebraic);
 
 
   return 0;
}
 
 
void getInput(int* pNum1, int* pNum2)
{
   printf("\nPlease enter two integers : ");
   scanf("%d%d", pNum1, pNum2);
}
 
void calc(int num1, int num2, int* pSum, int* pQuotient, int* pRemainder,
double* pHalfNum1, double* pHalfNum2, double* pFraction, int* pAlgebraic)
{
   intOps(num1, num2, pSum, pQuotient, pRemainder);
   doubleOps(num1, num2, pHalfNum1, pHalfNum2, pFraction);
   algebra(num1, num2);
}
 
 
int intOps(int num1, int num2, int* pSum, int* pQuotient, int* pRemainder)
{
   *pSum = num1 + num2;
   *pQuotient = num1 / num2;
   *pRemainder = num1 % num2;
}
 
 
double doubleOps(int num1, int num2, double* pHalfNum1, double* pHalfNum2,
double* pFraction)
{
   *pHalfNum1 = num1 / 2.0;
   *pHalfNum2 = num2 / 2.0;
   *pFraction = num1 / (double)num2;
}
 
 
int algebra(int num1, int num2)
{
   int algebraic;
   int x, y;
   x = num1;
   y = num2;
   algebraic = (2 * x) + (4 * y) + (x * y) - (x / y);
   return algebraic;
}
void display(int num1, int num2, int sum, int quotient, int remainder,
double halfNum1, double halfNum2, double fraction, int algebraic)
{
   printf("\n%20s%20s", "Description", "Data");
   printf("\n%20s%20s", "-----------", "----");
   printf("\n%20s%20d", "Input 1", num1);
   printf("\n%20s%20d", "Input 2", num2);
   printf("\n%20s%20d", "Sum", sum);
   printf("\n%20s%20.1lf", "Half of input 1", halfNum1);
   printf("\n%20s%20.1lf", "Half of input 2", halfNum2);
   printf("\n%20s%20d", "Quotient", quotient);
   printf("\n%20s%20d", "Remainder", remainder);
   printf("\n%20s%20.4lf", "Fraction", fraction);
   printf("\n%20s%20d", "Algebra", algebraic);
   printf("\n\n");
}


</stdio.h>

This is my code. It compiles and runs correctly except for the algebra method. That always returns a result of 0. What am i doing wrong?
Posted

you do not store the result of algebra anywhere
I guess you want to change the line algebra(num1, num2); to *pAlgebraic = algebra(num1, num2);
   
Your two other functions in calc are passing in variables by pointer that accept your answers:
<br />
int intOps(int num1, int num2, int* pSum, int* pQuotient, int* pRemainder)<br />
{<br />
   *pSum = num1 + num2;<br />
   *pQuotient = num1 / num2;<br />
   *pRemainder = num1 % num2;<br />
}<br />
<br />
double doubleOps(int num1, int num2, double* pHalfNum1, double* pHalfNum2,<br />
double* pFraction)<br />
{<br />
 *pHalfNum1 = num1 / 2.0;<br />
   *pHalfNum2 = num2 / 2.0;<br />
   *pFraction = num1 / (double)num2;<br />
}<br />

That is how they are storing the answers to print out.

your Algegra function takes in the values, calculates the result, and returns the result appropriately
<br />
int algebra(int num1, int num2)<br />
{<br />
  ...<br />
  return algebraic;<br />
}<br />

However, when you call it in the function calc, the result is not stored off into the intended field.

algebra(num1, num2);

Like the first answer suggests, you will want to change the call to this:

*pAlgebraic = algebra(num1, num2);

Before you make the change to correct this, place another instance of your printf to test with, in the algebra function, and you will see the correct answer:

printf("\n%20s%20d", "result inside of Algebra", algebraic);

Regards,
Paul
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900