Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1053457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:15:21+00:00 2026-05-16T17:15:21+00:00

I am trying to add two Vectors below is the code snippet :- #include

  • 0

I am trying to add two Vectors below is the code snippet :-

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

int main()
{
        unsigned int i = 0;
        vector <float> v1;
        vector <float> v2;
        vector <float> v3;



        cout << "Filling the Numbers\n";
        for (i=5;i < 125 ; i = i + 5) {
            v1.push_back(i/10);
            v2.push_back(i/100);
        }

        cout << "Adding the numbers\n";
        for (i = 0; i < v1.size(); i++) {
                v3[i] = v1[i] + v2[i];
        }

        cout << "Printing the numbers\n";
                for (i = 0; i < v3.size() ; i++) {
                        cout << v3[i];
                }


        return 0;
}

The program is crashing at Line 18. It seems to me I need to do operator overloading for + operation. Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:15:22+00:00Added an answer on May 16, 2026 at 5:15 pm

    This line doesn’t work, because there’s no v3[i] allocated:

    v3[i] = v1[i] + v2[i];
    

    You have two choices, either use ‘push_back’

    v3.push_back( v1[i] + v2[i] );
    

    Or resize the array to the given size before hand:

    v3.resize( v1.size() );
    

    If you push_back, it will be nice to preallocate the space anyway:

    v3.reserve( v1.size() );
    

    And finally, you can try to read up on std::valarray instead, as those operations are already built into it!

    Edit: and yes, as Johannes noted, you have a problem with floating point division :>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create Pdf from java using iText. How Can I Add two
I'm trying to add two images together using NumPy and PIL. The way I
I am trying to add two matrices using pthreads.Just a beginners program,but I am
I'm trying to add two object that they are in the same class. In
I'm trying to add two inputs to a QTCaptureSession in the following: mainSession =
I am trying to add two larger binary numbers (i.e. number of bits are
I am trying to add two values in a byte array. This is my
I am trying to add two decimal values but the returned sum is pure
We have a situation where we are trying to add two UITableViews inside a
I am trying to do this List<String> stringList = new ArrayList<String>(); stringList.add(one); stringList.add(two); stringList.add(three);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.