Hi everyone,

Ive got a question concerning an excersise I have to make in wich I use an array in a function:

I have to manually fill an array with twenty numbers randomly and the array has to be used by a function, now, I know how to do this automatically like this:

void settabel (int tabel[MAX]);

void main (void)
void settabel int tabel[MAX])
for int (int i=0; i<MAX; i++)tabel=i+1;

In this case, the array gets filled automatically from 1 to 20, that's not a problem!

But how on earth can I use this type of function and manually with cin>> get that array filled with numbers??????

Thanks for any advice and help :!:

12 Years
Discussion Span
Last Post by Narue

>for int (int i=0; i<MAX; i++)tabel=i+1;

for (int i=0; i<MAX; i++)cin>> tabel[i];

>void main (void)
In C++ this must be:

int main()

You can keep the void argument, but it's really not necessary for symmetry like it is in C. But the important part is that main must return an int, it's non-negotiable.

You have several other errors, but I'll assume that's because you typed the code in manually and didn't check yourself.


Thanks Narue :p

You saved my day :cheesy:

Anyway, it's like you said, I didn't check the code because I only wanted you to understand what I was trying to do, the code I have written is only a small part of the code I need.

Idea is to use several funtions in combination with arrays and that those functions get called upon in the main program and you have to write several functions to do so.

Wich ones?
1) Make an array with 20 numbers filled by keyboartd(cin ;) ) and check two things, when zero is entered by cin -> break, when array is full -> stop the function. I believe I can do this in one function, right :?:

2) When 10 numbers are entered by keyboard, return to next line, this can be done by: cout<<setw(5)<<list;

3) Ask question, "Do you want to make another array?", this also can be done by a small funtion wich I don't have a problem in handling!

Anyway, if your able, could you give me your opinion on this and thanks again for the help, much appreciated!


>I believe I can do this in one function, right
Sure, though it's best to restrict functions to performing one operation. For example, a function to get a number from the user that treats 0 as a terminating condition can be reused in a great many programs. If the function also checks array boundaries, it isn't as flexible and you won't find yourself reusing code that you've already written and debugged.


So, if I understand you correctly, you're saying it's best to write a function for every operation that is needed and not to combine several operations in one function?

Sorry to ask a question you've explained, I'm not native English speaking and don't want to miss interpretate your reply :o


>you're saying it's best to write a function for every operation that is
>needed and not to combine several operations in one function?
Yes, provided you do it intelligently. There's a fine line between a well modularized program and an overcomplicated mess. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.