C language only: You need to download the following L ab 10 .c , Lab10 _main.c a
ID: 3707047 • Letter: C
Question
C language only:
You need to download the following
Lab10.c,
Lab10_main.c and
Lab10.h.
In this lab we will have 3 files, lab10.c, lab10_main.c and lab10.h. All your work will be done in the lab10_main.c, you don't need to modify lab10.h and lab10.c. The program consists of two array list[], this is an array of char pointers and STRList[]this array is an array of struct cellData.
Each char pointer is list points to a string of the form
“Name^Account Number^STATE^Area Code^Mobile Number^Balance”
Example
“First Last^2134^IN^317^3411122^245.67”
You are to write two functions
ComputeAverageListBalance and SortListByBalance that will operate on the list []
double computeAverageListBalance(char *list[], int listsize);
void sortListByBalance(char * list[], int listsize);
Each array elements in the array STRList have the following format :
struct cellData{
char Name[50];
char account[5];
char state[3];
char areaCode[4];
char mobileNumber[8];
double balance;
}
The account number is unique and it is 3 or 4 digits, the state is a two character abbreviation, the area code is 3 digits, the mobile number is 7 digits and the balance is a floating value
In this part of the your program you are asked to calculate the average of the balancesand to sort based on the balance, compute average balance of all accounts with a 317 area code
You should declare and define the following functions :
double computeAverageBalance(struct cellData list[], int listsize);
void sortByBalance(struct cellData list[], int listsize);
double computeAverageBalance_with_317(struct cellData list[], int listsize);
Your main (it is provided to you) should look like this
int main()
{
struct cellData list[25]={NULL};
int seed=715;
int i,listsize;
listsize= generate_list(list, seed);//I wrote in lab9.c generates random strings
printf(" original list *************** ");
?for(i=0;i< listsize; i++)
??printf("%s ",list[i]);
?printf(" sort by balance *************** ");");
?sortByBalance(list, listsize);
?for(i=0;i< listsize; i++)
???printf("%s ",list[i]);
?printf(" ");
?printf("the average balance is %.2lf m", computeAverageBalance(list, listsize));
printf(" sort by Account Number *************** ");");
?sortByAccountNumber(list, listsize);
?for(i=0;i< listsize; i++)
???printf("%s ",list[i]);
?printf(" ");
?quit(list, listsize); //I wrote in lab9.c ignore but needed
}
The function generate_list will generate a random list of size listsize (which is <=25). This is written for you in lab9.c
You need to write your functions (they have a shell of the function) in lab9.c
Explanation / Answer
This is the code for the functions that were needed to be completed. I think the questions are mixed up since at first it asks to edit lab10_main.c file and then at the end it asks to write the functions in lab9.c file. However, I will just provide the code for the functions that are mentioned in the question. There are in total of five functions that I will declare and complete. These are the functions that were mentioned in the question-
double computeAverageListBalance(char *list[], int listsize);
void sortListByBalance(char * list[], int listsize);
double computeAverageBalance(struct cellData list[], int listsize);
void sortByBalance(struct cellData list[], int listsize);
double computeAverageBalance_with_317(struct cellData list[], int listsize);
//Code starts here
void sortByBalance(struct cellData list[], int listsize){
int i,j;
//Sorting using selection sort since the data most probably isn't going to be big
for(i=0;i<listsize-1;i++){
int min_idx=i;
for(j=i+1;j<listsize;j++){
if(list[j].balance < list[min_idx].balance)
min_idx=j;
}
struct cellData temp = list[min_idx];
list[min_idx] = list[i];
list[i] = temp;
}
}
double computeAverageBalance(struct cellData list[], int listsize){
double totalBalance=0;
int i;
for(i=0;i<listsize;i++){
totalBalance += list[i].balance;
}
double averageBalance = totalBalance/(double)listsize;
return averageBalance;
}
double computeAverageBalance_with_317(struct cellData list[], int listsize){
double totalBalance_with_317=0;
int num_ac_with_317=0,i;
for(i=0;i<listsize;i++){
if(strcmp(list[i].areaCode, "317") == 0){
//to use strcmp you need to include <string.h> file
totalBalance_with_317 += list[i].balance;
num_ac_with_317 += 1;
}
}
double average = totalBalance_with_317/num_ac_with_317;
return average;
}
void sortListByBalance(char *list[], int listsize){
//in this function the data is passed as an array of strings
//therefore first we'll need to take out the balance from the string and then sort according to that
//We'll read the string forward and start taking out the balance after encountering fifth '^'
int i,j,k;
for(i=0;i<listsize-1;i++){
int min_idx=i;
char* min_balance;
int count = 0;
for(j=0;list[i][j]!= '';j++){
if(list[i][j] == '^') count++;
if(count==5) break;
}
strncpy(min_balance, list[i] + j + 1, sizeof(list[i]) - j - 1);
for(k = 0;k<listsize;k++){
char* curr_balance;
int ccount = 0, l;
for(l=0;list[k][l]!= '';l++){
if(list[k][l] == '^') ccount++;
if(ccount==5) break;
}
strncpy(curr_balance, list[k] + l, sizeof(list[k]) - l - 1);
if(strcmp(curr_balance, min_balance) < 0) {
strcpy(min_balance, curr_balance);
min_idx=k;
}
}
char* temp;
strcpy(temp, list[i]);
strcpy(list[i], list[min_idx]);
strcpy(list[min_idx], temp);
}
}
double computeAverageListBalance(char *list[], int listsize){
int i,j;
double totalBalance=0;
for(i=0;i<listsize;i++){
double balance;
char* balance_in_string;
for(j=0;list[i][j]!= '';j++){
if(list[i][j] == '^') count++;
if(count==5) break;
}
strncpy(balance_in_string, list[i] + j + 1, sizeof(list[i]) - j - 1);
sscanf(balance_in_string, "%lf", balance);
totalBalance += balance;
}
double average = totalBalance/(double)listsize;
return average;
}
//code ends here
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.