MickeyMoise
MickeyMoise

Reputation: 259

How do I output the first, the last, the second, the second to last etc elements in an array?

If I say I want 10 elements, for example 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10, I want the output to be 1, 10, 2, 9, 3, 8, 4, 7, 5, 6.

And I can't figure out a way to do it.

#include <iostream>

using namespace std;

int main()
{
    int n, v[1000], j=0;

    cin >> n;

    for(int i=0; i<n; i++){
        cin >> v[i];
    }
    for(int i=0; i<n; i++){
        cout << v[i] << " " << v[n - j] << " ";
        j++;
    }
    return 0;
}

Upvotes: 0

Views: 937

Answers (3)

Aditya Tomar
Aditya Tomar

Reputation: 1639

You might consider using a better way to declare your array using pointers and dynamic memory allocation. In this way, you won't have to initialize an array with a large number of elements, hoping that the user doesn't enter a number greater than 1000 for the array size.

#include <iostream>

using namespace std;

int main() {
    int n = 0;
    cin >> n;

    int* v = new int[n];

    for(int i=0; i<n; i++){
        cin >> v[i];
    }
    for(int i=0; i<(n/2); i++){
        cout << v[i] << " " << v[n - i - 1] << " ";
    }
    if(n%2) 
        cout << v[n/2];
    return 0;
}

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

You can write a separate function for any type of an array the following way as it is shown in the demonstrative program below. Take into account that in general an array can have either an odd or even number of elements.

#include <iostream>

template <typename T>
std::ostream & display( const T *a, size_t n, std::ostream &os = std::cout )
{
    for ( size_t i = 0, j = i; i != n; )
    {
        os << a[j] << ' ';
            
        j = j == i ? --n : ++i;
    }
    
    return os;
}

int main() 
{
    int a1[] = { 1 };
    int a2[] = { 1, 2 };
    int a3[] = { 1, 2, 3 };
    int a4[] = { 1, 2, 3, 4 };
    int a5[] = { 1, 2, 3, 4, 5 };
    int a10[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    display( a1, sizeof( a1 ) / sizeof( *a1 ) ) << '\n';
    display( a2, sizeof( a2 ) / sizeof( *a2 ) ) << '\n';
    display( a3, sizeof( a3 ) / sizeof( *a3 ) ) << '\n';
    display( a4, sizeof( a4 ) / sizeof( *a4 ) ) << '\n';
    display( a5, sizeof( a5 ) / sizeof( *a5 ) ) << '\n';
    display( a10, sizeof( a10 ) / sizeof( *a10 ) ) << '\n';

    return 0;
}

The program output is

1 
1 2 
1 3 2 
1 4 2 3 
1 5 2 4 3 
1 10 2 9 3 8 4 7 5 6 

Upvotes: 0

Sahadat Hossain
Sahadat Hossain

Reputation: 4351

You can do this like this way : at first let's think the n is even, so we can divide the length of the array in two equal part, now we will go to the first part and with every element of the first half of the array we will print the respective element from the last. Anf if n is odd then at the end we need to print the middle one.

#include <iostream>

using namespace std;

int main()
{
    int n, v[1000], j=0;

    cin >> n;

    for(int i=0; i<n; i++){
        cin >> v[i];
    }
    int nn = n/2;

    for(int i=0; i<nn; i++){
        cout << v[i] << " " << v[n - i-1] << " ";
    }
    if(n%2) cout << v[nn];
    return 0;
}

Upvotes: 2

Related Questions