From 170b77021c3edda0b7ec6a5032c9777bb8022155 Mon Sep 17 00:00:00 2001 From: Vichingo455 Date: Tue, 3 Dec 2024 17:12:28 +0000 Subject: [PATCH] Upload files to "array" --- array/somma_array_zero.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 array/somma_array_zero.cpp diff --git a/array/somma_array_zero.cpp b/array/somma_array_zero.cpp new file mode 100644 index 0000000..9df1671 --- /dev/null +++ b/array/somma_array_zero.cpp @@ -0,0 +1,33 @@ +/* +AUTORE: Manuel Vichi +Dopo aver letto e memorizzato 8 numeri in un array, la funzione deve calcolare la somma di quelli negativi e memorizzare zero al loro posto +*/ +#include +#include +#include +#include +using namespace std; +int sommaNegativi(int array[], size_t size) { + int somma; + for (int i = 0; i < size; i++) { + if (array[i] < 0) { + somma += array[i]; + array[i] = 0; + } + } + return somma; +} +int main() { + size_t dimensioneArray; + cout << "Inserisci il numero di valori da memorizzare: "; + cin >> dimensioneArray; + cout << endl; + int array[dimensioneArray]; + for (int i = 0; i < dimensioneArray; i++) { + cout << "Inserisci il valore: "; + cin >> array[i]; + cout << endl; + } + cout << "La somma dei numeri negativi e': " << sommaNegativi(array,dimensioneArray) << endl; + return 0; +}