Adding Vectors in C++: Example Code for Summing Elements with an Offset Amount

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Multiple Vectors
Click For Summary
SUMMARY

The discussion focuses on summing elements from two vectors in C++. The task involves adding corresponding elements from an `origList` vector and an `offsetAmount` vector, then printing the results. A sample program is provided, demonstrating the initialization of both vectors and the need for a loop to perform the addition. The expected output for the given example is "45 57 63 70".

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with the C++ Standard Library, specifically the vector class
  • Knowledge of control structures in C++, particularly loops
  • Basic experience with input and output operations in C++
NEXT STEPS
  • Implement a for-loop to iterate through vectors in C++
  • Explore C++ vector methods such as at() and push_back()
  • Learn about range-based for-loops in C++ for cleaner code
  • Study error handling in C++ to manage vector bounds
USEFUL FOR

C++ students, software developers, and anyone looking to improve their skills in vector manipulation and basic arithmetic operations in C++.

ineedhelpnow
Messages
649
Reaction score
0
im going through previous assignments to study for a test and i can't seem to remember how i did this one.

Add each element in origList with the corresponding value in offsetAmount. Print each sum followed by a space. Ex: If origList = {40, 50, 60, 70} and offsetAmount = {5, 7, 3, 0}, print:

45 57 63 70
Sample program:

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

int main() {
   const int NUM_VALS = 4;             
   vector<int> origList(NUM_VALS); 
   vector<int> offsetAmount(NUM_VALS); 
   int i = 0;                         

   origList.at(0) = 40;
   origList.at(1) = 50;
   origList.at(2) = 60;
   origList.at(3) = 70;

   offsetAmount.at(0) = 5;
   offsetAmount.at(1) = 7;
   offsetAmount.at(2) = 3;
   offsetAmount.at(3) = 0;

   <STUDENT CODE>
   cout << endl;

   return 0;
}

(Wondering)
 
Technology news on Phys.org
ineedhelpnow said:
im going through previous assignments to study for a test and i can't seem to remember how i did this one.

Add each element in origList with the corresponding value in offsetAmount. Print each sum followed by a space. Ex: If origList = {40, 50, 60, 70} and offsetAmount = {5, 7, 3, 0}, print:

45 57 63 70

Hi! (Smile)

It sounds like a for-loop in which you add elements.
What kind of for-loop can you can up with? (Wondering)
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 3 ·
Replies
3
Views
7K
Replies
2
Views
5K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
12
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K