954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array Help

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.

shenmue232
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

So you get junk out?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Yeah i get aload of junk

shenmue232
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

post all your code

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
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

shenmue232
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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;
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

shenmue232
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 
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

shenmue232
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

> 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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
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 aspecific value.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Please post what you have tried.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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;

shenmue232
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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

shenmue232
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You