C++ Functions and Structs
Name (First Last):
Week 5 Lab Assignment Upload to the Lab5 under D2L/Course Number/Activities/Assignments
Functions and Structs (30 points)
1. (4 points) Trace and predict the outcome of the following code fragments
|
|
Code Fragments |
Output |
|
Code Fragments |
Output |
|
a
|
|
|
b
|
|
|
|
c |
|
|
d |
|
|
2. (6 points, 1 point for each function, 2 points for the program) Writing functions. In this part, you should determine whether the function needs parameters, how many parameters the function needs, whether the function needs to return a value, and what value the function should return.
a. Define a function displayMessage() that displays the message "Welcome!" to the user.
// C++ code fragment for function definition
b. Define a function getPositive() to get a positive integer from the user.
//C++ code fragment for function definition
c. Define a function isOdd() to tell whether an integer is an odd number. The function returns true if the integer is odd, otherwise it returns false.
//C++ code fragment for function definition
d. Define a function calcSum() to calculate and return the summation of the numbers from 1 to a given integer.
//C++ code fragment for function definition
e. Write a program that uses all the above 4 functions. The program shall display "Welcome!" to the user, and then ask the user to input a positive integer. The program then shall tell if the above integer is odd or even. Eventually the program shall display summation of the numbers from 1 to the above integer.
Submission:
The program (C++ source code in the *.doc(x)) file), screenshot of the output, and *.cpp file
3. (5 points, 2.5 points each) Determine the parameters and returning value of your own functions.
a. Write a C++ program to compute the value of
x * (x + 1) + y * y + z * (z - 1)
where x, y, and z are 3 floating point numbers entered by the user. Requirements:
· You should have a function get3numbers() that asks the user to enter 3 numbers.
· You should have a function computeExp() that computes the value of the expression.
· Your main function should call the above 2 functions and get the job done.
· You must determine the parameters and returning values of all those functions.
Submission:
The program (C++ source code in the *.doc(x)) file), screenshot of the output, and *.cpp file
b. Write a C++ program to compute the value of
x * (x + 1) + y * y + z * (z - 1)
where x, y, and z are 3 floating point numbers entered by the user. Requirements:
· You should have a function get3numbers() that asks the user to enter 3 numbers.
· You should have a function computeExp() that computes the value of the expression.
· computeExp() should call get3numbers(). The main function should call computeExp().
· You must determine the parameters and returning values of all those functions.
Submission:
The program (C++ source code in the *.doc(x)) file), screenshot of the output, and *.cpp file
4. (5 points, 2.5 points each) Use functions and arrays to complete the following programs.
Requirements:
· You should use the divide-and-conquer strategy and write multiple functions.
· You should always use arrays for the data sequences.
· You should always use const int to define the sizes of your arrays.
a. Write a C++ program. The program first asks the user to enter 4 numbers to form Sequence 1. Then the program asks the user to enter 8 numbers to form Sequence 2. Finally, the program shall tell which sequence has a larger average. For example, if the user enters 3 4 5 6 for Sequence 1, and 0 1 2 3 4 5 6 7 for Sequence 2. Your program should know the average of Sequence 1 is 4.5, and the average of Sequence 2 is 3.5. Therefore, your program should conclude that Sequence 1 has a larger average.
Submission:
The program (C++ source code in the *.doc(x)) file), screenshot of the output, and *.cpp file
b. Given this array:
int sequence[10] = { 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 };
Write a C++ program to ask the user to enter a number, if the number can be found in the array, your program should display "Found". Otherwise, your program should display "Not found". For example, if the user enters 7, your program should display "Found". If the user enters 11, your program should display "Not found".
Submission:
The program (C++ source code in the *.doc(x)) file), screenshot of the output, and *.cpp file
5. (10 points, 1 point for each from a to e, 5 points for f) Define a structure and write functions. In this part, you should determine whether the function needs parameters, how many parameters the function needs, whether the function needs to return a value, and what value the function should return.
a. Define a struct, studentType, with four components: studentFName and studentLName of type string, testScore of type int (testScore is between 0 and 100), and grade of type char.
// C++ code fragment for function definition
b. Define a function to read the students’ data (studentData.txt) into the array
// C++ code fragment for function definition
c. Define a function to assign the relevant grade to each student.
// C++ code fragment for function definition
d. Define a function to find the highest test score.
// C++ code fragment for function definition
e. Define a function to print the names of the students having the highest test score. It must output each student’s name in this form: last name followed by a comma, followed by a space, followed by the first name; the name must be left justified.
// C++ code fragment for function definition
f. Write a program that uses the defined structure and all the above functions. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score.
Submission:
The program (C++ source code in the *.doc(x)) file), screenshot of the output, and *.cpp file