C++ Massiv va yig`indi

+3 ovoz
91 marta ko‘rilgan
so‘radi 26 mart, 17 kamina94 (45 bal)
tahrirlandi 26 mart, 17 Saidolim

X=(x1,x2,....xn) massivdagi manfiy elementlarni Y=(y1,y2...yn) massivga ketma ket
yozing. Manfiy elementlarning sonini aniqlang

        k
P= SUMMA  y

i ni xisoblang i=1


#include <iostream>

int main(int argc, char *argv[])
{
    int *arr, *new_arr;
    size_t n, j = 0;

    std::cout « "Massiv sonini kiriting.." « std::endl;
    std::cin » n;
    arr     = new int[n];
    new_arr = new int[n];
    for(size_t i = 0; i < n; ++i)
    {
        std::cout « "Massiv[" « i + 1 « "].." « std::endl;
        std::cin » arr[i];
    }
    for(size_t i = 0; i < n; ++i)
        if(arr[i] % 2 == 0)
            new_arr[j++] = arr[i];
    std::cout « std::endl « "Natija" « std::endl;
    for(size_t i = 0; i < j; ++i)
        std::cout « new_arr[i] « ' ';
    return 0;
}

1 ta javob

+1 ovoz
javob berdi 28 mart, 17 parmonov98 (809 bal)
tahrirlandi 28 mart, 17 Saidolim
#include <iostream>

int main(int argc, char *argv[])
{
    int *arr, *new_arr;
    size_t n, j = 0, counter = 0;

    std::cout << "Massiv sonini kiriting.." << std::endl;
    std::cin >> n;
    arr     = new int[n];
    new_arr = new int[n];
    for(size_t i = 0; i < n; ++i)
    {
        std::cout << "Massiv[" << i + 1 << "].." << std::endl;
        std::cin >> arr[i];
    }
    for(size_t i = 0; i < n; ++i){
        if(arr[i] < 0){
            new_arr[j++] = arr[i];
            counter++;
            }
    }
    std::cout << std::endl << "Manfiy elemntlar soni: "<< counter << "\nNatija:" << std::endl;
    for(size_t i = 0; i < j; ++i)
        std::cout << new_arr[i] << ' ';
    return 0;
}

if(arr[i] % 2 == 0)  mana shu joyda 0 kichik bo`lganlarini olish kerak.agar so`ragan narsangizni tog`ri tushungan bo`lsam

Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.

Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin.

Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

Telegram kanal YordamUzRss

...