Lab 10 (this is to be turned in) You need to download the following Lab10.?. Lab
ID: 3707421 • Letter: L
Question
Lab 10 (this is to be turned in) You need to download the following Lab10.?. Labl0 main.c and LablO.h. In this lab we will have 3 files, labl0.c, labl0 main.c and lablO.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 STRListl 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 "Brian King 2134"IN*317*3411122*245.67" You are to write two functions ComputeAverageListBalance and SortListByBalance that will operate on the list D double computeAverageListBalance(char *list[], int listsize); void sortListByBalance(char *listl], 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 are asked to calculate the average of the balances In this part of the your program you and 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 listl, int listsize); double computeAverageBalance with 317(struct cellData listll, int listsize);Explanation / Answer
I cannot provide the main method for this as ths is not mentoned. Though am providing the methods as below: -
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;
}
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);
}
}
UPDATED Main file
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lab10.h"
double computeAverageBalance(struct cellData list[], int listsize);
void sortByBalance(struct cellData list[], int listsize);
double computeAverageListBalance(char *list[], int listsize);
void sortListByBalance(char * list[], int listsize);
/*
The program consists of an array list[], this array is an array of pointers which points to strings.
Each string has the following format :
“Name^Account Number^STATE^Area Code^Mobile Number^Balance”
Example
“Brian King^2134^IN^317^3411122^245.67”
Remember everything is stored as a string.
The information conveyed concerning: 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
*/
int main()
{
char * list[25] = { NULL };
struct cellData STRlist[25] = { 0 };
int seed = 715;
int i, listsize;
listsize = generate_list(list, seed);
printf(" original list *************** ");
for (i = 0; i< listsize; i++)
printf("%s ", list[i]);
printf(" ");
sortListByBalance(list, listsize);
for (i = 0; i< listsize; i++)
printf("%s ", list[i]);
printf(" ");
printf("the average balance is %.2lf ", computeAverageListBalance(list, listsize));
quit(list, listsize);
listsize = generate_Structlist(STRlist, seed);
printf(" original list *************** ");
for (i = 0; i< listsize; i++)
printf("%24s %5s %2s %3s %7s %7.2lf ", STRlist[i].Name, STRlist[i].account, STRlist[i].state, STRlist[i].areaCode, STRlist[i].mobileNumber, STRlist[i].balance);
printf(" ");
//sort by cell data balance
sortByBalance(STRlist, listsize);
for (i = 0; i< listsize; i++)
printf("%24s %5s %2s %3s %7s %7.2lf ", STRlist[i].Name, STRlist[i].account, STRlist[i].state, STRlist[i].areaCode, STRlist[i].mobileNumber, STRlist[i].balance);
printf(" ");
printf("the average balance is %.2lf ", computeAverageBalance(STRlist, listsize));
}
double computeAverageBalance(struct cellData ARlist[], int listsize)
//This function will calculate the average of all user's balances.
{
double avg = 0;
//Declares variable for the average.
double total = 0;
//Declares variable for total balance of all users.
int i = 0;
//Declares variable i.
while (i < listsize)
//While i is less than list size.
{
total += ARlist[i].balance;
//Adds the balance of customer "i" to the total.
i++;
//Move to the next customer's data.
}
avg = total / listsize;
//Average is equal to sum of all user's balances divided by listsize.
return avg;
//Return the average.
}
void sortByBalance(struct cellData ARlist[], int listsize)
{
int i, j, a, n;
for (i = 0; i < listsize; ++i)
{
for (j = i + 1; j < listsize; ++j)
{
if (ARlist[i].balance < ARlist[j].balance)
{
a = ARlist[i].balance;
ARlist[i].balance = ARlist[j].balance;
ARlist[j].balance = a;
}
}
}
}
double computeAverageListBalance(char *list[], int listsize)
{
int i;
char * ptr;
double x, avg, sum = 0;
for (i = 0; i < listsize; i++)
{
ptr = list[i] + (strlen(list[i]) - 1);
while (*ptr != '^')
ptr--;
if (*ptr = '^')
ptr++;
x = atof(ptr);
sum += x;
}
avg = sum / (double)i;
return avg;
}
void sortListByBalance(char * list[], int listsize)
{
int pass, i;
char * ptr1, * ptr2, * temp;
double f1, f2;
for (pass = 1; pass < listsize; pass++)
{
for (i = 0; i < listsize - 1; i++)
{
ptr1 = list[i] + strlen(list[i]) - 1;
ptr2 = list[i + 1] + strlen(list[i + 1]) - 1;
while (*ptr1 != '^')
ptr1--;
if (*ptr1 = '^')
ptr1++;
while (*ptr2 != '^')
ptr2--;
if (*ptr2 = '^')
ptr2++;
f1 = atof(ptr1);
f2 = atof(ptr2);
if (f1 > f2)
{
temp = list[i + 1];
list[i + 1] = list[i];
list[i] = temp;
}
}
}
}
Output
original list
***************
Wednesday Adams^122^VA^154^3121131^832.45
Carl Lewis^245^IL^721^4121131^832.45
Christopher Columbus^1234^VA^154^3121131^6.80
Dan Crane^1331^VA^616^2031101^6.80
Annette Wendt^5678^IL^721^3121131^86.67
Anna Washington^122^NC^393^9781234^394
Rene Williamson^100^IN^345^2113344^100.13
Willie Daniels^344^IN^345^3121131^394
George Sims^900^NY^317^8121101^7.4
Buster Keaton^1090^NY^711^3121131^59.45
Yannick Noah^1059^VA^317^2345678^77.23
Sigourney Weaver^9001^MD^812^3121131^100.13
Edward Morris^8777^VA^186^9121131^86.67
Pat Lane^688^MD^186^3121131^832.45
Phil Donahue^4112^IL^277^6421131^394
Wendy Morse^8771^IL^214^2521131^77.23
Dawn Harris^3056^NY^214^1021131^6.80
Jose Richards^3344^IN^317^4321131^59.45
Jane Eyre^4021^NY^214^3121101^100.13
Christopher Columbus^1234^VA^154^3121131^6.80
Dan Crane^1331^VA^616^2031101^6.80
Dawn Harris^3056^NY^214^1021131^6.80
George Sims^900^NY^317^8121101^7.4
Buster Keaton^1090^NY^711^3121131^59.45
Jose Richards^3344^IN^317^4321131^59.45
Yannick Noah^1059^VA^317^2345678^77.23
Wendy Morse^8771^IL^214^2521131^77.23
Annette Wendt^5678^IL^721^3121131^86.67
Edward Morris^8777^VA^186^9121131^86.67
Rene Williamson^100^IN^345^2113344^100.13
Sigourney Weaver^9001^MD^812^3121131^100.13
Jane Eyre^4021^NY^214^3121101^100.13
Anna Washington^122^NC^393^9781234^394
Willie Daniels^344^IN^345^3121131^394
Phil Donahue^4112^IL^277^6421131^394
Wednesday Adams^122^VA^154^3121131^832.45
Carl Lewis^245^IL^721^4121131^832.45
Pat Lane^688^MD^186^3121131^832.45
the average balance is 234.43
original list
***************
Wednesday Adams 122 VA 154 3121131 832.45
Carl Lewis 245 IL 721 4121131 832.45
Christopher Columbus 1234 VA 154 3121131 6.80
Dan Crane 1331 VA 616 2031101 6.80
Annette Wendt 5678 IL 721 3121131 86.67
Anna Washington 122 NC 393 9781234 394.00
Rene Williamson 100 IN 345 2113344 100.13
Willie Daniels 344 IN 345 3121131 394.00
George Sims 900 NY 317 8121101 7.40
Buster Keaton 1090 NY 711 3121131 59.45
Yannick Noah 1059 VA 317 2345678 77.23
Sigourney Weaver 9001 MD 812 3121131 100.13
Edward Morris 8777 VA 186 9121131 86.67
Pat Lane 688 MD 186 3121131 832.45
Phil Donahue 4112 IL 277 6421131 394.00
Wendy Morse 8771 IL 214 2521131 77.23
Dawn Harris 3056 NY 214 1021131 6.80
Jose Richards 3344 IN 317 4321131 59.45
Jane Eyre 4021 NY 214 3121101 100.13
Wednesday Adams 122 VA 154 3121131 832.45
Carl Lewis 245 IL 721 4121131 832.45
Christopher Columbus 1234 VA 154 3121131 832.45
Dan Crane 1331 VA 616 2031101 394.00
Annette Wendt 5678 IL 721 3121131 394.00
Anna Washington 122 NC 393 9781234 394.00
Rene Williamson 100 IN 345 2113344 100.13
Willie Daniels 344 IN 345 3121131 100.00
George Sims 900 NY 317 8121101 100.00
Buster Keaton 1090 NY 711 3121131 86.00
Yannick Noah 1059 VA 317 2345678 86.00
Sigourney Weaver 9001 MD 812 3121131 77.00
Edward Morris 8777 VA 186 9121131 77.00
Pat Lane 688 MD 186 3121131 59.00
Phil Donahue 4112 IL 277 6421131 59.00
Wendy Morse 8771 IL 214 2521131 7.00
Dawn Harris 3056 NY 214 1021131 6.00
Jose Richards 3344 IN 317 4321131 6.00
Jane Eyre 4021 NY 214 3121101 6.00
the average balance is 234.13
--------------------------------
Process exited after 0.257 seconds with return value 31
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.