Experiment 5

2020-12-07 14:20:55

1. Write function fact(int n) Realize the demand for n！, Enter... In the main function m and n, call fact( ) Function finding Value .

2. To write fun Function to find the absolute value of the difference between two numbers , Return the difference to the main function , Write a complete program implementation function
Data call ;
#include<stdio.h>
#include<stdlib.h>
int fact(int n);
#include<stdio.h>
int fun(int a,int b);
int main()
{
int a,b,y;
printf(" Input a Value ：\n");
scanf("%d",&a);
printf(" Input b Value ：\n");
scanf("%d",&b);
y=fun(a,b);
printf(" Output y=%d:",y);
system("pause");
return 0;
}
int fun(int a,int b)
{
int x;
if(a-b>=0){
x=a-b;
}else{
x=b-a;
}
return x;

} 3. Programming , Ask the user to enter a string of integers （ Store this string of integers in an array ）, And then by calling
selection_sort Function to sort these integers . In the given n After an array of elements ,selection-sort The function must be
The following work has to be done ：
(a) Search the array to find the largest element
(b) Recursively call the function itself to the front n-1 Array elements to sort

4. Write a function to calculate the values of the following polynomials ：
3x 5 +2x 4 -5x 3 -x 2 +7x-6
Programming requires user input x Value , Call this function to calculate the value of the polynomial and display the value returned by the function

5.
（1） Write function power, Use the formula x n =x*x n- Calculation x n
（2） If you calculate it in another way xn,power Functions can be faster . If n yes 2 The power of , You can get through
Calculate by self multiplication xn, for example ,x4 yes x2 The square of , therefore x4 You can multiply it twice , Without the need for
Triple multiplication . This method can even be used for n No 2 Omega to the power of omega , If n It's even , You can use the formula , Such as
fruit n Is odd , be x n =x.x n-1 Write recursive functions for computation

6. Write a function to simulate the game of dice （ Two dice ）, On the first throw , If the sum of the points is 7 or
11 Then win , If the sum of the points is 2、3、12 It's a failure , In other cases, the sum of the points is called “ The goal is ”, Game continued
To continue . In the subsequent throw , If the player throws the target point again , Then win , Throw 7 It's a failure , Everything else Ignore , The game goes on , At the end of each game , The program asks the user whether to play again , If the user inputs back
The answer is not ’y’ or ‘Y’, The program will show the number of wins and losses and then stop
You rolled:8
You rolled:3
You rolled: 10
You rolled: 8
You win!
Play again? y
You rolled:6
You rolled:5
You rolled:12
You rolled :3
You rolled:7
You lose!
Play again? y
You rolled:11
You win!
Play again? n
Wins:2 Losses 1
Write three functions ：main、roll_dice and play_game. Here are the prototypes of the last two functions ：
int roll_dice(void);
bool play_game(void);
roll_dice Two random numbers should be generated （ Everyone is in 1-6 Between ）, And return to their and .play_game Should be carried out
A game of dice （ call roll_dice Determine the number of points per roll ）, If the player wins, return to true, If
If the player is defeated, he will return false.play_game The function also displays the result of each roll .main The function repeats
call play_game function , Record the number of wins and losses , And display “you win” and ”you lose” eliminate
Rest . Tips ： Use rand Function to generate random numbers ,

https://chowdera.com/2020/12/202012071417069813.html