How to Add and Subtract arrays?

closed account (9ypNhbRD)
I need to make a calculator that adds and subtracts arrays. The problem the professor didn't really explain it well. This is what I got from the notes I took in class. Tho I'm 100% sure i copied them wrong.

#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;


int r[3];
static int r[3];
static char word[4];

for (i = 0; i < 3; i++)
{
cout << "\n Enter the value of r[" << i << "]=";
cin >> r[i];
}
for (i = 0); i < 3; i++);
{M[i] = r[i] - k[i];
0{i} = r{ i } +k{ i };
cout << "\n\n The subtraction of the two vectors are ""<<endl;" << endl;
for (i = 0; i < 3, i++)
{
M[i] = r[i] - k[i];
0{i} = r{ i } +k{ i };
}
cout << "r[x,y,z]=" << r[0] << "," << r[1] << "," << r[2] << "]k[x,y,z]=[" << k[0] << "," << k[1] << "," << k[2] << "]";
cout << "[" << M[0] << "," << M[1] << "," << M[2] << "]\n";
If they're math vectors as used in physics, then C = A + B is:
C[0] = A[0] + B[0];
C[1] = A[1] + B[1];
C[2] = A[2] + B[2];
// ...

You can imagine a vector being used to hold the velocity of an object on a plain, in which case you need a 2D vector to hold the x and y components. If it receives a nudge, the final velocity is the initial velocity plus that of the nudge, right?

These math vectors can be held in an array or a vector from the standard library.

Also, don't use static or global variables in your code unless you have to. That rule applies to pretty much all C++ features.
Last edited on
closed account (9ypNhbRD)
I don't understand. I'm new to all this, like I said the teacher didn't really explain it well and expects us to make a calculator. Why isn't the code i posted working?
Hi,

I just noticed this:

for (i = 0); i < 3; i++); // <= remove semicolon at the end and the first )

Here it is fixed up:

1
2
3
4
for (i = 0; i < 3; i++) {


}  


It would be easier to see, if you were to edit your post so it uses code tags. They show line numbers which is helpful. And they might mean you get more replies

If you have compiler errors , then post them here verbatim, we can explain what they mean. If you don't know what they mean, in the reference material you can look at each expression you are using on that line, to see if you are using it properly. There is reference material and a tutorial at the top left of this page.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;


int r[3];
static int r[3];
static char word[4];

for (int i = 0; i < 3; i++)
{
cout << "\n Enter the value of r[" << i << "]=";
cin >> r[i];
}
for (int i = 0; i < 3; i++)
{M[i] = r[i] - k[i];  // what is M ? Not declared anywhere 
0{i} = r{ i } +k{ i };  // 0{i} is that supposed to be O, not delcared? choose better variable names 
cout << "\n\n The subtraction of the two vectors are ""<<endl;" << endl;
for (int i = 0; i < 3, i++)
{
M[i] = r[i] - k[i];
0{i} = r{ i } +k{ i };  // use square brackets , not braces here
}

// use a for loop for the following as well
cout << "r[x,y,z]=" << r[0] << "," << r[1] << "," << r[2] << "]k[x,y,z]=[" << k[0] << "," << k[1] << "," << k[2] << "]";
cout << "[" << M[0] << "," << M[1] << "," << M[2] << "]\n"; 

// is this all the code ? where is the end of main?




Hope this helps :+)
Last edited on
There are two parts to this. The first is understanding the problem and what needs to be done. The second is being able to write a program that instructs the computer to do it.

As far as I can tell, you're unable to do either. We are trying to help, but ... "I don't understand." doesn't really help anyone.

Let me try again. Forget code right now. Do you understand the concept of a ball traveling in a straight line and being hit by another ball traveling in a straight line. How can we know what will be the effect on the first ball?
Topic archived. No new replies allowed.