-
Making functions
I am currently studying C programming as part of my curriculum. I'm over half the way through the quarter and I have an A in the class, but I've hit a stone wall with this week's homework. This is what I have to do:
Write a C function named change() that accepts a single-precision number and the addresses of the integer variables named quarters, dimes, nickels, and pennies. The function should determine the number of quarters, dimes, nickels, and pennies in the number passed to it and write these values directly into the respective variables declared in its calling function.
I am not asking for people to make this for me, I only ask for help in understanding it, at this point I don't even know where to begin.
-
Do you understand all the sentences in the problem description? If not, which ones do you not?
Do you know how to make change?
-
I know that I have to convert an amount into number of quarters, dimes, nickels and pennies. I'm just confused on functions as a whole.
-
So what part of "the whole idea of functions" DO you know, and what particular part are you struggling with?
Can you get started on a function that does what you need here?
--
Mats
-
I don't know how to make a function, I've looked at examples in our books and could point out a function, but I haven't a clue on how to make it. Maybe you guys could give me an example with a different conversion of numbers; something like feet to inches and then I could piece it together from there?
-
There's nothing more to making a function than what you've got. Figure out what to do, and put it there. For instance, to convert inches to feet, you divide by twelve, hence:
Code:
float inches_to_feet(float inches) {
return inches/12.0f;
}
You put the code that you want to happen in the function, and you're done.
-
Have a look at http://www.cprogramming.com/tutorial/c/lesson4.html
Then give it a try - you can not learn how to ACTUALLY do programming without actively writing your own code - that would be like trying to learn how to write a book by reading good books.
--
Mats
-
thanks for the link, I'll take a look.
-
I don't know if this is of any help - it depends on what you already know.
This is a function called "printTheNumberFive". It simply prints out some words, then finishes. It "returns" nothing so it's type is 'void' - as in void printTheNumberFive. We don't 'pass' anything to the function so the brackets after the function name is empty. Before you can call the function, you need to declare and then define it.
Code:
#include <stdio.h>
// 1. declare the function
void printTheNumberFive();
int main (int argc, const char * argv[]) {
//3. Call the function
printTheNumberFive();
return 0;
}
//2. define the function
void printTheNumberFive() {
printf("\nThe number five");
}
This is a function which returns an integer, so it's type is 'int' as in int returnTheNumberFive. As before we do not 'pass' anything to the function, so again the brackets after the name are empty
Code:
#include <stdio.h>
// 1. declare the function
int returnTheNumberFive();
int main (int argc, const char * argv[]) {
//3. Call the function
int number = returnTheNumberFive();
printf("\nThe number returned was %d", number);
return 0;
}
//2. define the function
int returnTheNumberFive() {
return 5;
}
Finally, here is a function which also returns an integer, so its type is also int. Unlike the previous functions, this one is designed to take a number (an integer to be exact). Therefore the brackets after the function name now contain the words (int aNumber).
Code:
#include <stdio.h>
// 1. declare the function
int addOneToANumber(int aNumber);
int main (int argc, const char * argv[]) {
//3. Call the function (this time passing the number 6 to it)
int number = addOneToANumber(6);
printf("\nThe number returned was %d", number);
return 0;
}
//2. define the function
int addOneToANumber(int aNumber) {
int newNumber = aNumber + 1;
return newNumber;
}
Is this too simple or too complicated?
-
-
Which '^' did you mean master5001? The OP's post or the officedog post?
-
-
Nope. Not an assignment. Wrote it on the spot in attempt to help out.
-
Oh ok. I was going to say man your instructor really doesn't worry about insulting the intelligence of the class.
-
I did some studying and this is what I came up with. I am not sure what kind of setup I should have to find the remaining dimes, nickels and pennies. Please tell me if there is anything else wrong with the code, thanks.
Code:
#include <stdio.h>
void change(double, double *, double *, double *, double *);
double amount, quarters, dimes, nickels, pennies;
int main()
{
printf("Enter a dollar amount.");
scanf("%f", &amount);
change(amount, &quarters, &dimes, &nickels, &pennies);
printf("\nThe amount of quarters is: %6.2d\n", quarters);
printf("\nThe amount of dimes is: %6.2d\n", dimes);
printf("\nThe amount of nickels is: %6.2d\n", nickels);
printf("\nThe amount of pennies is: %6.2d\n", pennies);
return 0;
}
void change(double total, double *quarter, double *dime, double *nickel, double *penny)
{
*quart = total / .25;
*dime =
-
1. why do you need globals?
2. scanf("%f", &amount);
%f is for floats, %lf - is for doubles in the scanf format strings
3. %6.2d\n", quarters
%d is for ints, %f is for floats/doubles in the printf format strings
-
You might find it useful to read about the modulus operator (%).
http://www.cprogramming.com/tutorial/modulus.html
Also, it might be worth trying to describe in sentences (not code) what steps you must take to solve the problem.
Lastly, are doubles the best type of variable to store your results?
-
I barely even know what I'm doing...Asking me questions isn't helping me, I'm just going to reread my book and hopefully get answers. I know you guys are trying to help though, thanks for the effort.