Arrays Data Structures

This is a discussion on Arrays Data Structures within the C++ Programming forums, part of the General Programming Boards category; Hello, I am a new programmer and have been learning it pretty well on my own for a while. I ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Unhappy 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. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    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;
    Last edited by DougDbug; 09-18-2003 at 03:21 PM.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use proper thread titles next time. "Please help" doesn't mean anything.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    2
    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. #6
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    Originally posted by Hammer
    Use proper thread titles next time. "Please help" doesn't mean anything.
    Yeah, and it seems that your 'e' 'l' and 'p' keys are getting stuck. Maybe you need a new keyboard .

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    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.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Talking 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 subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 12:27 PM
  2. Replies: 2
    Last Post: 06-16-2005, 11:03 AM
  3. Replies: 4
    Last Post: 06-14-2005, 06:45 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  5. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21