cout arrays

Is there a way to cout an array?

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

void displayPrice(double money[])
{
 for(int i = 0; i < 5; i++)//I am making the array 5 elements

    {

        //using this as an example for a similar problem
       //

    }

       cout<<monkey[i]<<endl;//something like this. Can that be done?
                            //to print everything the array
}




I know the syntax is not accurate and I'm missing some code.
Yes, move line 13 to be inside the loop. Assuming the length of the array is at least 5.
And I assume you mean 'money' not 'monkey'.
Last edited on
Assuming the length of the array

In other words: the length of an array is not passed to a function unless you do it explicitly.
1
2
3
void displayPrice( double money[], int size )
{
  for ( int i = 0; i < size; i++ )

If you keep the hardcoded "5", then the function can be used only to print exactly 5 first values from array.

yes this has been solved by ganado, you would in fact be displaying 5 array values held at positions 0-4 of money/monkey
lol, I meant money lol. I need to start doing that in the future. Would you mind showing me how to use the same loop but using a pointer?
What do you mean? The money is a pointer.
When you pass in an array into a function, it already degrades into a pointer.

I really don't understand why schools force you to use this horrible notation, but:

monkey[i] is equivalent to *(monkey + i) in pointer notation.
Last edited on
Heh heh heh:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void displayPrices( double money[], int size )
{
  if (!size) return;
  std::cout << "$" << money[0];
  for (int n = 1; n < size; n++)
    std::cout << ", $" << money[n];
  std::cout << "\n";
}

template <std::size_t N>
void displayPrices( double money[N] )
{
  displayPrices( money, (int)N );
}

:^}
I think I got it everyone. May not be the best looking piece of code, but its working. I hope I don't get a lot of point deductions. I really appreciate everyone's help. I would love to pick everyone's brain and learn more. If anyone is interested.
may as well … ask what you want to know.

theres another pointer approach, and its a good intro to the concept of an iterator.

char a[] = "abcde";
char* ap = a;
while(*ap)
{
cout << *ap;
ap++;
}

you basically advance a temporary pointer through the data, one location at a time, rather than index a base pointer + offset (which is what[] notation does for you).

Range based for loops and many stl tools work off this base concept. I used a c-string because it has an easy stop condition, but you can do it on any array/vector.
Last edited on
As I mentioned here ( http://www.cplusplus.com/forum/general/269246/#msg1158758 ):

When passing a regular array to a function it is recommended you also pass the array's size. The number of elements. Hard-coding that information limits the reusability of your function.


void printArray(int arr[], int arrSize) not void printArray(int arr[])
Thanks for the info everyone. I don't due a whole lot these days. Feel free to add me on snapchat or whatever. stay-strong365. Not a whole lot of adults to talk to lol.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

template <typename T, size_t SIZE>
std::ostream& operator<<(std::ostream& os, T (&arr)[SIZE]);

int main()
{
   int iarr[] { 1, 2, 3, 4, 5 };

   std::cout << iarr << '\n';

   double darr[] { 0.0, 0.1, 0.5, 5.3, -12.9 };

   std::cout << darr << '\n';
}

template <typename T, size_t SIZE>
std::ostream& operator<<(std::ostream& os, T(&arr)[SIZE])
{
   for (size_t index { }; index < SIZE; index++)
   {
      os << arr[index] << ' ';
   }
   return os;
}
Topic archived. No new replies allowed.