Printing out Array Elements

This is a discussion on Printing out Array Elements within the C++ Programming forums, part of the General Programming Boards category; I am trying to print out the array elements while using pointers. My Code: Code: #include <iostream> using namespace std; ...

  1. #1
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72

    Printing out Array Elements

    I am trying to print out the array elements while using pointers.

    My Code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    int numEntries;
    
        cout << "How many entries? ";
        cin >> numEntries;
    
        double*dPtr = new double[numEntries];
    
        for( int i = 0; i < numEntries; i++ )
        {
            cout << "Enter your values: ";
            cin >> dPtr[i];
    
            cout << "The Array Elements Are: " << dPtr[i] << endl;
        }
    
    }
    What I am getting:
    Code:
    How many entries? 5
    Enter your values: 2
    The Array Elements Are: 2
    Enter your values: 4
    The Array Elements Are: 4
    Enter your values: 6
    The Array Elements Are: 6
    Enter your values: 8
    The Array Elements Are: 8
    Enter your values: 10
    The Array Elements Are: 10
    
    Process returned 0 (0x0)   execution time : 182.454 s
    Press any key to continue.
    I am new to C++

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Congratulations...
    (is there a question going with the post?)
    (guessing question: Is my code OK? answer: yes, it is perfectly fine)

  3. #3
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    it looks like it's working to me, is this more of what your trying to achive?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    int numEntries;
    
        cout << "How many entries? ";
        cin >> numEntries;
    
        double*dPtr = new double[numEntries];
    
        for( int i = 0; i < numEntries; i++ )
        {
            cout << "Enter your values: ";
            cin >> dPtr[i];
    
        }
    
            cout << "\nThe Array Elements Are: \n";
    
        for(int i = 0; i < numEntries; i++ )
        {
        cout << dPtr[i] << endl;
    
        }
    
    
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Is there a question in there somewhere? Looks like it ran fine.

    BTW, you should free up the memory you allocated to the dPtr pointer.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    To free up the memory would I do it like this?

    Code:
    double*dPtr = new double[numEntries];
    delete [] dPtr;
    And this is what is supposed to be done.


    In the program you will use a dynamic array of integers (one dimensional Array). The program will first ask how many elements you want to put in the array. Then it will create an array dynamically of the size specified by you. After that the program will ask for integer elements and fill up the array. Finally the program will display the array elements.

    Is my program correct by this?
    I am new to C++

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Yes, that's how you free up the memory.

    Not quite, Matty_Alan's post is more closer to what your program requirement describes. He uses a second loop after the first input loop to display the data back to the user.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    A very small note. Don't know if it is a typo but you shouldn't write
    Code:
    double*dPtr
    instead you should leave at least one space so your code is more clear, like
    Code:
    double* dPtr; //personal favorite
    or
    double *dPtr;
    or
    double * dPtr;
    or
    ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Left Shift elements of a 2D array
    By w2look in forum C Programming
    Replies: 8
    Last Post: 01-22-2009, 11:13 PM
  4. adding elements in array
    By reb221 in forum C Programming
    Replies: 6
    Last Post: 12-30-2008, 01:41 PM
  5. Replies: 2
    Last Post: 08-03-2003, 10:01 AM

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