![]() |
| | #1 |
| Registered User Join Date: Feb 2005 Location: unf
Posts: 16
| problems storing values in varibles Code: #include <stdio.h>
#include <math.h>
void getInput(int input1, int input2);
void calc(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
void intOps(int input1, int input2, int sum, int iDiv, int iMod);
void floatOps(int input1, int input2, float half1, float half2, float frac);
double raiseToPower(int input1, int input2);
void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
int main(void)
{
int input1;
int input2;
int sum, iDiv, iMod;
float half1, half2, frac;
double power;
getInput(input1, input2);
calc(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
display(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
system("pause");
return 0;
}//end of main
/*******************************************************************************
Name: getInput()
Purpose: This function prompts the usr for two integers and stores them in memory
*******************************************************************************/
void getInput(int input1, int input2)
{
printf("%20s", "Sean Todd");
printf("\nPlease enter two integers: ");
scanf("%d%d", &input1, &input2);
printf("%20d%20d", input1, input2);
}//end of getInput()
/*******************************************************************************
Name: calc()
Purpose: This function takes the data the usr entered and calculates the sum,
the half of each number entered
*******************************************************************************/
void calc(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power)
{
printf("\n%20d %20d", input1, input2);
intOps(input1, input2, sum, iDiv, iMod);
floatOps(input1, input2, half1, half2, frac);
power = raiseToPower(input1, input2);
}//end of calc()
/****************************************************************************
Name: intOps()
Purpose: perform all integer calculations
****************************************************************************/
void intOps(int input1, int input2, int sum, int iDiv, int iMod)
{
sum = input1 + input2;
iDiv = input1 / input2;
iMod = input1 % input2;
}//end of intOps
/****************************************************************************
Name: floatOps()
Purpose: perform all integer calculations
****************************************************************************/
void floatOps(int input1, int input2, float half1, float half2, float frac)
{
half1 = (float) input1 / (2.0);
half2 = (float) input2 / (2.0);
frac = (float) input1 / (float) input2;
}//end of floatOps()
/****************************************************************************
Name: raiseToPower()
Purpose: perform all integer calculations
****************************************************************************/
double raiseToPower(input1, input2)
{
return pow(input1, input2);
}//end of raiseToPower()
/*******************************************************************************
Name: display()
Purpose: outputs the data to screen in report format
*******************************************************************************/
void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power)
{
printf("\n%20s%20s", "Description", "Data");
printf("\n%20s%20s", "-----------", "----");
printf("\n%20s%20d", "Input1", input1);
printf("\n%20s%20d", "Input2", input2);
printf("\n%20s%20d", "Sum", sum);
printf("\n%20s%20.1f", "Half of Input1", half1);
printf("\n%20s%20.1f", "Half of Input2", half2);
printf("\n%20s%20d", "Quotient", iDiv);
printf("\n%20s%20d", "Remainder", iMod);
printf("\n%20s%20.4f", "Fraction", frac);
printf("\n%20s%20.0f", "Power", power);
printf("\n\n");
}//end of display()
Last edited by stodd04; 02-08-2005 at 06:03 AM. |
| stodd04 is offline | |
| | #2 |
| Handy Andy Join Date: Dec 2004
Posts: 540
| You are confused. This Code: void getInput(int input1, int input2); Hopefully this little sample will clear some stuff up: Code: #include <stdio.h>
void myFunc1(int, int); //by value
void myFunc2(int&, int&); //by reference
int main(void) {
int x=1, y=1;
printf("In main() x= %d, y= %d", x, y);
printf("\nCalling myFunc1\n");
myFunc1(x, y); //pass by value
printf("\nIn main() x= %d, y= %d", x, y);
printf("\nCalling myFunc2\n");
myFunc2(x, y); //pass by reference
printf("\nIn main() x= %d, y= %d\n", x, y);
return 0;
}
void myFunc1(int a, int b) {
//we have copies of x and y here
a = a + 1;
b = b + 1;
printf("In myFunc1() x= %d, y= %d", a, b);
}
void myFunc2(int& c, int& d) {
c = c + 1;
d = d + 1;
printf("In myFunc2() x= %d, y= %d", c, d);
}
__________________ i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem |
| andyhunter is offline | |
| | #3 |
| Registered User Join Date: Jul 2004
Posts: 68
| What Compiler you using I got choked by warnings and errors. pow requres doubles Code: double pow(double x, double y); Code: #include <stdio.h>
#include <math.h>
void getInput(int *input1, int *input2);
void calc(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
void intOps(int input1, int input2, int sum, int iDiv, int iMod);
void floatOps(int input1, int input2, float half1, float half2, float frac);
double raiseToPower(int input1, int input2);
void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
int main(void)
{
int input1;
int input2;
int sum, iDiv, iMod;
float half1, half2, frac;
double power;
getInput(&input1, &input2);
calc(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
display(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
return 0;
}/* end of main */
/************************************************** *****************************
Name: getInput()
Purpose: This function prompts the usr for two integers and stores them in memory
************************************************** *****************************/
void getInput(int *input1, int *input2)
{
printf("%20s", "Sean Todd");
printf("\nPlease enter two integers: ");
scanf("%d%d", input1, input2);
printf("%20d%20d", *input1, *input2);
}/* end of getInput() */
/************************************************** *****************************
Name: calc()
Purpose: This function takes the data the usr entered and calculates the sum,
the half of each number entered
************************************************** *****************************/
void calc(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power)
{
printf("\n%20d %20d", input1, input2);
intOps(input1, input2, sum, iDiv, iMod);
floatOps(input1, input2, half1, half2, frac);
power = raiseToPower(input1, input2);
}/* end of calc() */
/************************************************** **************************
Name: intOps()
Purpose: perform all integer calculations
************************************************** **************************/
void intOps(int input1, int input2, int sum, int iDiv, int iMod)
{
sum = input1 + input2;
iDiv = input1 / input2;
iMod = input1 % input2;
}/* end of intOps */
/************************************************** **************************
Name: floatOps()
Purpose: perform all integer calculations
************************************************** **************************/
void floatOps(int input1, int input2, float half1, float half2, float frac)
{
half1 = (float) input1 / (2.0);
half2 = (float) input2 / (2.0);
frac = (float) input1 / (float) input2;
}/* end of floatOps() */
/************************************************** **************************
Name: raiseToPower()
Purpose: perform all integer calculations
************************************************** **************************/
double raiseToPower(int input1, int input2)
{
return pow((double)input1, (double)input2);
}/* end of raiseToPower() */
/************************************************** *****************************
Name: display()
Purpose: outputs the data to screen in report format
************************************************** *****************************/
void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power)
{
printf("\n%20s%20s", "Description", "Data");
printf("\n%20s%20s", "-----------", "----");
printf("\n%20s%20d", "Input1", input1);
printf("\n%20s%20d", "Input2", input2);
printf("\n%20s%20d", "Sum", sum);
printf("\n%20s%20.1f", "Half of Input1", half1);
printf("\n%20s%20.1f", "Half of Input2", half2);
printf("\n%20s%20d", "Quotient", iDiv);
printf("\n%20s%20d", "Remainder", iMod);
printf("\n%20s%20.4f", "Fraction", frac);
printf("\n%20s%20.0f", "Power", power);
printf("\n\n");
}/* end of display() */
|
| gsoft is offline | |
| | #4 |
| Registered User Join Date: Feb 2005 Location: unf
Posts: 16
| thanks for your responses... I am using bloodshed dev-c++ in c mode... for the class it has to compile on gcc which in past projects has not been a problem |
| stodd04 is offline | |
| | #5 |
| Registered User Join Date: Feb 2005 Location: unf
Posts: 16
| for some reason I cannot pass a reference in the function prototype. so I am going to give the pointers a shot |
| stodd04 is offline | |
| | #6 |
| Registered User Join Date: Feb 2005 Location: unf
Posts: 16
| pointers didnt do the trick either... gonna try gcc to see if there is a difference |
| stodd04 is offline | |
| | #7 |
| Just Lurking Join Date: Oct 2002
Posts: 5,006
| >pointers didnt do the trick either You need to do the same in a lot more places. Code: void getInput(int *input1, int *input2);
void calc(int input1, int input2, int *sum, int *iDiv, int *iMod, float *half1, float *half2, float *frac, double *power);
void intOps(int input1, int input2, int *sum, int *iDiv, int *iMod);
void floatOps(int input1, int input2, float *half1, float *half2, float *frac);
double raiseToPower(int input1, int input2);
void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
int main(void)
{
int input1;
int input2;
int sum, iDiv, iMod;
float half1, half2, frac;
double power;
getInput(&input1, &input2);
calc(input1, input2, &sum, &iDiv, &iMod, &half1, &half2, &frac, &power);
display(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
return 0;
}/* end of main */
__________________ 7. It is easier to write an incorrect program than understand a correct one. 40. There are two ways to write error-free programs; only the third one works.* |
| Dave_Sinkula is offline | |
| | #8 |
| Registered User Join Date: Feb 2005 Location: unf
Posts: 16
| thank ya'll very much for all of your help. I finally figured out passing by reference with pointers with great help from the many wonderful examples. attached is the final code. thanks for the support |
| stodd04 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Storing values in memory | Opel_Corsa | C Programming | 5 | 09-29-2006 03:10 AM |
| Storing a String with values | dgibson2004 | C Programming | 2 | 05-10-2006 11:39 AM |
| Help with storing letters in varibles | cgsarebeast | C++ Programming | 9 | 05-09-2006 06:03 PM |
| Storing values in arrays | Ripper1 | C++ Programming | 3 | 08-25-2003 11:04 AM |
| help storing and calculating hex values | anzas | C++ Programming | 4 | 07-14-2003 01:41 PM |