DEV Community

Cover image for C++ massivlar va funksiyalar
Begzod Murodullayev
Begzod Murodullayev

Posted on

C++ massivlar va funksiyalar

post mavzusi (maqsadi) : "array" ning maqsadi , afzalliklari va funksiya bilan ishlash .
kodli misollar :

#include <iostream>
using namespace std;

int main(){
    int son1 = 1;
    int son2 = 2;
    int son3 = 3;
    int son4 = 4;
    int son5 = 5;

    int sonlar[5] = {1 , 2 , 3 , 4 , 5};

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

sharx , kop elementlarni joylashning qulayligi .

elon qilish va elementlarni joylash:

array nomi , "[]" ichida arrayning kerakli indeksi beriladi

#include <iostream>
using namespace std;

int main() {

    int sonlar[5] = {};
    for (int i = 0; i < 5; i++) {
        cin >> sonlar[i]  ; 
    }
    for (int i = 0; i < 5; i++) {
        cout << "sonlar[" << i << "] = " << sonlar[i] << endl; 
    }
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

natija :

Image description

tavsif :
Array bu bir hil tipdagi bir nechta elementlarning jamlanmasi ,
arrayni boshqa oddiy o'zgaruvchilar kabi
lokal va global holatda ishlatish mumkun .
va turli finksiyalar ichida foydalanish imkoni mavjud

qoshimcha :
arrayga bn turli amallar
elon qilishda [] ichida turli amallarni bajarish imkoni

sonlar[i+2 % 10]

malumot uchun :

Image description

hajm 1 dan hisoblanadi va kerakli farmatta beriladi ,
index 0 dan hisoblanadi va hajmdan 1 qadam orqada turadi yani 3 ta lik arrayning oxirni elementi bu 3 emas 2 boladi va bu index deyiladi yani arraydagi elementlarning tartib raqami .

Image description

arrayni funksiyalarda va global tipda ishlatishka misol

bu dastur 0 kiritulguncha sonlarni qabul qiladi va engkatta 3 ta sonni qaytaradi

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int max_son[3] = {0, 0, 0};

void tekshirish(int son) {
    if (son > max_son[0]) { 
        max_son[2] = max_son[1];
        max_son[1] = max_son[0];
        max_son[0] = son;
    } 
    else if (son > max_son[1]) { 
        max_son[2] = max_son[1];
        max_son[1] = son;
    } 
    else if (son > max_son[2]) { 
        max_son[2] = son;
    }
}
int main() {
    int son;
    cout << "Sonlarni kiriting (0 kiritilganda tugaydi): ";
    cin >> son;
    while (son) { 
        tekshirish(son);
        cin >> son;
    }
    cout << "Eng katta 3 ta son: " << max_son[0] << " " << max_son[1] << " " << max_son[2] << endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

natija

Sonlarni kiriting (0 kiritilganda tugaydi):
789 987 654 85 4 12 -87 632 456 0
Enter fullscreen mode Exit fullscreen mode
Eng katta 3 ta son: 987 789 654
Enter fullscreen mode Exit fullscreen mode

murojaat uchun my telegram : begzodbek

Top comments (1)

Collapse
 
murodullayev_web profile image
Begzod Murodullayev

hatoliklar bo'lgan bo'lsa uzur ))
agar hato to'g'risida murojaat qilmoqchi bo'lsangiz bemalol
tg manzil post ostida qo'yilgan