On Fri, 12 Dec 2003 20:38:18 +0200, Aryan Ameri wrote: > Hi There: > > I am a first year CS student, learning C. A while ago I was asked this > question from a fellow friend of mine: > > "Write a program, which promts the uset to enter some numbers. The user > should terminate the sequence of numbers by entering EOF character. The > program should put numbers entered by the user in to a 1D array". >
Aryan, I've read the other responses, all interesting. But I want to bring a bit of real-world focus to your problem. Firstly, the program specification defines the result of the program as a one-dimensional array containing the numbers which the user entered. That precludes you from terminating the program with the result in any other structure than a one-dimensional array, although there is nothing to stop you using whatever structures you wish during intermediate processing, as long as you end up with the numbers in a 1D array. Secondly, the spec says that the program is to prompt the user to enter some numbers. One thing you can know for sure, and another thing is implied: 1. What you know for sure: your program doesn't have to be fast. As long as it can keep up with someone typing numbers, it's OK. 2. Implied: someone is sitting at a keyboard entering numbers. It isn't going to be a lot of numbers. How many numbers would you be willing to sit and type in? If it were a lot of numbers, there would be another mechanism for collecting them. Therefore, it is reasonable to assume that the user will not type in more than, say, 100 numbers. So set your array size to 1000, and you've got it covered. Generate an error message if the user types in 1001. Another option: bearing (1) above in mind, you could simply write the numbers out to a temporary file and keep a count. Then, when the user finishes, simply allocate the array memory and read the numbers back in to the array. That way, you don't need to specify your array size in advance. And writing and reading back a simple file is easier to do and easier for a stranger to follow than messing with dynamic memory allocation. In the real world: Do the simplest thing that is consistent with the specification. Someone, whose skill level you can't predict, is going to have to maintain it after you. This takes quite a bit of discipline. The temptation is always to write something cool and clever. A lot of development (and support) time and money gets wasted that way. Oh, by the way: your spec says "numbers", not integers. Perhaps you ought to accommodate non-integers also. In the real world, that would be an immediate query to the program specifier. -- ....................paul I'll tell you what kind of guy I was. If you ordered a boxcar full of sons-of-bitches and opened the door and only found me inside, you could consider the order filled. -- Robert Mitchum -- To UNSUBSCRIBE, email to [EMAIL PROTECTED] with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]