C language only: You need to download the following L ab 10 .c , Lab10 _main.c a
ID: 3707048 • 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
//The following code is written in c as per the problem statement and is self explainatory
The following code will be used to compute the average balance:
1. double computeAverageBalance(struct cellData list[], int listsize);
{
double avg, sum;
int loop;
sum = avg = 0;
for(loop = 0; loop < listSize; loop++) {
sum = sum + list[loop].balance;
}
avg = (double)sum / loop;
return avg ;
}
//The following code will be used to sort by balance:
2.void sortByBalance(struct cellData list[], int listsize);
{
for (i = 0; i < listsize; ++i)
{
for (j = i + 1; j < listsize; ++j)
{
if (list[i].balance > list[j].balance)
{
a = list[i].balance;
list[i].balance = list[j].balance;
list[j].balance = a;
}
}
}
}
//The following code will be used to compute average balance with 317 area code:
3.double computeAverageBalance_with_317(struct cellData list[], int listsize);
{
{
double avg, sum;
int loop;
int NoOfiTems_317_Balance;
sum = avg = 0;
NoOfiTems_317_Balance = 0;
for(loop = 0; loop < listSize; loop++) {
if (list[loop].areacode == "317")
{
sum = sum + list[loop].balance;
NoOfiTems_317_Balance = NoOfiTems_317_Balance + 1;
}
}
avg = (double)sum / NoOfiTems_317_Balance;
return avg ;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.