Ikki o‘lchovli massivlarga oid masalalar (massivga konsoldan qiymat kiritish, massivdagi barcha elementlar yig‘indisini hisoblash).
#include
#include
#include
int main()
{
int m, n, a[10][10];
cout < "Satrlar sonini kiriting \nm=";cin > m;
cout < "Ustunlar sonini kiriting \nn=";cin > n;
cout <"Massiv elementlarini kiriting \n";
for(int satr = 0; satr < m ; satr++)
for(int ustun = 0; ustun < n; ustun++)
{
cout < "a[" < satr < "][" < ustun < "]=";a[satr][ustun] = rand();
}
for(int satr = 0; satr < m; satr++)
{
for(int ustun = 0; ustun < n; ustun++)
cout < a[satr][ustun] < "\t"; cout<"\n";
}
return 0;
}
int jami = 0;
for (int satr = 0; satr < SATR_HAJM; satr++)
{
for (int ustun = 0; ustun < USTUN_HAJM; ustun++)
{
jami += matrix[satr][ustun];
}
}
Ikki o‘lchovli massivlarga oid masalalar (massiv elementlariga tasodifiy qiymatlar berish, massivni konsolga chop etish).
#include
#include
void matrix_print(int a[10][10], int m, int n)
{
// matritsani jadval shaklida chiqarish
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << a[i][j] << "\t";
}
cout << "\n";
}
}
Ikki o‘lchovli massivlarga oid masalalar (massivdagi barcha elementlar yig‘indisini hisoblash, elementlari yig‘indisi eng katta bo‘lgan satrni aniqlash).
int jami = 0;
for (int satr = 0; satr < SATR_HAJM; satr++)
{
for (int ustun = 0; ustun < USTUN_HAJM; ustun++)
{
jami += matrix[satr][ustun];
}
}
Ikki o‘lchovli massivlarga oid masalalar (ustun elementlari yig‘indisini hisoblash, massiv elementlari qiymatlarini tasodifiy almashtirish).
Tasodifiy almashtirish:
srand (time(0));
for (int i = 0; i < SATR_HAJM; i++)
{
for (int j = 0; j < USTUN_HAJM; j++)
{
int i1 = rand() % SATR_HAJM;
int j1 = rand() % USTUN_HAJM;
// matrix[i][j] ni matrix[i1][j1] bilan almashtirish
double temp = matrix[i][j];
matrix[i][j] = matrix[i1][j1];
matrix[i1][j1] = temp;
}
}
Ustun elementlari yig`indisini hisoblash:
for (int ustun = 0; ustun < USTUN_HAJM; ustun++)
{
int jami = 0;
for (int satr = 0; satr < SATR_HAJM; satr++)
jami += matrix[satr][ustun];
cout << ustun << "-ustun yi`g`indisi: " << jami << endl;
}
Ikki o‘lchovli massivlarni funksiyaga argument sifatida uzatish (passing two-dimensional arrays to functions).
Namuna :
#include
using namespace std;
const int USTUN_HAJM = 4;
int sum (const int a[][USTUN_HAJM], int satrHajm)
{
int jami = 0;
for (int satr = 0; satr < satrHajm; satr++)
{
for (int ustun = 0; ustun < USTUN_HAJM; ustun++)
{
jami += a[satr][ustun];
}
}
return jami;
}
int main()
{
const int SATR_HAJM = 3;
int m[SATR_HAJM][USTUN_HAJM];
cout << SATR_HAJM << " ta satr va "
<< USTUN_HAJM << " ta ustun kiriting: "<< endl;
for (int i = 0; i < SATR_HAJM; i++)
for (int j = 0; j < USTUN_HAJM; j++)
cin >> m[i][j];
cout << "\nBarcha elementlar yig`indisi: "
<< sum(m, SATR_HAJM) << endl;
return 0;
}
Rekursiv funksiyalar (recursive functions).
Namuna :
#include
using namespace std;
int f(int n){
if (n <= 1)
return 1;
else
return n*f(n-1);
}
int main(){
int num;
cout<<"Son kiriting: ";
cin>>num;
cout<<"Factorial of entered number: "<return 0;
}
Izoh: Dasturlashga oid masala sifatida D.Liang – “Introduction to programming with C++” kitobining 1- dan 8-gacha bo‘lgan boblari tarkibidan olingan bitta masala beriladi.
Do'stlaringiz bilan baham: |