
Newbie Array/Scanf Problem
: I am trying to write a program that has the user fill a dual
: array with floating point numbers. I can not seem to be able to
: get scanf to work if I use variables to represent the elements of
: the array. The two code fragments below were cut and pasted from
: an actual program. The first example where I use constants to
: represent the array elements in the scanf statment works just
: fine. The second however, where I use variables to substitute for
: the array elements, does not work. I get the error message that
: follows (By the way, I am using Turbo C++, v1.01, aka Second
: Edition. The only library I have included is stdio.h):
* I named it 'first list'.
:: float arr[5][2];
:: int i,j;
:: scanf("%f", &arr[0][0]);
:: printf("You entered %f", arr[i][j]);
:: OUTPUT Screen:
:: 3
:: You entered 3.000000
Does it work correctly ?! It's incredible..!!!
Two integer variables, i and j, is declared, but not initialized.
The variable must be initialized before
it used. It is very dangerous if you does not initialize the variable.
When you declared a variable, compiler allocates some memory area for
the variable. But nobody knows what values stored in the allocated area.
So, YOU MUST BE INITIALIZED VARIABLES BEFORE YOU USE THEM. Okay?
: ***********************************
* I named it 'second list'.
: ***********************************
: float arr[5][2];
: int i,j;
: i=0;
: j=0;
above two lines,'i=0; / j=0;' means initialization.
Initialization is not difficult.
Initialization is that give a value to a variable before they be used.
: scanf("%f", &arr[i][j]);
: printf("You entered %f", arr[i][j]);
: OUTPUT Screen:
: scanf : floating point formats not linked
: Abnormal program termination
: ------------------------------------------
I think that the 'second list' works correctly
but the 'first list' works incorrectly.
: What am I doing wrong!!
: Please reply via e-mail. Thanks!
Thank you.