5 Replies - 495 Views - Last Post: 20 March 2013 - 09:38 PM Rate Topic: -----

#1 dmartell1856  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-March 13

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.





#include<stdio.h>
#include<stdlib.h>
#include <math.h>
#define SIZE 6


//function prototypes

void inputData (float array1[], int count);
void processData (float array1[], float *sum, float *avg);
void outputData (float array1[], float sum, float avg);
void selectionSort (float array1[], int length);
void swap (float array1[], int first, int second);



// Global variable declarations

float array1[SIZE] = {0}; //initializing array1 to all 0's
float avg = 0;  //avg of array elements
int count = 0;
float sum = 0;   // sum of array elements
int k = 0;     // counter for sum
int counter = 5; //counter for output function



int main(void)
{

        //calling functions
        
        inputData (array1, count);
        //processData (array1, &sum, &avg);
        //OutputData (array1, sum, avg);
       

        //I moved the end output statement here just for diagnostics
        while(counter>=0)
        {
               
         printf("%f ", array1[counter]);
         --counter;
 
        } 
      system("pause");
      return 0;
   
}

// Input function

void inputData (float array1[], int count)
{
  puts("Please enter 6 expected time values. Use positive whole numbers only.  \n\n");

 
  while(count<SIZE)
  {
   printf("\n");
   scanf("%f", array1);

   //this printf() statement is only here to check input. It's currently printing the correct value on the first pass, but 0's afterwards
   printf("%f", array1[count]);
                 
              //validation that time > 0 
               while(array1 < 0 ) 
               {
                            puts("Please enter only positive whole integers.  Try again.");
                            scanf("%f", &array1);
               }
   
   count++;                
  }
 
        
  return;
}



//processing function
void processData (float array1[SIZE], float *sum, float *avg)
{
 
 selectionSort(array1, SIZE);
 
      while(k<SIZE)
     {
     *sum=*sum+array1[k];  //calculating sum
     k++;
     }
     
     *avg = *sum/SIZE; // calculating float average
  
     return; 
}



//output function
void outputData (float array1[SIZE], float sum, float avg)
{
    
    puts("\n\nThe times entered, from highest to lowest, are:\n\n");
    
     //while conditions print ascending array in reverse order, highest to lowest
    while(counter>=0)
        {
               
         printf("%f ", array1[counter]);
         --counter;
 
        } 
        
        printf("\n\nThe average of the times entered is %f\n\n\n\n", avg); 
        system("pause");
        return;
}

//selection sort function
void selectionSort( float array1[], int length )
{
   int smallest; // index of smallest element 
   int i, j; // used for loops
    
   //loop over length - 1 elements 
   for ( i = 0; i < length - 1; i++ ) 
   {
      smallest = i; // first index of remaining array 
        
      // loop to find index of smallest element 
      for ( j = i + 1; j < length; j++ )
         if ( array1[ j ] < array1[ smallest ] )
            smallest = j;
      swap (array1, i, smallest);
   } //end for
} //end selectionSort


