1. x, f f (x)=2 x +1 0≤ ≤4 2,5.
. .. , ( 0; 2,5), . : - , - , while ( - ), for ( - ), do while ( - ).
:
, .. ;
=0 ;
h =2,5 ;
≤4 - .
f x, f .
setw(10) <iomanip>, .
- . 1.
. 1 - 1
-:
x, f
x=0
x≤4 0≤4
f=2x+1=20+1=1
x, f
x f 0 1 |
x=x+2,5=0+2,5=2,5
x≤4 2,5≤4
f=2x+1=22,5+1=6
x, f
x f 0 1 2,5 6 |
x=x+2,5=2,5+2,5=5
x≤4 5≤4
.
while:
// proga23while.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x, f;
cout<<setw(10)<<"x"<<setw(10)<<"f"<<endl;
x=0;
while(x<=4){
f=2.0*x+1.0;
cout<<setw(10)<<x<<setw(10)<<f<<endl;
x=x+2.5;
}
return 0;}
for:
// proga23for.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x, f;
cout<<setw(10)<<"x"<<setw(10)<<"f"<<endl;
for(x=0; x<=4; x=x+2.5){
f=2.0*x+1.0;
cout<<setw(10)<<x<<setw(10)<<f<<endl;
}
return 0;}
- . 2.
. 2 - 1
-:
x=0
f=2x+1=20+1=1
x, f
x f 0 1 |
x=x+2,5=0+2,5=2,5
x≤4 2,5≤4
f=2x+1=22,5+1=6
x, f
x f 0 1 2,5 6 |
x=x+2,5=2,5+2,5=5
x≤4 5≤4
.
do while:
// proga23dowhile.cpp: .
//
#include "stdafx.h"
|
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x, f;
cout<<setw(10)<<"x"<<setw(10)<<"f"<<endl;
x=0;
do{
f=2.0*x+1.0;
cout<<setw(10)<<x<<setw(10)<<f<<endl;
x=x+2.5;
}while(x<=4);
return 0;
}
while, for, do while:
:
: 0; 2,5.
x=0 f=2x+1=20+1=1
x=2,5 f=2x+1=22,5+1=6
2. x, Z -1≤ ≤2 1.
. Z , .
.
:
=-1 ;
h =1 ;
≤2 - .
Z x, Z .
setw() .
-: .
- while for.
- do while.
- . 3.
. 3 - 2
while:
// proga24while.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double x, Z;
cout<<setw(10)<<"x"<<setw(10)<<"Z"<<endl;
x=-1.0;
while(x<=2.0){
if(x<1){
Z=x+3;
}
else {
Z=4*x;
}
cout<<setw(10)<<x<<setw(10)<<Z<<endl;
x=x+1.0;
}
return 0;}
for:
// proga24for.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double x, Z;
cout<<setw(10)<<"x"<<setw(10)<<"Z"<<endl;
for(x=-1; x<=2; x=x+1){
if(x<1){
Z=x+3;
}
else {
Z=4*x;
}
cout<<setw(10)<<x<<setw(10)<<Z<<endl;
}
return 0;}
- . 4.
. 4 - 2
do while:
// proga24dowhile.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double x, Z;
cout<<setw(10)<<"x"<<setw(10)<<"Z"<<endl;
x=-1;
do{
if(x<1){
Z=x+3;
}
else {
Z=4*x;
}
cout<<setw(10)<<x<<setw(10)<<Z<<endl;
x=x+1;
}while(x<=2);
return 0;}
while, for, do while:
:
: -1; 0; 1; 2.
x=-1 Z={ }=x+3=-1+3=2;
x=0 Z={ }=x+3=0+3=3;
x=1 Z={ }=4x=41=4;
|
|
x=2 Z={ }=4x=42=8.
3. x, y, Z -1≤ ≤0,5 1, 5≤ y ≤15 5.
. : . , : , . Z x, y, Z.
:
=-1 ;
hx =1 ;
≤0,5 - ;
=5 ;
hy =5 ;
≤15 - .
- . 5.
. 5 - 3
while:
// proga25while.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double x, y, Z;
cout<<setw(10)<<"x"<<setw(10)<<"y"<<setw(10)<<"Z"<<endl;
x=-1;
while(x<=0.5){
y=5;
while(y<=15){
if(x*y<1){
Z=x+y;
}
else{
Z=x-y;
}
cout<<setw(10)<<x<<setw(10)<<y<<setw(10)<<Z<<endl;
y=y+5;
}
x=x+1;
}
return 0;}
for:
// proga25for.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double x, y, Z;
cout<<setw(10)<<"x"<<setw(10)<<"y"<<setw(10)<<"Z"<<endl;
for(x=-1; x<=0.5; x=x+1){
for(y=5; y<=15; y=y+5){
if(x*y<1){
Z=x+y;
}
else{
Z=x-y;
}
cout<<setw(10)<<x<<setw(10)<<y<<setw(10)<<Z<<endl;
}
}
return 0;}
- . 6.
. 6 - 3
(Visual Studio) do while ():
// proga25dowhile.cpp: .
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double x, y, Z;
cout<<setw(10)<<"x"<<setw(10)<<"y"<<setw(10)<<"Z"<<endl;
x=-1;
do{
y=5;
do{
if(x*y<1){
Z=x+y;
}
else{
Z=x-y;
}
cout<<setw(10)<<x<<setw(10)<<y<<setw(10)<<Z<<endl;
y=y+5;
}while(y<=15);
x=x+1;
}while(x<=0.5);
return 0;}
while, for, do while:
:
: -1; 0.
: 5, 10, 15.
x=-1 y=5 Z={ }=x+y=-1+5=4;
x=-1 y=10 Z={ }=x+y=-1+10=9;
x=-1 y=15 Z={ }=x+y=-1+15=14;
x=0 y=5 Z={ }=x+y=0+5=5;
x=0 y=10 Z={ }=x+y=0+10=10;
x=0 y=15 Z={ }=x+y=0+15=15.
. . , while . : do while for, for while .. .