对于下面的函数,我必须按年龄按降序排序一系列员工,并且需要大小但不能将它放在函数变量中,我的老师告诉我通过在main中执行此操作来计算大小但是对于循环用j中的它告诉我elementSize可以在这个函数中使用unitialized。在main中它表示int elementSize =“”elementSize是一个未使用的变量。
int elementSize = sizeof(employeeList)/sizeof(Employee);//getting the size of the list
主
#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
}
答案 0 :(得分:2)
在此代码中,您实际上有两个名为elementSize
的变量,一个是bubbleSort
的全局变量,另一个是elementSize
的本地变量。局部变量阴影全局变量,意味着全局变量不可见。只有本地可见,并且本地从未初始化。
删除本地....
Application.ScreenUpdating = False
ShowPDFs "C:\Test\Working\", ws
ws.UsedRange.EntireColumn.AutoFit
Application.ScreenUpdating = True
End Sub
------------------------------
Private Sub ShowPDFs(ByRef fsoPath.......
变量,全局变量将变为可见并将被使用。