// swap function for selectionSort
void swap (float array1[], int first, int second)
{
     int temp; 
     temp = array1[first];
     array1[first] = array1[second];
     array1[second] = temp;
     return;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Float array not saving input

#2 Switters  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 110
  • Joined: 03-June 12

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.
Was This Post Helpful? 0
  • +
  • -

#3 dmartell1856  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-March 13

Re: Float array not saving input

Posted 20 March 2013 - 08:48 PM

View PostSwitters, 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?

//Assignment 3
//CIT 145 - Dr. Grant
//Written by Dolard Martell - 3/15/13 - 3/19/13

#include<stdio.h>
#include<stdlib.h>
#include <math.h>
#define SIZE 6


//function prototypes

void inputData (float array1[], int count);
void processData (float array1[], float *sum, float *avg);
void outputData (float array1[], float sum, float avg);
void selectionSort (float array1[], int length);
void swap (float array1[], int first, int second);

int main(void)
{
float array1[SIZE] = {0}; //initializing array1 to all 0's
float *avg = 0;  //avg of array elements
float *sum = 0;   // sum of array elements
int counter = 5; //counter for output function
int count = 0; 
        //calling functions
        
        inputData (array1, count);
        //processData (array1, &sum, &avg);
        //OutputData (array1, sum, avg);
       while(counter>=0)
        {
               
         printf("%f ", array1[counter]);
         --counter;
 
        } 
      system("pause");
      return 0;
   
}

// Input function

void inputData (float array1[], int count)
{

  puts("Please enter 6 expected time values. Use positive numbers only.  \n\n");

 
  while(count<SIZE)
  {
   printf("\n");
   scanf("%f", array1);

   
   printf("%f", array1[count]);
                 
              //validation that time > 0 
               while(array1 < 0 ) 
               {
                            puts("Please enter only positive whole integers.  Try again.");
                            scanf("%f", &array1);
               }
   
   count++;                
  }
 
        
  return;
}



//processing function
void processData (float array1[SIZE], float *sum, float *avg)
{
 int k = 0;     // counter for sum
 
 selectionSort(array1, SIZE);
 
      while(k<SIZE)
     {
     *sum=*sum+array1[k];  //calculating sum
     k++;
     }
     
     *avg = *sum/SIZE; // calculating float average
  
     return; 
}



//output function
void outputData (float array1[SIZE], float sum, float avg)
{
    int counter = 5; //counter for output function
    puts("\n\nThe times entered, from highest to lowest, are:\n\n");
    
     //while conditions print ascending array in reverse order, highest to lowest
    while(counter>=0)
        {
               
         printf("%f ", array1[counter]);
         --counter;
 
        } 
        
        printf("\n\nThe average of the times entered is %f\n\n\n\n", avg); 
        system("pause");
        return;
}

//selection sort function
void selectionSort( float array1[], int length )
{
   int smallest; // index of smallest element 
   int i, j; // used for loops
    
   //loop over length - 1 elements 
   for ( i = 0; i < length - 1; i++ ) 
   {
      smallest = i; // first index of remaining array 
        
      // loop to find index of smallest element 
      for ( j = i + 1; j < length; j++ )
         if ( array1[ j ] < array1[ smallest ] )
            smallest = j;
      swap (array1, i, smallest);
   } //end for
} //end selectionSort


// swap function for selectionSort
void swap (float array1[], int first, int second)
{
     int temp; 
     temp = array1[first];
     array1[first] = array1[second];
     array1[second] = temp;
     return;
}






OUTPUT:

Please enter 6 expected time values. Use positive numbers only.



1
1.000000
2
0.000000
3
0.000000
4
0.000000
5
0.000000
6
0.0000000.000000 0.000000 0.000000 0.000000 0.000000 6.000000 Press any key to c
ontinue . . .
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4290
  • View blog
  • Posts: 13,454
  • Joined: 25-December 09

Re: Float array not saving input

Posted 20 March 2013 - 09:06 PM

Look closely at the following snippet:
void inputData (float array1[], int count)
{
.... 
  while(count<SIZE)
  {
   printf("\n");
   scanf("%f", array1);


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

This post has been edited by jimblumberg: 20 March 2013 - 09:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 dmartell1856  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-March 13

Re: Float array not saving input

Posted 20 March 2013 - 09:25 PM

View Postjimblumberg, on 20 March 2013 - 09:06 PM, said:

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


Yes, that's because I previously sorted in ascending order, but we are required to print it in descending order. But thank you very much for the other advice. I'm looking into those things right now.
Was This Post Helpful? 0
  • +
  • -

#6 dmartell1856  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-March 13

Re: Float array not saving input

Posted 20 March 2013 - 09:38 PM

View Postjimblumberg, on 20 March 2013 - 09:06 PM, said:

Look closely at the following snippet:
void inputData (float array1[], int count)
{
.... 
  while(count<SIZE)
  {
   printf("\n");
   scanf("%f", array1);


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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1