![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0
![]() |
Array addition?
Hi everyone,
This maybe something very trivial, I am having trouble adding or multiplying elements of 2 or more arrays togethor. My original program is more complicted but this simple program may be a good example to test the ideas. #include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main ()
{
int i, j, k, t, num1[6], num2[6], numTot[6];
// place 6 numbers in array num1
for(t=0; t<=5; t++) {
num1[t] = t;
cout << num1[t] << endl;
}
// place 6 numbers in array num2
for(j=5; j<=10; j++) {
num2[j] = j;
cout << num2[j] << endl;
}
// sum of 2 arrays element by element
for(k=0; k<=5; k++) {
numTot[k] = num1[t] + num2[j];
cout << numTot[k] << endl;
}
}Really appreciate it if anyone has some ideas. Thanks. ![]()
__________________
Greatness courts failure and solitude. --- Anonymous |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Hope this makes sense to you:
#include <iostream>
using namespace std;
int main()
{
int num1[6], num2[6], numTot[6];
// place 6 numbers in array num1
for(int t=0; t<=5; t++)
{
num1[t] = t;
cout << "num1[" << t << "]: " << num1[t] << endl;
}
cout << endl;
// place 6 numbers in array num2
for(int j=0; j<=5; j++)
{
num2[j] = j;
cout << "num2[" << j << "]: " << num2[j] << endl;
}
cout << endl;
// sum of 2 arrays element by element
for(int k=0; k<=5; k++)
{
numTot[k] = num1[k] + num2[k];
cout << "numTot[" << k << "]: " << numTot[k] << endl;
}
return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0
![]() |
NNxion
That helped out a lot, I can't believe it was that simple. Thanks for your help.
__________________
Greatness courts failure and solitude. --- Anonymous |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|