Napisz program wyświetlający elementy dodatnie tablicy.

Napisz program sumujący elementy tablicy.
język c++



Odpowiedź :

REGNAD

Odpowiedź:

#include <iostream>

int main() {

   int tab[] = {-1, -2, 3, 4}; //deklarujemy naszą tablicę

   int sum = 0; //deklarujemy licznik sumy

   for(const auto &number : tab){ //pętla po elementach tablicy

       if(number > 0){ //jeśli element większy od 0, to wyświetl

           std::cout << number << std::endl;

       }

       sum += number; //dodajemy do sumy elementow numer

   }

   

   std::cout << "Suma elementow tablicy, to: " << sum; //wyświetlamy sumę

   return 0;

}

Wyjaśnienie:

Zrobiłem oba w jednym, bo czemu nie