-1

Whenever I tell it to print out what is in my array it prints out a memory address. I am a beginner.

#include <iostream>
#include <cstdlib>
#include <algorithm>


using namespace std;
int main()
{
    const int Array_Size = 6;
    int The_array[Array_Size] ={ 30, 60, 20, 50, 40, 10 };

    for(int starting_index = 0; starting_index < Array_Size; starting_index++)
    {
        int smallestindex = starting_index;

        for (int current_index = starting_index + 1; current_index < Array_Size; current_index++)
        {
            if (The_array[current_index] < smallestindex) 
            {
                smallestindex = current_index;
            }
            swap(The_array[current_index], The_array[smallestindex]);
        }
        cout<<(The_array);

    }


    cin.get();
    system("pause");
    return 0;
}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • 4
    It is going to take you more than 300 years to learn C++ like this. You should try reading [some good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – juanchopanza Jul 01 '14 at 20:35
  • @juanchopanza I already have a book but I just figure I could learn a lot more on the internet than in one book. – user3768136 Jul 01 '14 at 20:43
  • other way around, you learn a lot more from one book than from the internet – M.M Jul 02 '14 at 02:03

4 Answers4

1

There is no builtin function for outputting an array (or other container). This is because it's up to you to decide how you want to separate the items. For example, do you want 10 20 30 40 50 60? Or 10, 20, 30, 40, 50, 60? Or something else.

So you have to write code to do this. A simple way is:

for (auto item : The_Array )
    cout << item << " ";
M.M
  • 138,810
  • 21
  • 208
  • 365
0

To print the elements in the array, you need to iterate over the array (like you're doing) and print the element at the current index, rather than the entire array. To access an element at a given index, use the operator[], like so:

Instead of: cout<<(The_array); (which prints the address of the first element in the array) use cout << The_array[starting_index]; (which gets the element at starting_index from the array and prints it) wherein starting_index is the index of the array each time the loop iterates.

As an aside, while starting_index is the first index in the array during the first iteration, each loop will cause it to increase by 1 (so the second time the loop iterates, starting_index will be 1, the third time, 2, and so on). Thus starting_index is probably a bad name, although the compiler won't care.

In general, if you want to print the contents of an array called arr of size ints, do this:

for(int i=0; i<size; i++)
{
    // prints the element followed by a new line, so each
    // element is printed on its own line
    cout << arr[i] << "\n";
}
weberc2
  • 7,423
  • 4
  • 41
  • 57
  • Please consider explaining your answer to improve its quality and to help others understand it better. – TylerH Jul 01 '14 at 20:51
0

The_array is an address (the address of the array). To print elements you must pass an element to cout.

for (int i = 0; i < Array_Size; i++)
    cout << The_array[i];
ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
-2

Use for loop to cout each value in the elements

Array[5]={1,2,3,4,5};
For(int i=0;i<4;i++}
{ cout<<array[i];}     \\ we have an integer i that is equal to 0 so 0 is less than  4( because as you know in array the first value is represented as 0) so will increment it by one each time it loops so it will display from 0 to 4 \\ it will display from 12345 but represented as 01234 total 5 elements !
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Sardor
  • 1
  • 2