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

AI Thread Summary
To solve the problem of adding corresponding elements from two vectors, a for-loop is necessary. The task involves iterating through each index of the `origList` and `offsetAmount`, summing the values at each index, and printing the results. The example provided illustrates that for `origList` containing {40, 50, 60, 70} and `offsetAmount` containing {5, 7, 3, 0}, the output should be the sums: 45, 57, 63, and 70. The discussion emphasizes the need for a clear implementation of the for-loop to achieve this output.
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)
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top