# Thread: Arrays Data Structures

1. ## Pleeease Help w/ code

Hello, I am a new programmer and have been learning it pretty well on my own for a while. I was recently given an assignment using arrays and ran into a problem. I was hoping that someone could please give me a hint and let me know where I went wrong because I've written most of the code already and want to complete it myself. I've written it many different ways but am not sure if something is wrong with the program or/and the formula. I know that it is something so small that I am overlooking; unfortunately I am 100% comfortable with how arrays work yet but am definitely trying to learn. Any help would greatly be appreciated

The program is basically supposed to calculate the distance an object travels from when it is dropped. It also prompts the user for time (seconds it has been dropped from-1 to 10) then it prints out the users results as well as the results for the other seconds (1-10).

Formula I am using is
d=.5*g*t*t

g=9.8m
t= the number user enters between 1-10

I have been able to get the program to compile successfully, unfortunately the same cannot be said about the results. The output should resemble

Second Traveled (m)
1 4.9
2 19.6
.......
5 122.5
......
10

My program doesn't calculate the values for the array correctly and I cannot figure out why. It will correctly calculate the distance for the number that the user enters (e.g. 1 second will be 4.9 meters) but incorrectly for the other seconds. Please let me know if you have any ideas as to what I am doing wrong.

Thank you so much

Code:
```#include<iostream.h>#include<iostream.h>
#include<iomanip.h>

int main()
{

const int arraysize=10 ;

double d[arraysize];
int t;
double tot;

cout<<"Please enter the travel time\n";
cin>>t;

for (int i=0; i<arraysize; i++)
d[i]=.5*9.8*t*+i;

cout<<"      Time"<<setw(13)<< "     Distance Traveled"<<endl;

for (int j=0;j<=t;j++)
{
tot =d[j];
cout<<setw(9)<<j<<setw(13)<<tot<<endl;
}

for (int s=j;s<=arraysize;s++)
{
cout<<setw(9)<<s<<setw(13)<<tot<<endl;
}

return 0;
}```

2. ## It's your formula.

I haven't tried this out, but from your text, I think you want to calculate the users time outside the loop, using t, and all the other times inside the loop using i.

Code:
```tot =  0.5 * 9.8 * t*t ;

for (int i=0; i<arraysize; i++)
d[i]= 0.5 * 9.8 * i*i ;```
Hopefully that will get you back on track.

You could use t in place of i & j, if you are done with the user's input after the first calculation.

You could also use 4.9 in place of 0.5 * 9.8

You don't need to use a new variable in your cout statement, you could write:
cout<<setw(9)<<j<<setw(13)<<d[j]<<endl;

[EDIT
This is just a personal preference, but I think some whitespace makes programs more readable:
cout<< setw(9) << j << setw(13) << d[j] <<endl;

3. Here's one of the problems with your code:
Code:
```for (int j=0;j<=t;j++)
{
tot =d[j];
cout<<setw(9)<<j<<setw(13)<<tot<<endl;
}

for (int s=j;s<=arraysize;s++)
{
cout<<setw(9)<<s<<setw(13)<<tot<<endl;
}```
Because you declared j in the first for loop, it can only be accessed in that for loop. Declare it within main with your other declarations if you plan on using it again.

Oh, and do you really need an array for this? I think you could code this with just one for loop.

5. Instead of this:
Code:
```for (int i=0; i<arraysize; i++)
d[i]=.5*9.8*t*+i;

cout<<"      Time"<<setw(13)<< "     Distance Traveled"<<endl;

for (int j=0;j<=t;j++)
{
tot =d[j];
cout<<setw(9)<<j<<setw(13)<<tot<<endl;
}

for (int s=j;s<=arraysize;s++)
{
cout<<setw(9)<<s<<setw(13)<<tot<<endl;
}```
Try this:
Code:
```for (int i = 1; i < arraysize + 1; i++)
d[i] = .5 * 9.8 * i * i;

cout << "      Time" << setw(13) << "     Distance Traveled"<< endl;

for (int j = 1; j <= t; j++)
cout << setw(9) << j << setw(13) << d[j] << endl;

for (int s = j + 1; s < arraysize + 1; s++)
cout << setw(9) << s << setw(13) << d[j] << endl;```
However, I think this would be the simplest way to do it:

Code:
```cout << setw(9) << t << setw(13) << (.5 * 9.8 * t * t) << "\n\n";

cout << "      Time" << setw(13) << "     Distance Traveled"<< "\n\n";

for(int i = 1; i < arraysize + 1; i++)
cout << setw(9) << i << setw(13) << (.5 * 9.8 * i * i) << endl;```
This code will print out:
"
....<t>......<.5 * 9.8 * t * t>

...Time......Distance Traveled

......1........4.9
......2........19.6
......3........44.1
......4........78.4
......5........122.5
......6........176.4
......7........240.1
......8........313.6
......9........396.9
......10......490
" {substitute spaces for the periods}
everytime.

6. Originally posted by Hammer
Yeah, and it seems that your 'e' 'l' and 'p' keys are getting stuck. Maybe you need a new keyboard .

7. For starters, you're addressing your arrays wrong. You can only access 0 to arraysize -1 as valid array elements.

All of your loops should be:
Code:
`for( int x = 0; x < arraysize; x++ )`
And not:
Code:
`for( int x = 0; x <= arraysize; x++ )`
Next, your value is likely correct, except you're expecting the wrong thing. The first value of your first array will be zero, because you're multiplying by i, which starts at zero and not one.

Also, it's good to typecast integers to floating point numbers when using floats, because floats are wierd about things like that some times. (For example, they're not good for loop control and precise comparison.)

Quzah.

Quzah.

8. This thread has been merged with 2 others that were about the same problem by the same user. Therefore you may have some trouble following the discussion. However, all 3 threads had valid responses so they are all here.

9. ## It worked

Thanks everyone (especially Wyatt_Earp). Using the examples I was able to correct my own program (and shorten it) so that I could include the while loop I needed to complete the program.

Popular pages Recent additions