Solved

array initilization.

Posted on 2003-10-22
7
167 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
Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 nested-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.

932 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

11 Experts available now in Live!

Get 1:1 Help Now