Nested Vector Logic - C++

Asked

Viewed 183 times

4

I have a problem with the following statement:

Given a vector A with n real numbers, obtain another vector B, also with n real numbers, as follows:

B[1] = 2*A[1]

B[2] = 3*A[1] + 2*A[2]

B[3] = 4*A[1] + 3*A[2] + 2*A[3]

(...and so on)

I did the program, but my logic is wrong and I can’t identify the error. Can anyone help me? Follow the code I wrote.

#include <iostream>
using namespace std;


int main(){

    int tamanho;

    cout << "Qual o tamanho do vetor?  ";
    cin >> tamanho;

    float vetorA[tamanho], vetorB[tamanho];

    for (int i = 0; i < tamanho; i++){  
        cout<< "Digite o numero :";     
        cin >> vetorA[i];
    }


    for(int i = 0; i < tamanho; i++){
        for(int j = 2; j <= tamanho + 1; j++){
            vetorB[i] += j * vetorA[i];
        }       
    }   


    int i = 0;

    while(i < tamanho){
        cout << "\nA["<< i << "] = " << vetorA[i] << "\t B[" << i << "] = " << vetorB[i]; 
        i++; 
    }
}
  • Note: The program runs without build errors. The error is in logic!

  • No need to put RESOLVIDO, this is indicated otherwise, with an accepted answer. How and why to accept an answer? and tour

  • Thanks for the tip!

2 answers

3

Error solved. I leave the code to help anyone who might be having the same problem.

#include <iostream>
using namespace std;


int main(){

    int tamanho, aux = 0;

    cout << "Qual o tamanho do vetor?  ";
    cin >> tamanho;

    float vetorA[tamanho], vetorB[tamanho];

    for (int i = 0; i < tamanho; i++){  
        cout<< "Digite o numero :";     
        cin >> vetorA[i];
    }


    for(int i = 1; i <= tamanho; i++){
        aux = i + 1;
        for(int j = aux, k = 1; k <= i; j--, k++){
            vetorB[i-1] += j * vetorA[k-1];
        }       
    }   


    int c = 0;
    while(c < tamanho){
        cout << "\nA["<< c << "] = " << vetorA[c] << "\t B[" << c << "] = " << vetorB[c]; 
        c++; 
    }
}

0


Maybe, have thought of a "formula" would help to get the right result more quickly.

B[n] = (n+1) * A[1] + n * A[2] + (n-1) * A[3] +...+ 3 * A[n-1] + 2 * A[n]

The error was really only in logic. Analyzing the statement carefully we can notice some patterns.

Explicação gráfica

It is noticeable that you recognized the pattern by observing your code.

for(int i = 1; i <= tamanho; i++){
    aux = i + 1;
    for(int j = aux, k = 1; k <= i; j--, k++){
        vetorB[i-1] += j * vetorA[k-1];
    }       
} 

The for external, has as a stop condition, the size of the vector. When storing the value of i in an auxiliary variable plus one unit, covers one of the conditions for the right result to be obtained. In the second for, ensures that the rule will be executed for each term of the expression until the indexes j and k be equal.

Another important way to test your code (as a beginner) to find what is wrong is tracking.

Bons Estudos!

Browser other questions tagged

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