Question

Exercise #1: Write a C program that contains the following steps (make sure all variables are...

Exercise #1:

Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work.

  1. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not.
  2. Commenting out the existing coding, write the code to divide a two digit integer number by 10 (pick any number you want between 10 and 99; hard code it right into the program rather than input it). Output the result as an integer. Try this with a few different initial numbers. For example, 59 would give an output of 5.
  3. Next write the code to find the remainder when a two digit number (again, hard code it into your program) is divided by 10 [note that this is done in a single statement]. Output the result as an integer. Try this with a few different numbers. For example, 59 would give an output of 9.
  4. Comment the code from steps 4 and 5 out of your program, and put the coding to find the number of 10's in a number into a user defined (helper) function named f1. Set the initial number in a variable named x in the main function and print it out to check that it was set correctly). Your code will then pass x to a variable named y in f1 - the syntax for the call is of course f1( x ); . (That is, you are copying the value in the argument variable x into the parameter variable y.) f1 should return the number of 10's in y. After f1 returns, the main function should print the returned value. You should see the original number printed from the main function and the returned number of 10's in that original number, also printed from the main function. Try this with a couple of different initial numbers.
  5. Repeat step 6 but this time put the coding of the step 5 into another user defined (helper) function named f2. Again use y as the name of the parameter in f2; it will not get confused with the y in f1 - they are declared in separate functions and are thus completely different variables. (f1 has its own y and f2 has its own y.) f2 should return the remainder when the passed (copied) number in y is divided by 10. After the setting of the initial number and printing it to check it, the main function should invoke (call) f2, and after f2 returns the main function should print the returned value. You should see the original number printed from the main function and the returned remainder when that original number is divided by 10, also printed from the main function. Try this with a couple of different initial numbers.
  6. Comment out the calls for f1 and f2. Keep the assignment of a value to x and the print to check that it was properly set. Write a void (no return value will be given) function f3 which will be passed the address of your initial number (x). The syntax for the call to this function is, of course f3(&x); The function f3 will have a single parameter, named x_address, and that parameter must be able to hold the ADDRESS of an integer value, that address being copied from the &x. Check with you notes Ch 3 slide 7 to see how this should be declared.
  7. By dereferencing an address (a pointer) we are accessing the value AT that address (either getting it or setting it). Inside f3, dereference the parameter x_address (the dereference syntax is "*x_address", with no quotes of course) and then (still inside f3) print out the result of the dereferencing. You will see that you have obtained the value of x, which is in the main function. After returning, the main function should print out x again. All three prints of x (the original check in the main, inside f3 after dereferencing, and in the main after returning from f3) will be the same - all three printfs will be printing the value of the x which is located in the memory area belonging to the main function. Be sure to label each printing of x with a few words so you can tell them apart on your output.
  8. Modify f3 to add 4 to x ensuring that in the main function you are printing out the value of x both before and after the call to f3. Drop the print inside f3.
  9. Again note that x is in the main function (it's a word - at some location - in the memory area associated only with the main function). You want to change the value of x, but you need to do that work in the function f3. f3 has its own memory area, but it can't directly use the variables in the main function's memory area (where x lives). BUT, in f3 you DO have the address of where x is located in the main function's memory area. To emphasize, x is in the main function, x_address is in function f3. You have that address of x inside f3 because it was copied ("passed") into x_address when in the call to f3 you passed &x as an argument. So to access x (either to get it or to set it) in f3 you have to dereference the x_address. Again, by dereferencing the x_address in the f3 code, you are working (in f3) with the memory word x which is only in the main function.
  10. Important aside: It is important to recognize that using addresses (pointers) is not directly dependent on whether or not you are using functions. If you had both x and x_address declared in a main function and were to put the address of x into the variable x_address in the main function code with the statement x_address = &x; , then the following two lines are absolutely equivalent in the main function and you could use either: x = x + 4; and *x_address = *x_address + 4;
  11. Why do we go through all this rigamarole with addresses when we want to change a value in a function? Because in C (and many other computer languages) argument values are only ever **passed into** (copied into) function parameters. Nothing (!!) is EVER "passed back". So if you just copied in x to y, as in the earlier steps of this lab, you could change y all you wanted in the function... and x in the main function would still be the same old x it always was. You would only be changing the COPY of x in f3 (y would be the copy as you used it in the earlier steps 6 or 7). So, if we want a change to be made to a value in the main function, we pass the address of WHERE we want the change made (in the main function) to the helper function (such as f3); the helper function accesses that address/location in the main function's memory, which it does have [of course it does, we passed that address to the function] and the called function (f3) makes the change there (in the calling main function). Note also that using a return statement to bring something "back" is a completely separate issue, and it can only return a single value - whereas in a great many cases far more than one item from a function is wanted. In those cases, such as you did in this step and will also do in the step below, you have to pass an address and then use dereferencing in the called function.
  12. Now that you've seen how to pass in an address of a variable to a function and in that function use that address to make a change to the variable, back in the calling (main in this case) function, you can comment all the preceding function calling and write and call another void function f4 which has three parameters. The first, an integer variable named y, will be passed the initial number x from the main function, as seen above. The other two parameters will receive addresses from the call. The second parameter, named addr1, will be used to receive the address of a variable named num_of_tens which you will declare in the main function. The third parameter, named addr2, will be used to receive the address of a variable named remainder also declared in the main function . In the main function, the value will first be set for x, as above, and then f4 is to be called, passing it the value of x and the value of the address of num_of_tens (written as &num_of_tens) and the value of the address of remainder (written as &remainder). Neither num_of_tens or remainder have had anything put into them at the point where f4 is called. In fact, nothing in the main function will set them. f4 will, using the code you wrote earlier, determine the number of 10s in y (copied from x) and the remainder when y is divided by 10. By dereferencing addr1 and addr2, f4 will then set these calculated values into the variables num_of_tens and remainder (they are both in the main function's memory area or as we often term in "they are in the main function") ... and then f4 will quit. Nothing is returned, nothing is "sent back". You passed in two addresses, made changes at those two addresses, and then the function is finished and control passes back to the main function. Immediately after the call to f4, in the main function write code so that it will print out the values of num_of_tens and remainder, properly labeled. Try this with a few different initial numbers.
  13. You final program should look like this. The user enters a number between 10 and 99 (validated), then the program calls the function f3 that gives both the quotient and the remainder of a division by 10. The two values are printed out (not in the f3 function, but in the main program).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:-

