Solved

array initilization.

Posted on 2003-10-22
7
169 Views
Last Modified: 2009-12-16
#include <stdio.h>

int main(void) {

int i=0;
int numbers[20];

while(scanf("%d",&numbers[i])!=EOF) i++;

return 0;

}


// what's wrong in this code...
0
Comment
Question by:xinex
7 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9598365
What error are u facing...........????
0
 
LVL 3

Expert Comment

by:komar
ID: 9598405
Hi,

Syntax wise, your code has no errors. But it has few logical errors; the while loop in your code should have an extra condition to check for array boundry so that the index 'i' does not go beyond the aray limits, this could be done this way:

      while(i < 20 && scanf("%d", &numbers[i]) != EOF) i++;

I added a check on the value of 'i' so that 'i' will not go beyond the array maximum index which is '19' in your case.
Also, the check on the index is placed before the scanf() function so that it is executed first.

Thanks,
Khalid Omar


0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9598418
scanf  function returns the number of input items assigned
when you try to give EOF, it is assigned to numbers[i] and 1 is returned !! which is not equal to EOF

perhaps you want this

do {
       scanf("%d",&numbers[i]);
        i++;
} while (numbers[i-1] != EOF);
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9600057

Hi Sunnycoder,

I don't think that that is the answer, either.  scanf() returns EOF, it doesn't store EOF as a data value.

The original code should work if the input source is a data file and NOT the keyboard.  This should work fine:

program.exe < DataFile


gets() returns EOF if a lone carriage return is entered from the keyboard.  (Don't use gets()!)  scanf() does not return EOF when input is from the keyboard so other trappings need to occur.

Kent
0
 
LVL 6

Expert Comment

by:Ajar
ID: 9604498
Revised verion of the original code that should not give errors is :

int main(void)
{
int i=0;
int numbers[20];
while(scanf("%d",&numbers[i]) && i < 20 && numbers[i]!=EOF ) i++;
return 0;
}

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9604530
you are right Kent... I shud have read the man page completely

using fgets() might help
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 20 total points
ID: 9605827

Hi Sunnycoder,

fgets() doesn't return EOF from the keyboard, either.  You'd still have to test for a line-length of zero, which would also mean that blank lines are no longer allowed.


Hi Ajar,

The test "numbers[i]!=EOF" is the same thing that Sunnycoder tripped over.  scanf() won't store EOF at numbers[i] when EOF is encountered.  Since EOF is generally defined to be -1, your loop will exit if scanf() stores a -1.



There needs to be two test cases.  One for keyboard input and one for file input.

Kent
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question