1.6.1 1-
1 , . , , , . .
// // int Sum=0; for (i=0; i<n; i++) Sum+=a[i]; cout<< = <<Sum/n; } |
1.6.2 2-
2 , . :
int R=a[I];a[I]=a[J]; a[J]:=R; // a[I] a[J]
1. . // for(int i=0,j=n-1;i<j;i++,j--) {int r=a[i]; a[i]=a[j]; a[j]=r;} // 2. : 1 2, 3 4, . . for(int i=0;i<n-1;i+=2) {int r=a[i]; a[i]=a[i+1]; a[i+1]=r;} 3. (). int k,i,t,r; cout<<"K=";cin>>k; for(t=0;t<k;t++) { r=a[0]; for(int i=0;i<n-1;i++) a[i]=a[i+1]; a[n-1]=r; } |
1.6.3 3-
3 , . .
1. n . c, c[i]=a[i]+b[i]. for (int i=0; i<n; i++) c[i] = a[i] + b[i]; 2. . int b[10]; // int i,j=0; for(i=0;i<n;i++) if(a[i]<0){b[j]=a[i];j ++;} // b for(i=0;i<n;i++) if(a[i]>=0){b[j]=a[i];j++;} // b for(i=0;i<n;i++) cout<<b[I]<< ; 3. . int b[10]; int i,j=0; for(i=0;i<n;i++) if(a[i]%2!=0){b[j]=a[i];j++;} for(i=0;i<j;i++) cout<<b[i]<<" "; cout<<"\n"; |
1.6.4 4-
4 , , . . :
;
, .
1. . int k; cout<<"K=?";cin>>k; int ok=0; // int i, nom; for(i=0;i<n;i++) if(a[i]==k){ok=1; nom=i; break;} if(ok==1) cout<<"nom="<<nom; else cout<<"\nthere is no such element!"; |
1.7 ++.
|
|
1. .
#include iostream using namespace std; void main() {const int n=10; int i; double A[n],SA; // cout<<Vvesti elementi massiva:; for(i=0; i<n; i++) { out<<A[<<i<<]=; cin>>A[i]; } SA=0; for(i=0; i<n; i++) SA=SA+A[i]; // SA=SA/n; out<< = <<SA; } |
2. . .
#include iostream using namespace std; void main() {const int n=10; int i; int A[n],nomer, min; // cout<<Vvesti elementi massiva:; for(i=0; i<n; i++) { out<<A[<<i<<]=; cin>>A[i]; } min = A[0]; for(i=1; i<n; i++) if (A[i]<min){ min = A[i]; nomer = i;} out<<Minimalnij element = <<min<<endl; out<<Nomer elementa = <<nomer<<endl; } |
3. .
#include iostream #include <iomanip.h> using namespace std; void main() {const int n=12; int i, j; int A[n]; // cout<<Vvesti elementi massiva:<<endl; for(i=0; i<n; i++) { out<<A[<<i<<]=; cin>>A[i]; } cout<<Element<<setw(13)<<Znachenie<<setw(17)<<Bar graph<<endl; for(i=0; i<n; i++) {cout<<setw(7)<<i<<setw(13)<<A[i]<< ; for(j=1; j<=A[i]; j++) cout<<*; // cout<<endl; } } : Vvesti elementi massiva: 12 3 25 7 11 5 14 4 16 2 19 3 Element Znachenie Bar graph 0 12 ************ 1 3 *** 2 25 ************************* 3 7 ******* 4 11 *********** 5 5 ***** 6 14 ************** 7 4 **** 8 16 **************** 9 2 ** 10 19 ****************** 11 3 *** |
4. , , .
#include iostream using namespace std; void main() {const int n=10; int i; int A[n], kol_pol, kol_otr, kol_num; // cout<<Vvesti elementi massiva:; for(i=0; i<n; i++) { out<<A[<<i<<]=; cin>>A[i]; } kol_pol = kol_otr = kol_nul = 0; // for(i=1; i<n; i++) if (A[i]==0){ kol_nul = kol_nul + 1; } cout<<Kolichestvo nulevih elimentov = << kol_nul<<endl; // for(i=1; i<n; i++) if (A[i]<0){ kol_otr = kol_otr + 1; } cout<<Kolichestvo otricatelnih elimentov = << kol_otr<<endl; // for(i=1; i<n; i++) if (A[i]>0){ kol_pol = kol_pol + 1; } cout<<Kolichestvo polojitelnih elimentov = << kol_pol<<endl; } |
5. 10 ; 3 7 5. . .
|
|
#include iostream using namespace std; void main() { int i; int A[10]; // cout<<Vvesti elementi massiva:; for(i=0; i<10; i++) { out<<A[<<i<<]=; cin>>A[i]; } // 2 6 5 for(i=2; i<7; i++) A[i] = A[i] + 5; // for(i=0; i<10; i++) if (A[i]%2==0) A[i]= - A[i]; // for(i=0; i<10; i++) out<<A[<<i<<]<<A[i]<<endl; } |
6. .
( . bubble sorting) , . , . , 0 . , (.. ), .
#include iostream #include <iomanip.h> using namespace std; void main() {const int n = 10; int A[n]={9, - 6, 89, 4, 7, - 4, 95, 2, 3, 12}; int t; // cout<<Elements in the initial order:<<endl; for(int i=0; i<n; i++) out<< setw(5) << A[i]; for(int pass=n-1; pass>1; pass--) for(i=0; i<pass; i++) if (A[i]>A[i+1]) {t = A[i]; A[i] = A[i+1]; A[i+1] = t; } // cout<<endl<<Elements in the ascending order:<<endl; for(int i=0; i<n; i++) out<< setw(5) << A[i]; cout<<endl; } : Elements in the initial order: 9 -6 89 4 7 -4 95 2 3 12 Elements in the ascending order: -6 -4 2 3 4 7 9 12 89 95 |
7. .
#include iostream using namespace std; void main() {const int n = 10; int a[n]={1, 3, -9, -5, 1, -2, 1, -3, 8, 4}; int i, imax, imin, kol; for(i = imax = imin = 0; i<n; i++) {if (a[i]>a[imax]) imax=i; if (a[i]<a[imin]) imin=i;} cout<<max = << a[imax] << min = << a[imin] << endl; int ibeg = imax < imin? imax: imin; int iend = imax < imin? imin: imax; cout<<ibeg = << ibeg << iend = << iend << endl; for(kol = 0; i = ibeg + 1; i < iend; i++) if (a[i]>0) kol++; cout<<endl<<The number of positive elements: << kol<<endl; } : max = 8 min = - 9 ibeg = 2 iend = 8 The number of positive elements: 2 |
8. A n, .
. k k . , k, Sg : .
, A= {1, 0, 2, 4, 0}, P = 8, k = 3,
. 2. i ( 5 9) . , , (k>0 ), . " ". Sg (double), .. .
|
|
|
9. X Y. Z X Y X.
. X n, Y m, , , Z m+n/2 . Z (new) m+[n/2] ([n/2] ). k .
X , , .. i 1 n 2. X[i]>0. Y , .. i 0 m 1. Y[i]> X[0].
. 3.
|
10. , .
|
|
#include iostream using namespace std; void main() {int i, j; int A[5][5]; // cout<<Vvesti jlementi massiva:; for(i=0; i<5; i++) for(j=0; j<5; j++) { out<<A[<<i<<][<<j<<]=; cin>>A[i][j]; } for(i=0; i<5; i++) for(j=0; j<5; j++) A[i][j]=A[i][j]+1;// 1 // for(i=0; i<5; i++) for(j=0; j<5; j++) { out<<A[<<i<<][<<j<<]=<<A[i][j]; } |
11. .
#include iostream
#include <iomanip.h>
using namespace std;
void main()
{const int m = 4, n = 6;
int a[m][n];
int i,j, int kol;
//
cout<<Vvesti elementi massiva:<< endl;
for(i=0; i<m; i++)
for(j=0; j<n; j++)
cin>>a[i][j];
//
for(i=0; i<m; i++)
{for(j=0; j<n; j++)
cout<< setw(4) << a[i][j] << ;
cout<< endl;}
float S = 0;
for(i=0; i<m; i++)
{kol = 0;
for(j=0; j<n; j++)
{S += a[i][j];
if (a[i][j] > 0) kol++;
}
cout<<String: << i << Number: << kol << endl;
}
s/= m*n;
cout << Srednee arifmeticheckoe: << S << endl;}
:
Vvesti elementi massiva:
5 -3 0 4 6 2
0 7 -5 -8 1 0
-1 0 3 0 -5 0
1 1 -3 8 0 1
String: 0 Number: 4 String: 1 Number: 2 String: 2 Number: 1 String: 3 Number: 4 Srednee arifmeticheckoe: 0.583333 |
13. : |
#include iostream using namespace std; void main() { int a[4][4]; int i,j; for(i=0; i<4; i++) for(j=0; j<4; j++) if (i = = j) a[i][j] = 1; else a[i][j] = 0; for(i=0; i<4; i++) {for(j=0; j<4; j++) cout<< a[i][j]; cout<< endl;} } |