#include <stdio.h>

/*int f1(int y);
int f2(int y);
void f3(int&x);*/
void f4(int x,int& addr1,int& addr2);
int main()
{
int x,num_of_tens,remainder;
/*Question a //Prompt for a number between 10-99 and loop until correct*/
do {
printf("Enter a 2 digit number: ");
scanf("%d", &x);
} while (x < 10 || x>99);
/*Question b//Hard code x value a 2 digit number and display the value divided by 10
x = 59;
printf("%d\n", (x / 10));*/
/*Question c//Hard code x value a 2 digit number and display remainder while it divided by 10
x = 59;
printf("%d\n", (x % 10));*/
/*Question d//Find number of tens using function
x = 59;
printf("Value of x before function call = %d\n", x);
printf("Number of 10s in %d = %d\n", x,f1(x));
printf("Value of x after function call = %d\n", x);
//Question e/Find remainder using function
printf("Value of x before function call = %d\n", x);
printf("Remainder by dividing 10 of %d = %d\n", x, f2(x));
printf("Value of x after function call = %d\n", x);*/
x = 59;
/*question f
printf("Value of x before function call = %d\n", x);
printf("Value of x using function call = ");
f3(x);
printf("Value of x after function call = %d\n", x);*/
/*question g &h
printf("Value of x before function call = %d\n", x);
f3(x);
printf("Value of x after function call = %d\n", x);*/
//Call f4 to get number of tens and remainder
f4(x,num_of_tens,remainder);
printf("Value of x = %d\n", x);
printf("Number of 10s in %d = %d\n", x, num_of_tens);
printf("Remainder by dividing 10 of %d = %d\n", x, remainder);
return 0;
}
/*
Function: f1
ParamIn: y, value of x from main
ParamOut: Value of 10's
Description: Function take value of x from main and return number of 10's
*/
int f1(int y) {
return y / 10;
}
/*
Function: f2
ParamIn: y, value of x from main
ParamOut: Remainder while dividing by 10
Description: Function take value of x from main and return remainder by dividing by 10
*/
int f2(int y) {
return y % 10;
}
/*
Function: f3
ParamIn: &x, Address of the integer x
ParamOut: None
Description: Function print the value of x address pointing
*/
/*void f3(int& x) {
printf("%d\n", *&x);
}*/
/*
Function: modified f3
ParamIn: &x, Address of the integer x
ParamOut: None
Description: add 4 with x address pointing integer
*/
/*void f3(int& x) {
*&x += 4;
}*/

/*
Function: f4
ParamIn: x, Integer
&addr1, address of num_of_tens
&addr2, address of remainder
ParamOut: None
Description: Calculate number of 10's and remainder and change the corresponding address values
*/
void f4(int x, int& addr1, int& addr2) {
*&addr1 = x / 10;
*&addr2 = x % 10;
}

Add a comment
Know the answer?
Add Answer to:
Exercise #1: Write a C program that contains the following steps (make sure all variables are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C programming language Question 1 a) through m) Exercise #1: Write a C program that...

    Using C programming language Question 1 a) through m) Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. a. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. b....

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • Salary Lab In this lab you are going to write a time card processor program. Your...

    Salary Lab In this lab you are going to write a time card processor program. Your program will read in a file called salary.txt. This file will include a department name at the top and then a list of names (string) with a set of hours following them. The file I test with could have a different number of employees and a different number of hours. There can be more than 1 department, and at the end of the file...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • 11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl...

    11p Python Language Read the following code for oofraction. import oofraction class OOFraction: def main(): fl = oofraction.o0Fraction( 2, 5) f2 = oofraction.o0Fraction ( 1, 3) f3 = f1 + f2 print (str(3)) main() def __init__(self, Num, Den): self.mNum = Num self.mDen = Den return def_add_(self,other): num = self.mNumother.mDen + other.mNum*self.mDen den = self.mDen*other.mDen f = OOFraction(num,den) return f 1. Write the output below. def_str_(self): s = str( self.mNum )+"/" + str( self.mDen) returns 2. Write a class called wholeNum...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT