| | |
Function[Array] in combination with cin>>
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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)
{
settabel(lijst):
}
void settabel int tabel[MAX])
{
for int (int i=0; i<MAX; i++)tabel[i]=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 :!:
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)
{
settabel(lijst):
}
void settabel int tabel[MAX])
{
for int (int i=0; i<MAX; i++)tabel[i]=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 :!:
>for int (int i=0; i<MAX; i++)tabel[i]=i+1;
>void main (void)
In C++ this must be:
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.
C++ Syntax (Toggle Plain Text)
for (int i=0; i<MAX; i++)cin>> tabel[i];
In C++ this must be:
C++ Syntax (Toggle Plain Text)
int main()
You have several other errors, but I'll assume that's because you typed the code in manually and didn't check yourself.
I'm here to prove you wrong.
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[i];
if(i%10==9)cout<<endl;
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!
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[i];
if(i%10==9)cout<<endl;
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.
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.
I'm here to prove you wrong.
![]() |
Similar Threads
- Function(array) Help! (C++)
- RemoveAll Function (for an Array) (C++)
- bubble sort function w/ array (C++)
Other Threads in the C++ Forum
- Previous Thread: need help with code
- Next Thread: Send Me The Code For Round Robin
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






