Float array not saving input

Posted 20 March 2013 - 07:12 PM

Hello all. This is my first post, and I'll try to follow all the protocol. Please forgive me (and let me know) if I do or say something wrong. I have to write a program using functions and arrays in which the user inputs 6 values into the array, then a separate function sorts them and calculates average, then passes it back to main then to output function. Easy-peesy, right? Well, I had it 100% good to go, but I was using roundf() to prevent errors in the int input type in the array. Then my prof informed me that I need to retain the decimals, so I need to change the array type to float or double. Well, when I change it to float, everything goes haywire. I commented out the processing and output functions and put an output expression to immediately after the input to diagnose and have at least kindof isolated the problem that way. When I run this, the printf() matches the input the first time (sweet!) but is 0 on all subsequent runs. I hope it's just something really dumb and simple I overlooked. Where am I messing up? Thanks.

Replies To: Float array not saving input

Re: Float array not saving input

Posted 20 March 2013 - 07:15 PM

Your use of global variables is going to cause you headaches. You should declare them locally, in main(), and then go from there. At this stage of the game you need to get a grasp of parameter passing anyway.

Re: Float array not saving input

Posted 20 March 2013 - 08:48 PM

Switters, on 20 March 2013 - 07:15 PM, said:

Your use of global variables is going to cause you headaches. You should declare them locally, in main(), and then go from there. At this stage of the game you need to get a grasp of parameter passing anyway.

Ok. You're right about that, but that doesn't seem to be where my problem lies. I moved all global variables into main() or the functions they are called in, but my output is unchanged. The first input is stored and recalled correctly, but loops 2-6 return 0's. Where is my error that is causing this particular issue?

Re: Float array not saving input

Look closely at that scanf(). Why are you always trying to get a number from the user and placing it into the first element of your array. Don't you want to fill in each element of your array? I really think you probably want something like:

scanf("%f", &array1[count]);

You are doing things like this in several places.

Also getting entry into an array is usually handled by a for loop instead of the while loop.

for(count = 0; count < SIZE; count++)

Also be careful when you go to actually use your pointer variables, be sure to properly allocate memory for these pointers.

Also in the following snippet from main():

while(counter>=0)
{
printf("%f ", array1[counter]);
--counter;
}

Do you realize that you are printing the last element first and the first element last?

Look closely at that scanf(). Why are you always trying to get a number from the user and placing it into the first element of your array. Don't you want to fill in each element of your array? I really think you probably want something like:

scanf("%f", &array1[count]);

You are doing things like this in several places.

Also getting entry into an array is usually handled by a for loop instead of the while loop.

for(count = 0; count < SIZE; count++)

Also be careful when you go to actually use your pointer variables, be sure to properly allocate memory for these pointers.

Also in the following snippet from main():

while(counter>=0)
{
printf("%f ", array1[counter]);
--counter;
}

Do you realize that you are printing the last element first and the first element last?

Jim

Thanks, Jim. That got me squared away. You are a gentleman and a scholar.