Sign in to follow this  
xShrimp

C++ :: Arrays and Functions

Recommended Posts

xShrimp    122
Hey there, So I have a programming assignment due in about 5 hours, and I'm completely stuck on this one problem, and solving this one problem will get me through this assignment very quickly. I know what I want to do in my head, but when it comes to coding; errors everywhere. So my assignment is focusing on arrays. So what I'm stuck on is this: I've assigned the array, and size. int theArray[100]; int n; In this assignment, the size of the array not determined and is chosen by the user, but is only allowed a maximum of 100. Next is, I have to design a function that will prompt the user for array size, then the user enters values into the array in order from the beginning to the end of the array, as stated by the user. The problem is that when I declare function, so that whatever the user inputs into the array can be mapped back to 'theArray' in main. void readArray (int&, int&); Call it: readArray (theArray, n) And I only gave it a return to test of it work. Now, I built it (using Xcode), and these are the errors I get. http://img155.imageshack.us/my.php?image=picture1nz3.png Could someone please help, I'm really stuck. Thanks

Share this post


Link to post
Share on other sites
fpsgamer    856
First of all, this is how you declare an function which accepts a reference to an arary:


int myArray[100];

void eatArray(int(&a)[100])
{

}

int main()
{
eatArray(myArray);
}


Welcome to the wonderful world of little known C++ syntax :) The function you declared accepts a reference to an int, which is not the same thing as a reference to an array.

However this method doesn't allow the user to determine the size of the array at runtime since the function is typed to only accept arrays of size 100.What you want to do is accept a pointer to the beginning of an array. So your code should look something like this:


int* myArray;

void eatArray(int* a)
//void eatArray(int a[]) //This signature will also work
{

}

int main()
{
int arraySize;
cin >> arraySize;
myArray = new int[arraySize];
eatArray(myArray);
}



Hope that provides you a good starting point.

Share this post


Link to post
Share on other sites
swiftcoder    18437
Quote:
Original post by xShrimp
So I have a programming assignment due in about 5 hours, and I'm completely stuck on this one problem, and solving this one problem will get me through this assignment very quickly.
The forum rules specifically forbid homework problems, so all I can give you is generalised advise.

Quote:
int theArray[100];
int n;

void readArray (int&, int&);

readArray (theArray, n)
You are trying to pass an array of integers as an integer reference. This doesn't work, so consider what an array is, and what it should be passed as.

Quote:
Now, I built it (using Xcode), and these are the errors I get.
http://img155.imageshack.us/my.php?image=picture1nz3.png
In future, it is more helpful if you post the relevant part of your code, and the relevant compiler errors (from the XCode 'Build' panel), in [ source ] [/ source ] tags, rather than an image.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this