Level 2015 C + + 4th week Project function

Source: Internet
Author: User
Tags cos gcd greatest common divisor sin

"Project 1-seeking greatest common divisor" reference solution
(1) Enter two numbers and find out the greatest common divisor

#include <iostream>usingnamespacestd;//自定义函数的原型(即函数声明)int main(){    int a,b,g;    cin>>a>>b;    g=gcd(a,b);    cout<<"最大公约数是: "<<g;    return0;}int gcd(int x,int//定义用于求两数的最大公约数的函数,函数只管求值。输出由main完成{}

(2) on the basis of the above program, add function GCDs function declaration and definition, achieve 4 greatest common divisor function

int gcds(int x,int y,int z,int w)   //调用gcd()求四数的最大公约数{ }

Tips:
① since the GCD function has been implemented to find the function of two number greatest common divisor, GCDs can call GCD to find two pairs of greatest common divisor, and then greatest common divisor greatest common divisor;
② Modify the main function to complete the test of the newly defined function.

(3) using the single-Step debugging tool, step into to the function of the internal observation program operation, master into the function "inside" to observe the operation of the method.

"Item 2-Too happy" reference solution
The words sin and cos are a couple. One day, sin went to hear crosstalk, cos at home. After a while, someone knocked at the door, and the Cos opened a look, is a polynomial function that does not know. Cos Q: Who are you? He said, "I am your husband, sin." Cos said: "You are not to listen to crosstalk?" How do you feel like this? Sin says, "Yes, it's fun!"
The story is finished. Here is the Taylor display of the sin function:
(Note: X takes a Radian value, not an angle value)
Using Sin Taylor to write a program, find the value of sin (π/2) and sin (56°), the precision required to reach 6 digits after the decimal point (that is, when the absolute value of the last item is less than 0.00001, the summation ends, the function of the absolute value can also be myabs implementation of the custom function).
Here is the program template, complete the declaration and definition of the custom function, and add the underlined part of the main function to complete.

 #include <iostream>  using  namespace  STD ; const  double  pi=3.1415926 ; int  Main () {cout  <<" sin (Π/2) is " <<mysin (Pi/2 ) <<endl; cout  << "sin (56°) value is"  <<_________    ____<<endl; return  0 ;} //define the Mysin function below to find the sin value  //define the Myabs function below to find the absolute value   

Tip 1: In fact, the C + + math library ( #include<Cmath> ) has already provided the sin and COS functions, in general, we call directly in the problem, and the subject requires the implementation of custom functions, for the sake of differentiation, respectively named Mysin and Mycos.
Tip 2: The output of the program should be:
The value of sin (Π/2) is 1
The value of sin (56°) is 0.829038

"Item 3-random number function applied to game"
(1) Guess Number game reference solution
Randomly generates a number within 1000 that asks the user to guess the integer. Enter a guessing integer to determine if it is equal to the resulting random number, and the result is judged by the screen display. If the guess is not right, give a "big" or "small" hint until you guess the number. (You can add a request and guess a few times to get the right result.) )
(2) Elementary school students ' arithmetic ability Test System reference solution
A program designed to help pupils perform arithmetic operations, which has the following functions: 10 Add, subtract, multiply, and divide four basic arithmetic operations, the operands in each problem are randomly generated, and the operand does not exceed the 2-digit positive integer; The practitioner enters his own answer according to the question displayed. The program automatically determines whether the answer entered is correct and displays the appropriate information. The last show has done several questions.

"Item 4-Program analysis"
Write down the running results of the program below and parse its operating mechanism. Please describe the process of its execution and the change of related variables, can be completed with the diagram, upload the picture in the blog.

(1)

#include<iostream>usingnamespacestd;void fun(int k){    if(k>0)        fun(k-1);    cout<<k;}int main(){    int w=5;    fun(w);    cout<<endl;    return0;}

(2)

#include <iostream>usingnamespacestd;void recur(char);int main(){    recur(‘0‘);}void recur(char c){    cout<<c;    if(c<‘5‘)        recur(c+1);    cout<<c;}

(3)

#include <iostream>using namespace STD;intFun2 (intAintb) {intC c=a*b%3;returnC;}intFUN1 (int&a,int&AMP;B) {intC    A+=a;    B+=b; C=fun2 (A, b);returnC*c;}intMain () {intx= One, y= +;cout&LT;&LT;FUN1 (x, y) <<endl;return 0;}

(4) Understand the default parameters of the function: Run the program, use the debugging function, observe the values of variables and function parameters, combined with classroom explanation, taste the role of default parameters.

#include <iostream>using namespace Std; Const Double Pi=3.1415926;float  Area(floatR=6.5);//Specifies the default value for R is 6.5float  Volume(floatHfloatR=6.5);//Specifies the default value for R is 6.5int main () {cout<< Area() <<endl;//equivalent to Area (6.5); cout<< Area(7.5) <<endl;the//parameter gets a value of 7.5, not 6.5cout<< Volume(45.6) <<endl;//equivalent to Volume (45.6,6.5)cout<< Volume(34.2,10.4) <<endl;The value of the //h value is 34.2,r 10.4    return 0; }float  Area(floatR) {returnPi*r*r; }float  Volume(floatHfloatR) {returnPi*r*r*h; }

① Remove the 4th line of "= 6.5" test, the cause of the error is __________;
② the 14th line to float area (float r=6.5), the reason for the error is ________;
③ the 5th line "float h,float r=6.5" to "float h=1,float R", the reason for the error is ___;
④ changed the 5th line to "float volume (float h=0,float r=6.5)", the change would be __________

"Item 5-solving with recursive methods" reference solution
(1) Write the recursive function to find the factorial of n (custom main function, call the defined recursive function)
(2) write a recursive formula for 1*3*...*n, and write a recursive function solution.
(3) programming, using recursive function to find out two numbers of greatest common divisor. (including the main function, calling the defined recursive function)
(4) A recursive function fib (int n) is programmed to return the nth Fibnacci number, which outputs the 20th number of the Fibnacci sequence.

#include <iostream>  usingnamespacestd;  int fib(int n);  int main(){     cout<<fib(20//输出     return0;        }  //返回Fibnacci序列中的第n个数  int fib(int n)  {}

Level 2015 C + + 4th week Project function

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.