代写C++代码 代做C++程序 C++辅导 C++教学

C++网络家教 远程写代码 在线Debug 讲解答疑 不是中介,本人直接写

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航

代写CS161 Lab Pass by Reference远程写代码debug辅导

1
CS 161 Lab #5 – Pass by Reference
• Start by getting checked off for (up to) 3 points of work from the previous lab in the first
10 minutes.
• Each lab will start with a recap of the last lab and a demo by the TAs of the new
concepts in the current lab. Therefore, this document does not cover everything that
will be covered. You are highly encouraged to ask questions and take your own notes.
• To get credit for this lab, you must be checked off by a TA by the end of lab.
Goals:
• Gain experience with the Proficiency Demo setup and type of questions
• Practice defining functions that use reference parameters
• Practice passing arguments by reference
(5 pts) A. Practice Proficiency Demo (50 minutes)
1. Once setup:
• Type vim to use vim, not just vi, and create test.cpp
• To compile your code, use g++ test.cpp -o test
• To run your program, use ./test
2. Get 2 questions from the TA. Choose one question you want to solve. All questions include
variables, user input, conditional statements, and repetition. You should be able to finish the
code within 50 minutes.
3. Scoring:
• 5 pts for fully coding the solution
• 3 pts for getting pretty far, but not finishing the if/else or loop logic
• 1 pt for making an attempt (if you get this grade, consider it a sign that you should
go to TA or instructor office hours and work to get comfortable writing programs
so you can pass the Proficiency Demo in week 10)
4. To get points for this section, ask a TA to check off your work.
(5 pts) B. Practice Passing Function Arguments by Reference
Create a file called lab5_ref.cpp with a main() function.
1. Above main(), define a function
int plus_10_value(int n)
{
...
}
that returns the value n + 10. Inside main(), read in an integer from the user and use your
function to update this number by adding 10 to it (calling your function with their input and
assigning the result back to the same variable).
2
For example:
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
number = plus_10_value(number);
cout << "Your number plus 10 is " << number << endl;
return 0;
}
2. Add a complete function header above your function. What are your function's preconditions (assumptions about what is true of the function parameters) and post-conditions
(return value and/or what is true about parameters after the function completes)?
/*******************************************************************
** Function: plus_10_value
** Description:
** Parameters:
** Pre-Conditions:
** Post-Conditions:
*******************************************************************/
3. Once that is working, write a second function that accomplishes the same thing, but instead
of returning the new value, it modifies the input parameter:
void plus_10_ref(int& n)
{
...
}
Note the void return type and the & next to the input parameter type (int&), indicating that it is
a reference (and can be modified inside the function). This function should update n directly
instead of returning the new value.
4. Update your main() function to have a second section where it reads a second value, m,
from the user and calls
plus_10_ref(m);
and prints m afterwards.
Notice that there is no assignment because there is no value returned from this function.
Run your program a few times with appropriate test cases to ensure that both functions work
correctly.
3
5. Questions to answer for yourself (discuss with a TA if you are stuck):
• If you remove the & from the parameter list for the definition of plus_10_ref(), what
happens?
• What are the pre-conditions and post-conditions for void plus_10_ref(int& n)?
(Fill in a function header for it as well.)
Ask a TA to check your work (get points) and submit your program to TEACH
1. Transfer your .cpp file from the ENGR servers to your local laptop.
2. Connect to TEACH here: https://teach.engr.oregonstate.edu/teach.php
3. In the menu on the right side, go to Class Tools -> Submit Assignment.
4. Select CS 161 020 Lab_5 from the list of assignments and click “SUBMIT NOW”.
5. Select your .cpp file (lab5_ref.cpp).
6. Click the Submit button.
7. You are done!
Point totals: 5 pts (practice proficiency demo) + 5 pts (references)
If you finish the lab early, this is a chance to work on your Assignment 3 implementation
(with TAs nearby to answer questions!).
 

相关推荐