Need Help Solving a Problem!

Hey guys, I'm very new to this stuff, I have never felt as helpless as I do in my Algorithms course that is centered around C++.

This is the problem I recieved

"Create a function copy_int_array which takes an array of integer A , a minimum (min) and maximum (max) array index as input arguments and output an array B of integer (already created elsewhere) which is an exact copy of A[min..max] (all value of A between min and max index are copied inside B)
Hint: arrays are passed to functions as reference arguments."

This is my current code (fill me in on the lingo if I sound like a noob as well)

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
26
27
#include <cstdlib>
#include <iostream>

using namespace std;

void copy_int_array(int A[], int B[], int min, int max);
     int i, n;
     for (i=0; i<n; i++){
         cout<<endl<<"Array A is Copied to Array B"<<endl;
         B[i]=A[i];
}

const int MAX_N=1000;
int main()
{
    int NA=10;
    int A[MAX_N]={5,2,8,6,1,4,7,3,9,0};
    int B[MAX_N];
        copy_int_array(A,B,0,9);
    
    copy_int_array(A,B,0,9);
    cout<<B[];
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Hopefully you guys can steer me in the right direction, saving me money from having to get tutors.
Hi !
Don't despair you're very close !

It's mostly little things. Remember how to create a function ? There has to be a prototype (usually above main()) and a matching definition (usually bellow main()). Line 6 is the prototype - so what is the syntax missing for the definition ?

Another thing is the bounds. Line 17 declares A with to be 10 elements but line 21 is sending the 'max' index as 9 elements.

Line 7 is useless for 2 reasons. 'int i' should be a part of the for loop
for (int i=0;...)
and the 'n' is never assigned a value :/

But, is the question asking to copy the whole array A into B ? No, it's asking for a range from min to max right ? So why not use those as the range in the 'for' loop ?

Line 22 will cause errors since u can only access an element of the array B. So u'd have to print them 1 by 1

Last edited on
Topic archived. No new replies allowed.