53.2k views
3 votes
Create an array of doubles to contain 5 values Prompt the user to enter 5 values that will be stored in the array. These values represent hours worked. Each element represents one day of work. Create 3 c-strings: full[40], first[20], last[20] Create a function parse_name(char full[], char first[], char last[]). This function will take a full name such as "Noah Zark" and separate "Noah" into the first c-string, and "Zark" into the last c-string. Create a function void create_timecard(double hours[], char first[], char last[]). This function will create (write to) a file called "timecard.txt". The file will contain the parsed first and last name, the hours, and a total of the hours. See the final file below. First Name: Noah

1 Answer

5 votes

Solution :


$\#$include
$<stdio.h>$


$\#$include
$<string.h>$

void parse
$\_$name(
$char \ full[]$, char first
$[],$ char last
$[])$

{

// to_get_this_function_working_immediately, _irst

// stub_it_out_as_below._When_you_have_everything_else

// working,_come_back_and_really_parse_full_name

// this always sets first name to "Noah"

int i, j = 0;

for(i = 0; full[i] != ' '; i++)

first[j++] = full[i];

first[j] = '\0';

// this always sets last name to "Zark"

j = 0;

strcpy(last, full+i+1);

// replace the above calls with the actual logic to separate

// full into two distinct pieces

}

int main()

{

char full[40], first[20], last[20];

double hours[5];

// ask the user to enter full name

printf("What is your name? ");

gets(full);

// parse the full name into first and last

parse_name(full, first, last);

// load the hours

for(int i = 0; i < 5; i++)

{

printf("Enter hours for day %d: ", i+1);

scanf("%lf", &hours[i]);

}

// create the time card

FILE* fp = fopen("timecard.txt", "w");

fprintf(fp, "First Name: %s\\", first);

fprintf(fp, "Last Name: %s\\", last);

double sum = 0.0;

for(int i = 0; i < 5; i++)

{

fprintf(fp, "Day %i: %.1lf\\", i+1, hours[i]);

sum += hours[i];

}

fprintf(fp, "Total: %.1f\\", sum);

printf("Timecard is ready. See timecard.txt\\");

return 0;

}

User Jeremy Bourque
by
3.1k points