How to subtract extreme indices from the same vector?

Asked

Viewed 22 times

0

A formula em questão

I am having difficulty in this exercise, I would like to know how it is possible to carry out this formula operation

int main()
{
    int A[20], i, soma, j;

    cout << "Digite o valor do vetor A";

        for (i = 0; i < 6; i++)
        {
            cin >> A[i];
        }

            for (i = 0; i < 6; i--)
            {
                soma=A[i]+A[pow(i+i--)]
            }


    return 0;
}

1 answer

0

Loop up to half the amount of elements. Take the ends (in ascending and descending order) and add the square dd difference. At the end display the calculated sum.

#include <iostream>
#include <cmath>
#define N 6
using namespace std;
int main() { 
    int AN0], i, soma=0, j;
    cout << "Digite o valor do vetor A";
    for (i = 0; i < N; i++) {
        cin >> A[i];
    }
    for (i = 0; i < N/2; i++) {
        soma += pow((A[i] - A[N-i-1])), 2);
    }
    cout << "Soma: " << soma << endl
    return 0;
}

Note that in your loop you start i with zero value and decrease with each cycle, so you will be in an infinite loop because you will always be less than 6 (at least until an underflow occurs).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.