dimanche 1 décembre 2019

Compute a new list of integers by adding consecutive elements in a list with C++

I am a beginner in C++. I am wondering how can I compute a new list by adding 8 consecutive elements and then divide them by the number of elements added in a list with C++. For example, the new list is re[], and the list we'll be using is a[], it has 200 elements. so re[i] = (a[i-1]+a[i-2]+a[i-3]+a[i-4]+a[i]+a[i+1]+a[i+2]+a[i+3]+a[i+4])/9

for(int i=4;i<196;i++){
    re[i] = (a[i-1]+a[i-2]+a[i-3]+a[i-4]+a[i]+a[i+1]+a[i+2]+a[i+3]+a[i+4])/9
}

However the above code is not applicable to the first 4 elements and the last 4 elements in re[], because a[i] in these cases has no 4 consecutive elements either preceding or following a[i]. So I am wondering how can I do the same using for loop for these elements? Thanks for any help.

Aucun commentaire:

Enregistrer un commentaire