Vector does not print correctly

Asked

Viewed 48 times

1

Create a program in C++. Have a vector of integers of 10 positions filled by the user. Print the sum of the components of this vector.

I made the code below but it does not print the values of the vectors.

#include <iostream>
using namespace std;
#define SIZE 10
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    int valor[SIZE];
    int total, i, a;
    
//  a = sizeof(valor)/ sizeof(valor[0]);
    
    for (i = 1; i <= sizeof(valor)/ sizeof(valor[0]); i++)
    {
        cout<<"Digite o valor: ";
        cin>>valor[i];
    }
    
    for ( i = 0; i < 10; i++)
    {
        cout<< valor[i];
    }
    
    
    for ( i = 1; i < 10; i++)
    {
        a = a + valor[i];
    }
    cout<< a;
    

    return 0;
}
  • What’s the point of using sizeof(valor)/ sizeof(valor[0]) and not simply SIZE? If the variable a is an accumulator so you need to initialize it with 0 and the index i from 0, or make a = valor[0]; since its loop part of index 1.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

  • As @Maniero demonstrated, if you know the size of the array previously, use this. To start, in C++, if you don’t know what you’re doing (or why), in practice you might be using Std::vector instead of making arrays "in the hand". In C, the story is another. C/C++, unlike other mainstream languages (PHP, Python, Ruby, etc...), you must knowing the size of what you’re dealing with, unless need be dynamic. That is, if your array doesn’t have to be dynamic, assume your size is static and use this to your advantage.

1 answer

1

The biggest problem is that you are not reading the amount of data you think you are. You are starting to read the data and storing at position 1 of the array, whereas the array starts at position 0. Then you will already have a garbage in that position and will make the result wrong or right just by coincidence. I didn’t quite understand the account you made to go all the way. This technique is used for something else. You already know the size of the array, why not use this?

And why not use the vector of C++ which is much better? I don’t think you should mix C with C++.

There are other things that can be better, like having a single loop to add and print. Organizing the code would look like this:

#include <iostream>
using namespace std;
#define SIZE 10

int main() {
    int valor[SIZE];
    for (int i = 0; i < SIZE; i++) {
        cout << "Digite o valor: ";
        cin >> valor[i];
    }
    int total = 0;
    for (int i = 0; i < SIZE; i++) {
        cout << valor[i] << endl;
        total += valor[i];
    }
    cout << total;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

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