Iv. conclusion of the experiment
1.
① function declarations and functions define their respective roles, the difference between the two;
function declaration:
int Fun (intint b);
function definition:
int Fun (int A,int b) { int C; C=a+b; return c; }
A function declaration is like the head of a function definition, and when a function is called, if the function definition is used in front of a function call, execution to the part of the call cannot be called, and an error is made, then a function declaration needs to be added before the function call, or the function definition is written before the function call.
② what is a formal parameter? What is an argument? What does function parameters and return values do in a function?
Formal parameters, called formal arguments, are parameters that are used when defining function names and body functions to receive the arguments passed when the function is called.
An argument is called an actual parameter, which is the argument passed to the function at the time of invocation, that is, the value passed to the called function.
A function parameter is a parameter passed in a function operation or a function call.
The function return value is the result of the return of the function after it has been executed.
What is the difference between value passing and reference passing during ③ function parameter passing?
Value passing is only passing values
The reference is passed, the memory address is passed, and the memory address corresponding to the stored value is changed after the modification.
2.
(1) Exercise 2-28
① to judge with If...else statement
#include <iostream>
char x;
while (x!= ' Q ')
{
cin>>x;
if (x== ' A ')
cout<< "Data has been increased" <<endl;
else if (x== ' D ')
cout<< "Data has been deleted" <<endl;
else if (x== ' S ')
cout<< "Data has been excluded" <<endl;
}
}
② using the switch statement
#include <iostream>using namespacestd; intMain () {Charx; cout<<"menu:a (DD) D (elete) S (ORT) Q (uit), Select one:"; while(x!='Q') {cin>>x; Switch(x) { Case 'A': cout<<"Data has been increased"<<endl; Break; Case 'B': cout<<"Data has been deleted"<<endl; Break; Case 'S': cout<<"Data has been excluded"<<endl; Break; } } return 0; }
(2) Exercise 2-29
①
Prime numbers are in the natural number greater than 1, except 1 and it is no longer the number of other factors.
The prime number should first be greater than 1, then the number is removed from 2 to the number-1, and if it is not divisible, it is prime.
Ii
#include <iostream>using namespace std; int isprime (int x) {int ret=1;int i;for (i=2;i<x;i++) {if (x%i==0) {ret=0;break;}} if (x<=1) ret=0;return ret;} int main () { int x=1;; while (x<=100) {if (IsPrime (x) ==1) cout<<x<< "; x + +; } return 0;
(3) Exercise 2-32
#include <iostream>#include<cstdlib>#include<ctime>using namespacestd;intMain () {Srand (0)); intA=rand ()% -; ints; intCount=0; while(1) {cout<<"Please enter the number you want to guess"; CIN>>s; if(s>a) {cout<<"it's too big."<<Endl; Count++; } Else if(s<a) {cout<<"it's too small."<<Endl; Count++; } Else{Count++; cout<<"Guess right!" ?"<<Endl; Break; } } return 0;}
(4) Exercise 2-34
①
#include <iostream>#include<iomanip>using namespacestd;intMain () {enumColor{red,yellow,blue,white,black}; Color pri; inti,j,k,n=0, Loop; for(i=red;i<=black;i++) { for(j=red;j<=black;j++) { if(i!=j) { for(k=red;k<=black;k++) { if(k!=i&&k!=j) {N++; cout<<SETW (3) <<N; for(loop=1; loop<=3; loop++) { Switch(loop) { Case 1:p Ri=color (i); Break; Case 2:p Ri=color (j); Break; Case 3:p Ri=color (k); Break; default: Break; } Switch(PRI) { CaseRED:COUT<<SETW (8) <<"Red"; Break; CaseYELLOW:COUT<<SETW (8) <<"Yellow"; Break; CaseBLUE:COUT<<SETW (8) <<"Blue"; Break; CaseWHITE:COUT<<SETW (8) <<" White"; Break; CaseBLACK:COUT<<SETW (8) <<"Black"; Break; }} cout<<Endl; } } } } } return 0;}
Five, the experiment summary and experience
In C + +, to output control character length, you need to use the stew () function, and add #include <iomanip> header files.
In question fourth, color (i) is a forced type conversion, so that the PRI value is i.
To generate a random number, you need to use the Srand and Rand functions while adding #include<cstdlib> and #include <ctime> header files
For example:
#include <iostream>
#include <cstdlib>
#include <ctime>
int main ()
{
Srand (Time (0));
INT A=rand ()%;
return 0;
}
Where A is a random integer of 1~100.
C + + Simple programming-2