dylan
dylan

Reputation: 47

uninitialized variable in function

for the function below i have to sort an array of employees by age in descending order and need the size but cant put it in the function variables, my teacher told me to compute the size by doing this in main but for the loop with j in it it tells me elementSize may be used unitialized in this function. In main it says the int elementSize = " " elementSize is an unused variable.

int elementSize = sizeof(employeeList)/sizeof(Employee);//getting the size of the list

main

#include "lab10.h"

int main() {

Employee employeeList[10000];
readfile(employeeList);

clock_t start,end;
double cpu_time_used;
start = clock();

int elementSize = sizeof(employeeList)/sizeof(Employee);//getting the size of the list
bubbleSort(employeeList);//call bubble sort function to sort list in descending order
printList(employeeList, 25);
end = clock();
cpu_time_used = (double)(end-start)/CLOCKS_PER_SEC*1000;
printf("Time used to compute sorting was %f ms.\n",cpu_time_used);


int bin30=binSearch(employeeList, 0, 9999, 30);//binary search for people of the age of 30
if(bin30== -1){
 printf("employees not found!\n");//if employee of age 30 arent found
} else {
 printf("%s %s %d %d\n", employeeList[bin30].fname, employeeList[bin30].lname, employeeList[bin30].age, employeeList[bin30].salary); //print out employees info
 printf("element present at index %d", bin30);
 printf("time used is %f ms\n\n",cpu_time_used);//record time 
}

 int bin130=binSearch(employeeList, 0, 9999, 130);//binary search for people of the age of 30
   if(bin130== -1){
   printf("employees not found!\n");//if employee of age 130 arent found
     } else {
    printf("%s %s %d %d\n", employeeList[bin130].fname, employeeList[bin130].lname, employeeList[bin130].age, employeeList[bin130].salary); //print out employees info
    printf("element present at index %d", bin130);
    printf("time used is %f ms\n\n",cpu_time_used);//record time 
  }

   return 0;
}

void bubbleSort(Employee employeeList[]) {

   int swap;
   int i=0;
   int j=0;
   for(i=0; elementSize > i; i++) {//for loop to sort 
     for(j=0; elementSize > j ;j++) {
        if(employeeList[j+1].age > employeeList[j].age) {//comparing the two values
           swap = employeeList[j].age;//swapping the temp variable with the employee
           employeeList[j].age= employeeList[j+1].age;//setting the lower employeee to [j]
           employeeList[j+1].age = swap;//setting the higher employee to [j+1]
        }//end of if statement
     }//end of second for loop
   }//end of first for loop

}


#include "lab10.h"

//This function reads from a file and creates an array of Employee structure containing their information. 

void readfile(Employee employeeList[]){
FILE *fp;
fp = fopen("employeeData.csv", "r");
int i = 0;
if (fp) {
      while (i<10000){
         fscanf(fp, "%[^,],%[^,],%d,%d\n",employeeList[i].fname,employeeList[i].lname,&employeeList[i].age,&employeeList[i].salary);
         i++;
       }
   }

  fclose(fp);
}


 void printList(Employee employeeList[], int size)
{  
int i=0;
for(i=0; i<size; i++)//going through the first 25 elements of the array
{
printf("%d ", employeeList[i].age);//printing off the elements
}
}

int binSearch(Employee employeeList[], int first, int last, int age)
 {
if(first > last){
 return -1;//base case
 }
 int mid= (first + last)/2;//finding middle of the array

if(age > employeeList[mid].age)
   return binSearch(employeeList, mid+1, last, age);//searching for high in 
  binary search through recursion
 else if(age < employeeList[mid].age)
 return binSearch(employeeList, first, mid-1, age);//searching for low in binary search through recursion
 else 
 return mid;//return the expected value

}

Upvotes: 2

Views: 70

Answers (1)

dbush
dbush

Reputation: 223882

You actually have two variables named elementSize in this code, one global and one local to bubbleSort. The local variable is shadowing the global variable, meaning that the global is not visible. Only the local is visible, and that local was never initialized.

Remove the local elementSize variable and the global one will become visible and will be used.

Upvotes: 2

Related Questions