Level 1 C ++ 2015 week project functions

Source: Internet
Author: User

Level 1 C ++ 2015 week project functions

[Project 1-calculate the maximum public approx]
(1) enter two numbers and obtain their maximum common approx.

# Include
  
   
Using namespace std; // the prototype of the custom function (that is, the function declaration) int main () {int a, B, g; cin> a> B; g = gcd (a, B); cout <"the maximum public approx. is:" <
   
  

(2) Based on the above program, add the Declaration and definition of the function gcds to realize the function of finding the maximum number of 4 Public approx.

<Code class = "hljs cpp"> <code class = "hljs cs"> int gcds (int x, int y, int z, int w) // call gcd () calculate the maximum number of four public approx. {}</code> </code>

Tip:
① Since the gcd function has implemented the function of finding the maximum common divisor of two numbers, gcds can call gcd to find the maximum common divisor of the two pairs respectively, and then calculate the maximum common divisor of the maximum common divisor;
② Modify the main function to test the new function.

(3) using the single-step debugging tool, step into observe the program running in the function, and master the methods for entering the function "internal" to observe its running status.

Project 2-joy]
Sin and cos are a couple. One day, sin went to listen to crosstalk and cos was at home. After a while, someone knocked on the door and cos opened the door. It was an unknown polynomial function. Cos Q: Who are you? He said: I am your husband sin. Cos said: Aren't you going to listen to crosstalk? How can this image be taken? Sin said: Yes, it's so happy!
The story is over. The Taylor extension of the sin function is as follows:
(Note: x takes the radian value instead of the angle value)
Compile a program using sin Taylor, and obtain the values of sin (π/2) and sin (56 °, the precision must be six digits after the decimal point (that is, when the absolute value of the last item is less than 0.00001, the accumulation ends, and the function for calculating the absolute value can also be implemented by the custom function myabs ).
The following is a program template. Complete the Declaration and definition of the custom function and complete the underline section of the main function.

<Code class = "hljs cpp"> <code class = "hljs cpp"> # include <iostream> using namespace std; const double pi = 3.1415926; int main () {cout <"sin (π/2) the value is "<mysin (pi code =" "return ="> </mysin (pi> </iostream> </code>

Tip 1: in fact, the C ++ Math Library (#include .
Tip 2: The program output should be:
The value of sin (π/2) is 1.
The sin (56 °) value is 0.829038

Project 3-random number Function Applied to games]
(1) Guess digital games
A random number within 1000 is generated, which requires the user to guess. Enter a hypothetical integer to determine whether it is equal to the generated random number. The result is displayed on the screen. If you do not guess correctly, the system will prompt "big" or "small" until you can guess this number. (You can add another requirement and guess several times to get the correct result .)
(2) Pupils Arithmetic Ability Test System
Design a program to help primary school students perform arithmetic operations. It has the following functions: provides 10 basic arithmetic operations, such as addition, subtraction, multiplication, and division, the operands in each question are random integers and the operands do not exceed 2 digits. The practitioners enter their answers based on the displayed questions, the program automatically checks whether the entered answer is correct and displays the corresponding information. The last few questions are displayed.

Project 4-Program Analysis]
Write the running result of the following program and parse its running mechanism. Describe the process of execution and the change of related variables. You can use the diagrams to complete the process.

(1)

<code class="hljs cpp"><code class="hljs cpp"><code class="hljs cpp">#include<iostream>using namespace std;void fun(int k){    if(k>0)        fun(k-1);    cout<<k; code="" int="" return="" w="5;"></k;></iostream></code></code></code>

(2)

<code class="hljs cpp"><code class="hljs cpp"><code class="hljs cpp"><code class="hljs cpp">#include <iostream>using namespace std;void recur(char);int main(){    recur('0');}void recur(char c){    cout<<c; code=""></c;></iostream></code></code></code></code>

(3)

<code class="hljs cpp"><code class="hljs cpp"><code class="hljs cpp"><code class="hljs cpp"><code class="hljs cpp">#include<iostream>using namespace std;int fun2(int a,int b){    int c;    c=a*b%3;    return c;}int fun1(int &a,int &b){    int c;    a+=a;    b+=b;    c=fun2(a,b);    return c*c;}int main(){    int x=11,y=19;    cout<<fun1(x,y)<<endl; code="" return=""></fun1(x,y)<<endl;></iostream></code></code></code></code></code>

(4) understand the default parameters of a function: run the program, use the debugging function, observe the values of the variables and function parameters, and combine them with the classroom instructions to taste the role of the default parameters.

<Code class = "hljs cpp"> <code class = "hljs cpp"> <code class = "hljs rsl"> # include <iostream> using namespace std; const double pi = 3.1415926; float area (float r = 6.5); // The default value of the specified r is 6.5 float volume (float h, float r = 6.5 ); // set the default value of r to 6.5 int main () {cout <area () <endl; 10.4 = "" 6.5 = "" code = "" float = "" return = ""> </area () <endl;> </iostream> </code>

① Try to remove "= 4th" from the 6.5 rows. The cause of the error is:__________;
② Change row 14th to "float area (float r = 6.5)". The cause of the error is________;
③ Change "float h, float r = 5th" to "float h = 1, float r". The cause of the error is:___;
④ Change the number of rows to "float volume (float h = 0, float r = 5th)". The change will be__________

Project 5-solving with recursive Methods]
(1) Write a recursive function to obtain the factorial of n (custom main function, call the defined recursive function)
(2) calculate 1*3 *... * Recursive formula of n, and write the recursive function to solve the problem.
(3) Compile the program and use recursive functions to obtain the maximum common approx. of two numbers. (Including compiling the main function and calling the defined recursive function)
(4) Compile the recursive function fib (int n) to return the nth Number of fiber nacci, so as to output 20th of the total number of fiber nacci sequences.

<code class="hljs cpp"><code class="hljs cpp"><code class="hljs cpp"><code class="hljs cpp"><code class="hljs cpp"><code class="hljs rsl"><code class="hljs cpp">#include <iostream>  using namespace std;  int fib(int n);  int main(){     cout<<fib(20)<<endl; code="" int="" return=""></fib(20)<<endl;></iostream></code></code></code></code></code></code></code>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.