Bu dastur saralashni qator bo’yicha olib borish uchun ishlatiladi.
Agar saralashni ustun bo’yicha qilish kerak bo’lsa, quyidagicha yozish kerak bo’ladi:
for ( i=0; i< 2; i++)
for ( j=0; j<3; j++)
{ if ( a[i][j] > a[i+1][ j] ) { b= a[i][j]; a[i][j]= a[i+1][ j]; a[i+1][ j]= b; }
11-misol. Matritsaning izini hisoblash dasturini tuzish. Matritsaning izi deb bosh diagonal elementlarining yig’indisiga aytiladi. Shu dasturda teskari (qo’shimcha) diagonal elementlarining yig’indisini ham hisoblashni ko’rib o’tamiz.
// Matritsa (bosh va teskari) diagnal elementlari
// yig'indisini hisoblash dasturi.
# include
# include
using namespace std;
int main ( )
{
float a[3][3] = { {9.1, 8.3, 6.8},
{4.8, 5.7, 1.6},
{4.7, 3.8, 9.5} };
float ds=0, tds=0; int i, j;
for ( i=0; i<3; i++)
ds = ds + a[i][i];
for ( i=0; i<3; i++)
for ( j=0; j<3; j++)
if ( i+j == 2) tds = tds + a[i][j];
cout <<"\n Bosh diagnal elementlari summasi ds= "<
cout <<"\n Teskari diagnal elementlari summasi tds= "<
getch ( );
return 0;
}
Bosh diagnal elementlari summasi ds= 24.3
Teskari diagnal elementlari summasi tds= 17.2
12-misol. Xar bir hadi an = formulasi orqali hisoblanadigan satr yig’indisini 0,0001 aniqlikda hisoblash dasturini to’zish.
// Har bir hadi An =2n!/(2n+1)! formulasi orqali hisoblanadigan
// satr yig'indisini 0,0001 aniqlikda hisoblash dasturini tuzish.
# include
# include
using namespace std;
int main ( )
{
int n =1; float s1 = 0, s2 = 0;
float p1 =1, p2 = 1;
while (s2 < 0.0001)
{ p1 = p1 * 2*n*(2*n-1); // p1*= 2*n*(2*n-1);
p2 = p2 * (2*n+1)*((2*n+1)-1); // p2*= (2*n+1)*(2*n);
s2 = p1 / p2;
s1 = s1 + s2; // s1+ = s2;
n=n+1; } // n++;
cout. precision (5);
cout<
cout << " s1="<
http://fayllar.org
Do'stlaringiz bilan baham: |