#Problem in code?

4 messages · Page 1 of 1 (latest)

white kraken
#
 #include <iostream>
#include <vector>
using namespace std;

int main(){
    int n;
    cin>>n;
    vector <int> v(n);
    for (auto x:v)
    cin>>x;
   
    
    for (auto x:v)
    cout<<x<<" "<<endl;
}

is there a problem in my code? because no matter what i input the output is always 0
please check attachment for refference
thankyou

dim spruce
#

There's a couple things to check:

  1. You're using the vector<int> constructor that creates N elements, yes, but it default initializes each int inside to be 0.
  2. for (auto x : v) only gets copied values of each int from the vector you created.

Consider using a different for loop or some other approach.

white kraken
white kraken