Function[Array] in combination with cin>>

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Function[Array] in combination with cin>>

 
0
  #1
Oct 16th, 2004
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 :!:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Function[Array] in combination with cin>>

 
0
  #2
Oct 16th, 2004
>for int (int i=0; i<MAX; i++)tabel[i]=i+1;
  1. for (int i=0; i<MAX; i++)cin>> tabel[i];
>void main (void)
In C++ this must be:
  1. 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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Function[Array] in combination with cin>>

 
0
  #3
Oct 17th, 2004
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Function[Array] in combination with cin>>

 
0
  #4
Oct 17th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Function[Array] in combination with cin>>

 
0
  #5
Oct 17th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Function[Array] in combination with cin>>

 
0
  #6
Oct 17th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC