Solved

array initilization.

Posted on 2003-10-22
7
168 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 45

Expert Comment

by:Kdo
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:
Kdo 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

809 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