0

My code uses a for Loop to copy and paste the contents of V1 -> V2. I would like to see the output of v2 using cout but i am unsure where to put that line of code.

void copy_fct();

int main()
{

copy_fct();


}


void copy_fct()
{
    int v1[10] = {0,1,2,3,4,5,6,7,8,9};
    int v2[10];

    for (int i=0; i!=10; ++i){
        v2[i]=v1[i];

    }

}

  • 3
    Just print each element as you copy it? – Some programmer dude Feb 04 '20 at 10:49
  • This is my first day of learning how to code, so i'm not too sure how to do that. When I write cout << v2[5]; right under copy_fct(); in my main function, I get an error saying 'v2' was not declared. – Michael Karibi Feb 04 '20 at 10:53
  • Put the `cout << v2[i];` below the `v2[i]=v1[i];`. – Blaze Feb 04 '20 at 10:55
  • @MichaelKaribi `v2` is declared in `copy_fct`, so it's only accessible from there. – Maximouse Feb 04 '20 at 10:55
  • Hey @Blaze, I tried that and it says "error 'cout' was not declared in this scope". – Michael Karibi Feb 04 '20 at 10:57
  • 2
    Then I would argue that you're going to fast and need to slow down a bit. Do you have [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read? Then follow it, learning bit by bit. And if you don't have a good book, then I suggest you invest in one. It will help you in the long run. – Some programmer dude Feb 04 '20 at 10:58
  • Put `std::cout` instead of `cout`. Make sure `` is included. – Blaze Feb 04 '20 at 10:58
  • @Someprogrammerdude This is an exercise from the very first chapter of 'The c++ Programming Language - Fourth Edition - Bjarne Stroustrup' . However, in their example, they didn't print out the array and I wanted to print it out to see if it's working. – Michael Karibi Feb 04 '20 at 11:01
  • I would just like to thank everyone for your help! The answers below seem to have worked! – Michael Karibi Feb 04 '20 at 11:10

2 Answers2

1
#include <iostream>
void copy_fct();

int main()
{

copy_fct();


}


void copy_fct()
{
    int v1[10] = {0,1,2,3,4,5,6,7,8,9};
    int v2[10];

    for (int i=0; i!=10; ++i){
        v2[i]=v1[i];
        std::cout << v2[i] << " ";
    }
    std::cout << std::endl;

}
Michael Lukin
  • 829
  • 3
  • 9
  • 19
0

In your program design the name of the function copy_fct does not make great sense because it copies nothing passed to the function by the user. It deals with its local variables.

It seems you mean something like the following.

#include <iostream>

void copy_fct( const int a1[], size_t n, int a2[] )
{
    for ( size_t i = 0; i < n; i++ ) a2[i] = a1[i];
}

int main() 
{
    const size_t N = 10;
    int v1[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int v2[N];

    copy_fct( v1, N, v2 );

    for ( int item : v2 ) std::cout << item << ' ';
    std::cout << '\n';

    return 0;
}

The program output is

0 1 2 3 4 5 6 7 8 9 

The same task can be done using standard algorithms.

#include <iostream>
#include <iterator>
#include <algorithm>

int main() 
{
    const size_t N = 10;
    int v1[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int v2[N];

    std::copy( std::begin( v1 ), std::end( v1 ), std::begin( v2 ) );

    std::copy( std::begin( v2 ), std::end( v2 ), std::ostream_iterator<int>( std::cout, " " ) );

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335