Solved

array initilization.

Posted on 2003-10-22
7
166 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now