You will also revisit file I/O.
Part I – Understanding Pointers
Create a new project and add an empty file named yourLastName_lab6.txt to your project by choosing the File à New à Empty File option and when prompted, make sure to add the file to your project. Click OK when you are prompted to select the Debug and Release targets. Store your answers to the following questions in this text file. You will use this text file to store answers in Part I in this lab.
1. Given the following code, answer the following questions (a-h) assuming that these statements have been fully executed. See the example output text file at the end of this handout for the format of your answers.
int x = 8, y = 3;
int *xp = NULL;
int *yp = NULL;
xp = &x;
yp = &y;
*xp = *xp * *yp;
*yp = *xp + *yp;
a. Which variables store integer numbers?
b. Which variables store addresses?
c. What is stored in x?
d. What is stored in y?
e. What is stored in xp?
f. What is stored in yp?
g. What is the result of *xp?
h. What is the result of *yp?
2. Given the following declarations:
int m = 12, n = 56;
int *ip;
Describe the errors, if any, in each of the following statements. Make sure to use the letters (a, b, c, etc.) for your answers in your text file.
a. printf( \"\ *ip = %d\ \", *ip);
b. m = &n;
c. *ip = m;
d. *ip = &m;
Part II – Using Pointers as Output Parameters for Functions
In your project for this lab, create a new function called from main that calculates the surface area and volume of a cylinder. Pass the computed values back to main through the parameter list by using the following declaration:
void computeProperties( float radius, float height, float *area,
float *volume)
In main, prompt the user to enter a radius and the height of the cylinder. Since both the radius and height must be positive, use two separate input validation loops after getting each value and ensure that the entered value is greater than 0. Once the radius and height have been successfully entered, call the computeProperties function to calculate the required output. You will need to declare the necessary variables before this function call. After the function call, output the radius, height, and calculated values returned by the function. Display all values with 4 decimal places. Note that no output is performed within the function; it calculates the values only; main prints the output.
Part III – Using Text Files for Input and Output
Download the data file inputdata.txt from Pilot and add it to your project (easiest to add an empty file as described above in Part I and copy/paste the content from Pilot). This data file contains a list of pairs of floating point numbers. Each pair is on a separate line. The first line in the file contains the number of pairs that follow.
Create a new function in your project named readInputFile that opens inputdata.txt and reads the number of pairs to be read (i.e., the first line of the input file). It should then use a for loop to read each pair of numbers. After the number pair has been read, call a new computeValues function to compute their sum and their difference (see next paragraph for the function requirements). Your readInputFile function should then print the sum and difference to the console, with each number pair and its sum and difference on a single line (use 4 digits of precision in your output).
The computeValues function is a void function with 4 parameters – the two numbers read, and output parameters for the sum and difference. When computing the difference, subtract the second number in each pair from the first number; some answers may be negative.
Summary of the processing sequence for Part III: main calls readInputFile which opens the input file. Remember to check that the file is successfully opened. If successful, read the first line from the input file. Using that number in the control of your loop, use a for loop to read each pair of numbers. For each pair you read, call computeValues to calculate the sum and difference of these two numbers. Print the two numbers, the sum, and difference in your for loop for that pair of numbers back in readInputFile. Continue processing each pair of numbers until you’ve processed and printed results for all pairs. Your program will then return to main (since main called readInputFile). Back in main, there is nothing else to do, so your program should end normally.
Reminder: Be sure to fill in the required commenting in your program before submitting it. Check the Style Requirements document for specifics on commenting a function with output parameters.
You will also revisit file I/O.
Part I – Understanding Pointers
Create a new project and add an empty file named yourLastName_lab6.txt to your project by choosing the File à New à Empty File option and when prompted, make sure to add the file to your project. Click OK when you are prompted to select the Debug and Release targets. Store your answers to the following questions in this text file. You will use this text file to store answers in Part I in this lab.
1. Given the following code, answer the following questions (a-h) assuming that these statements have been fully executed. See the example output text file at the end of this handout for the format of your answers.
int x = 8, y = 3;
int *xp = NULL;
int *yp = NULL;
xp = &x;
yp = &y;
*xp = *xp * *yp;
*yp = *xp + *yp;
a. Which variables store integer numbers?
b. Which variables store addresses?
c. What is stored in x?
d. What is stored in y?
e. What is stored in xp?
f. What is stored in yp?
g. What is the result of *xp?
h. What is the result of *yp?
2. Given the following declarations:
int m = 12, n = 56;
int *ip;
Describe the errors, if any, in each of the following statements. Make sure to use the letters (a, b, c, etc.) for your answers in your text file.
a. printf( \"\ *ip = %d\ \", *ip);
b. m = &n;
c. *ip = m;
d. *ip = &m;
Part II – Using Pointers as Output Parameters for Functions
In your project for this lab, create a new function called from main that calculates the surface area and volume of a cylinder. Pass the computed values back to main through the parameter list by using the following declaration:
void computeProperties( float radius, float height, float *area,
float *volume)
In main, prompt the user to enter a radius and the height of the cylinder. Since both the radius and height must be positive, use two separate input validation loops after getting each value and ensure that the entered value is greater than 0. Once the radius and height have been successfully entered, call the computeProperties function to calculate the required output. You will need to declare the necessary variables before this function call. After the function call, output the radius, height, and calculated values returned by the function. Display all values with 4 decimal places. Note that no output is performed within the function; it calculates the values only; main prints the output.
Part III – Using Text Files for Input and Output
Download the data file inputdata.txt from Pilot and add it to your project (easiest to add an empty file as described above in Part I and copy/paste the content from Pilot). This data file contains a list of pairs of floating point numbers. Each pair is on a separate line. The first line in the file contains the number of pairs that follow.
Create a new function in your project named readInputFile that opens inputdata.txt and reads the number of pairs to be read (i.e., the first line of the input file). It should then use a for loop to read each pair of numbers. After the number pair has been read, call a new computeValues function to compute their sum and their difference (see next paragraph for the function requirements). Your readInputFile function should then print the sum and difference to the console, with each number pair and its sum and difference on a single line (use 4 digits of precision in your output).
The computeValues function is a void function with 4 parameters – the two numbers read, and output parameters for the sum and difference. When computing the difference, subtract the second number in each pair from the first number; some answers may be negative.
Summary of the processing sequence for Part III: main calls readInputFile which opens the input file. Remember to check that the file is successfully opened. If successful, read the first line from the input file. Using that number in the control of your loop, use a for loop to read each pair of numbers. For each pair you read, call computeValues to calculate the sum and difference of these two numbers. Print the two numbers, the sum, and difference in your for loop for that pair of numbers back in readInputFile. Continue processing each pair of numbers until you’ve processed and printed results for all pairs. Your program will then return to main (since main called readInputFile). Back in main, there is nothing else to do, so your program should end normally.
Reminder: Be sure to fill in the required commenting in your program before submitting it. Check the Style Requirements document for specifics on commenting a function with output parameters.
You will also revisit file I/O.
Part I – Understanding Pointers
Create a new project and add an empty file named yourLastName_lab6.txt to your project by choosing the File à New à Empty File option and when prompted, make sure to add the file to your project. Click OK when you are prompted to select the Debug and Release targets. Store your answers to the following questions in this text file. You will use this text file to store answers in Part I in this lab.
1. Given the following code, answer the following questions (a-h) assuming that these statements have been fully executed. See the example output text file at the end of this handout for the format of your answers.
int x = 8, y = 3;
int *xp = NULL;
int *yp = NULL;
xp = &x;
yp = &y;
*xp = *xp * *yp;
*yp = *xp + *yp;
a. Which variables store integer numbers?
b. Which variables store addresses?
c. What is stored in x?
d. What is stored in y?
e. What is stored in xp?
f. What is stored in yp?
g. What is the result of *xp?
h. What is the result of *yp?
2. Given the following declarations:
int m = 12, n = 56;
int *ip;
Describe the errors, if any, in each of the following statements. Make sure to use the letters (a, b, c, etc.) for your answers in your text file.
a. printf( \"\ *ip = %d\ \", *ip);
b. m = &n;
c. *ip = m;
d. *ip = &m;
Part II – Using Pointers as Output Parameters for Functions
In your project for this lab, create a new function called from main that calculates the surface area and volume of a cylinder. Pass the computed values back to main through the parameter list by using the following declaration:
void computeProperties( float radius, float height, float *area,
float *volume)
In main, prompt the user to enter a radius and the height of the cylinder. Since both the radius and height must be positive, use two separate input validation loops after getting each value and ensure that the entered value is greater than 0. Once the radius and height have been successfully entered, call the computeProperties function to calculate the required output. You will need to declare the necessary variables before this function call. After the function call, output the radius, height, and calculated values returned by the function. Display all values with 4 decimal places. Note that no output is performed within the function; it calculates the values only; main prints the output.
Part III – Using Text Files for Input and Output
Download the data file inputdata.txt from Pilot and add it to your project (easiest to add an empty file as described above in Part I and copy/paste the content from Pilot). This data file contains a list of pairs of floating point numbers. Each pair is on a separate line. The first line in the file contains the number of pairs that follow.
Create a new function in your project named readInputFile that opens inputdata.txt and reads the number of pairs to be read (i.e., the first line of the input file). It should then use a for loop to read each pair of numbers. After the number pair has been read, call a new computeValues function to compute their sum and their difference (see next paragraph for the function requirements). Your readInputFile function should then print the sum and difference to the console, with each number pair and its sum and difference on a single line (use 4 digits of precision in your output).
The computeValues function is a void function with 4 parameters – the two numbers read, and output parameters for the sum and difference. When computing the difference, subtract the second number in each pair from the first number; some answers may be negative.
Summary of the processing sequence for Part III: main calls readInputFile which opens the input file. Remember to check that the file is successfully opened. If successful, read the first line from the input file. Using that number in the control of your loop, use a for loop to read each pair of numbers. For each pair you read, call computeValues to calculate the sum and difference of these two numbers. Print the two numbers, the sum, and difference in your for loop for that pair of numbers back in readInputFile. Continue processing each pair of numbers until you’ve processed and printed results for all pairs. Your program will then return to main (since main called readInputFile). Back in main, there is nothing else to do, so your program should end normally.
Reminder: Be sure to fill in the required commenting in your program before submitting it. Check the Style Requirements document for specifics on commenting a function with output parameters.
1. Given the following code, answer the following questions (a-h) assuming that these statements have been fully executed. See the example output text file at the end of this handout for the format of your answers.
int x = 8, y = 3;
int *xp = NULL;
int *yp = NULL;
xp = &x;
yp = &y;
*xp = *xp * *yp;
*yp = *xp + *yp;
a. Which variables store integer numbers?
The variables x, and y will store integer numbers.
b. Which variables store addresses?
The variables xp, and yp will store addresses.
c. What is stored in x?
An integer 8 is stored in x.
d. What is stored in y?
An integer 3 is stored in y.
e. What is stored in xp?
Initially NULL is store in xp. But after 4th step, the address of x is stored in it.
f. What is stored in yp?
Initially NULL is store in yp. But after 5th step, the address of y is stored in it.
g. What is the result of *xp?
Initially, *xp is invalid. After 4th step *xp is 8, and after 6th step, *xp is 24.
h. What is the result of *yp?
Initially, *yp is invalid. After 5th step *yp is 3, and after 7th step, *yp is 27.