I'm getting weird letters and numbers when the array is being shown. What is the problem? I've initialized array1 with 10,20,...etc. So shouldn't it cout those numbers?

#include <iostream>
using namespace std;

void reverseArray(int[], int);

int main() {//start main

	const int size = 5; //constant size of array is 5
	int array1[size] = {10,20,30,40,50};

	int x;
	
	cout << array1 << endl;

	reverseArray(array1, size);


	cin >> x;

}//end main


void reverseArray(int array1[5], int size) {//start function

	//int temp;
	//bool swap;

	for(int count = 0; count < 4; count++)
		{//start for
			//int temp;

			array1[count] = array1[size];
			//array1[count] = temp;
			--size;
			

		}//end for

	cout <<array1;


}//end function

You do not output an array like that. You have to go through each element and output it. array1 just points to the first element in the memory block.

for( int i = 0; i < size; i++ )
    cout << array1[i] << endl;

This should do exactly what you want.

This is one way to do it. Just compare it to your code you will find the parts that I have changed.

#include <iostream>
using namespace std;

void reverseArray(int[], int);
void displayArray(int[], int);
void pause();

int main()
{
//    const int size = 10;
//	int array1[size] = {10,20,30,40,50,60,70,80,90,100};

    const int size = 5;
	int array1[size] = {10,20,30,40,50};

    displayArray(array1, size);

    cout << endl << endl;

    reverseArray( array1, size );
    displayArray( array1, size );

    pause();

    return 0; //you can ignore this line
}//end main


void reverseArray(int array1[], int size)
{
    int temp = 0;

    for ( int count = 0; count < size/2; count++ )
    {
         temp = array1[count];
         array1[count] = array1[size - count - 1];
         array1[size - count - 1] = temp;
    }

    return; //you can ignore this line
}//end function

void displayArray( int array[], int size )
{
    for ( int i = 0; i < size; i++ )
        cout << array[i] << endl;

    return; //you can ignore this line
}

void pause()
{
    cout << endl << "Press return to continue.";
    cin.get();

    return; //you can ignore this line
}

I think the code is clear enough, but you can ask me if you have any questions.

I'm getting weird letters and numbers when the array is being shown. What is the problem? I've initialized array1 with 10,20,...etc. So shouldn't it cout those numbers?

#include <iostream>
using namespace std;

void reverseArray(int[], int);

int main() {//start main

	const int size = 5; //constant size of array is 5
	int array1[size] = {10,20,30,40,50};

	int x;
	
	cout << array1 << endl;

	reverseArray(array1, size);


	cin >> x;

}//end main


void reverseArray(int array1[5], int size) {//start function

	//int temp;
	//bool swap;

	for(int count = 0; count < 4; count++)
		{//start for
			//int temp;

			array1[count] = array1[size];
			//array1[count] = temp;
			--size;
			

		}//end for

	cout <<array1;


}//end function

The way you output your array is ONLY acceptable for null-terminated char arrays (C-Style Strings). For any other type of array you have to do it with a loop. Inside the loop, you would output one element at a time using the subscripting operator.

Demonstration:

#include <iostream>
#include <cstring>

using namespace std;

int main() {
	const int stringLength = 50;
	const int intLength = 5;
	char cStyleString[stringLength] = "This should output fine.";
	int intArray[intLength];

	for (int i = 0; i < intLength; ++i) {		//populate the int array
		intArray[i] = i * 5;
	}

	cout << cStyleString << endl;								//this line should work fine
	cout << "This should be gibberish: " << intArray << endl;	//this line should not work right

	cout << "\nNow the actual int array:" << endl;
	for (int j = 0; j < intLength; ++j) {						//begin display of intArray
		cout << intArray[j] << " ";
	}
	cout << endl;

	cin.get();
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.