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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top