C++ :: Arrays and Functions

Started by
1 comment, last by swiftcoder 15 years, 5 months ago
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
Advertisement
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.
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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement