Hi guys Ive been having problems with calculations on arrays. What i want to do is times two numbers in an array for example:

4 x 4 = 16

Here's what i'm trying to do

double timeArray[3];

cin >> timeArray[0];

cint >> timeArray[1];

answer = timeArray [0] * timeArray[1];

the program compiles OK, the problem is i get a load of numbers can anyone help.

Recommended Answers

All 13 Replies

Member Avatar for iamthwee

So you get junk out?

Yeah i get aload of junk

Member Avatar for iamthwee

post all your code

post all your code

There's a lot of code :) this is one part of the program

would you be able to make me a basic program with an array that times 2 numbers from the array and shows it to the user

Member Avatar for iamthwee

My guess is that you are couting a variable that has yet to be initialised to some arbitary value.

At the beginning try doing:

timearray[0] = 0;
timearray[1] = 0;

answer = 0;

then do the rest... so get the user to do the cins

And maybe you're couting timearray[3] without actually assigning it to answer, i.e:

timearray[3] = answer;

My guess is that you are couting a variable that has yet to be initialised to some arbitary value.

At the beginning try doing:

timearray[0] = 0;
timearray[1] = 0;

answer = 0;

then do the rest... so get the user to do the cins

ok i will give that a try and get back to you

ok i will give that a try and get back to you

Still no luck, it would be a great help if you could make a sample program to show me how it works.

Regards shenmue232

> would you be able to make me a basic program with an array that times
> 2 numbers from the array and shows it to the user
Why can't you create one, to explore the problem for yourself.
If you can get it to work, you might be able to figure out the answer to your bigger question.
If not, then at least you have something small to post here.

My guess is that you are couting a variable that has yet to be initialised to some arbitary value.

Actually, you may be cout ing a variable that has an arbitrary value, and you should initialize it to a specific value.

Member Avatar for iamthwee

Please post what you have tried.

I have a picture of the program follow the link below to view it

http://www.tommynewman.co.uk/C++.jpg

Heres some of the code

void calculate::pythagoras() // member function for pythagoras function
{
  answer = pow(cityX,2) + pow(cityY,2) + pow(cityZ,2);
  answer2 = pow(*ptrX,2) + pow(*ptrY,2);  //THIS PART IS THE PROBLEM
  pythag = (sqrt(answer));// squre root data member "answer" to find out pythagoras 
  pythag2 = (sqrt(answer2));//THIS PART IS THE PROBLEM
}

void calculate::output() // show the outputs of pythagoras and cityblock
{ 
  cout << "Cityblock Distance Between X = " << cityX << endl;
  cout << "Cityblock Distance Between Y = " << cityY << endl;
  cout << "Cityblock Distance Between Z = " << cityZ << endl;
  cout << "" <<endl; // add a space 
  cout << "Pythagoras Distance Between Object 1 and Object 2 = " << pythag << endl;  
}
/******************************************************************************/
/*** PART 3                                                                 ***/
/******************************************************************************/
void calculate::arrayPointers()
{
  ptrX = &timeArray[0];/* point our pointer at the first double in our array  */
  ptrY = &timeArray[1];/* point our pointer at the second double in our array */
  ptrZ = &timeArray[2];/* point our pointer at the third double in our array  */   
}

void calculate::inputObject3()// ask the user for coodinates for object 3
{     
  cout << "Enter Coordinate X for Object 3" << endl; 
  cin >> timeArray[0];// user input stored in point 0 of array

  cout << "Enter Coordinate Y for Object 3" << endl;
  cin >> timeArray[1];// user input stored in point 1 of array

  cout << "Enter Coordinate Z for Object 3" << endl; 
  cin >> timeArray[2];// user input stored in point 2 of array
}

void calculate::forLoop()
{
{     
//a cycle function should be provided, that will run through the array from beginning to end,
//reporting the coordinates of the point object at each index     
     
     for( i = 0; i < 4; i++) // if i equals 4 stop counting
      
     if (i == 0)// if i equals 1 show point 0 of the array
     cout << "Value Of X For Object 3 = " << *ptrX << endl; 
     else
     if (i == 1)// if i equals 2 show point 1 of the array
     cout << "Value Of Y For Object 3 = " << *ptrY << endl;
     else
     if (i == 2)// if i equals 3 show point 2 of the array
     cout << "Value Of Z For Object 3 = " << *ptrZ << endl;
     else
     if (i == 3)
     cout << "This Work! " << pythag2 << endl;
Member Avatar for iamthwee

I think in this instance it would be wise to post all your code. I'm wondering how you've declared timeArray ?

Are they public or private declarations?And I think you're confusing the pointer notation. Anyway, post all your code, then we won't have to guess.

double timeArray[3]; // Create an array of 3 integers
     double *ptrX;
     double *ptrY;
     double *ptrZ;

The values above are private and they are in the same class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.