problem with arrays

Hi,

I wrote this simple program and I am trying to add up all the elements of one array into one element of another array but this does not seem to work. As shown below I am using this function: dx[y]+= fx [y];

Any suggestions?


# include <iostream>
# include <cmath>
using namespace std;


double fx [4], dx [4];
int y;
int main ()
{

{ fx [0] = -2.123; fx [3] = 4.2233;

cout<< abs (fx [0]) <<endl;
cout << fx [1]<< endl;
cout <<fx [2]<< endl;
cout << fx [3] << endl;
cout << pow (fx [3], 3)<< endl;
cout << sqrt (81) << endl;
for (y = 0; y < 4; y++)
{
dx[y]+= fx [y];
cout << dx [y]<<endl;
}

cin.get ();
}}


By the way I am using DEV C++ and Windows XP.

Last edited on
Ok so in my application how do I remedy this...how do I use tags?
closed account (zwA4jE8b)
Format: <> button for tags

what does not work?

You only initialize fx[0] and fx[3]. dx[y] is uninitialized so you cannot += it.

Use for loops to initialize all slots in the array first.
[ code ] code here [/ code ]

You have to set all your variables to 0 manually, The thing is when you don’t you will get a number that is left in the memory by another program. Not a very good example

like:
1
2
for(y = 0;y < 4;y++)
     fx[y] = dx[y] = 0;
Thank you sahzz and CreativeMFS....that worked...
Topic archived. No new replies allowed.