Pointer Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Undermine
    New Member
    • Oct 2008
    • 6

    Pointer Question

    [PHP]#include <iostream>
    #include <vector>


    using std::cout;
    using std::cin;
    using std::endl;


    int main(void)
    {
    int x;
    x=0;
    while ( x < 3 )
    {
    int m1;
    int m2;
    int m3;
    int a[] ={m1,m2,m3};
    cout<<"Enter the Miles used (-1 to quit):" << endl;
    cin>> a[x];

    cout<<"Enter gallons:" << endl;
    int f1;
    int f2;
    int f3;
    int b[] ={f1,f2,f3};
    cin>> b[x];
    int k1;
    int k2;
    int k3;
    int MPG[]={k1,k2,k3};

    MPG[x]=a[x]/b[x];
    int * mypointer;
    mypointer=MPG[x]; *mypointer=???? ?;
    mypointer++; *mypointer=???? ?;
    mypointer = &MPG[x]; *mypointer=???;
    for (int n=0; n<3; n++)
    cout << MPG[n] << ", ";

    cout << "MPG this tankful:" << MPG[x] << endl;
    // cout << "Total MPG:" << TMPG << endl;
    x++;


    }



    char holdscr;
    cout << "\n\n\t*** Enter a character and press return to exit the program! ***\n\n\t"<< std::endl;
    cin >> holdscr;

    return 0;
    }[/PHP]

    I have no idea what im doing, but it's supposed to keep the values of MPG and then i need to average them at the end so i need to save 3 different output values.

    However I don't know how to specify it in the ???? mark part, I need it to point to the First value of my MPG[x] array and store the # in a variable or something.
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Get rid of the m, f, and k variables. When you create an array, you don't have to declare all the items in it separately. Declare a, b, and MPG outside of the while loop. And give them better names. It should be something like:
    Code:
    int miles[3];
    int gallons[3];
    int MPG[3];
    As for this code:
    Code:
    int * mypointer;
    mypointer=MPG[x]; *mypointer=?????;
    mypointer++; *mypointer=?????;
    mypointer = &MPG[x];  *mypointer=???;
    for (int n=0; n<3; n++)
        cout << MPG[n] << ", ";
    
     cout << "MPG this tankful:" << MPG[x] << endl;
     // cout << "Total MPG:" << TMPG << endl;
    I'm sure you don't have to do anything with mypointer. It looks like you're making this harder than it needs to be. Also, I don't think this code should be in the while loop; you don't want to do this three times, do you? Please explain what it's supposed to do. Thanks.

    Comment

    • Undermine
      New Member
      • Oct 2008
      • 6

      #3
      Originally posted by boxfish
      Get rid of the m, f, and k variables. When you create an array, you don't have to declare all the items in it separately. Declare a, b, and MPG outside of the while loop. And give them better names. It should be something like:
      Code:
      int miles[3];
      int gallons[3];
      int MPG[3];
      As for this code:
      Code:
      int * mypointer;
      mypointer=MPG[x]; *mypointer=?????;
      mypointer++; *mypointer=?????;
      mypointer = &MPG[x];  *mypointer=???;
      for (int n=0; n<3; n++)
          cout << MPG[n] << ", ";
      
       cout << "MPG this tankful:" << MPG[x] << endl;
       // cout << "Total MPG:" << TMPG << endl;
      I'm sure you don't have to do anything with mypointer. It looks like you're making this harder than it needs to be. Also, I don't think this code should be in the while loop; you don't want to do this three times, do you? Please explain what it's supposed to do. Thanks.
      Hello boxfish thanks for the help, the pointers are supposed to store the value given by MPG, because each time in the loop i will assign different values for miles and gallons and i need it to store the output for each loop into a certain variable so i can average them later.

      Basically im trying to reference as an elemental in my array, how do i do that. Say i want the first element in my MPG array, how do i reference it ?

      [PHP]val1=*(&MPG[x]); [/PHP]

      that gives me all of it but i just want the first element in it.
      Last edited by Undermine; Oct 21 '08, 03:32 AM. Reason: more input

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        You're storing three different values the MPG array. After the loop, just sum them all up, divide by three, and print it out. No pointer variables nessecary.

        Comment

        • boxfish
          Recognized Expert Contributor
          • Mar 2008
          • 469

          #5
          Sorry if this thread is dead, but I didn't see your edit. To access the first element of MPG, you would use MPG[0], the second is MPG[1], and the x+1'th is MPG[x]. I don't know if you still need to know this, but I hope it helps.

          Comment

          Working...