Question

In: Computer Science

You will update the program as follows: [8 marks] Define the TimeType and DateType data types....

You will update the program as follows:
[8 marks] Define the TimeType and DateType data types. The content of these data types can be deduced from a thorough reading of the base code.
[6 marks] Complete the implementation of the initDate() function.
[16 marks] Write the complete implementation of the selectTimes() function. This function populates the newArr parameter with all the dates in arr that have a time earlier than the given hours and minutes. For example, if the cut-off time specified is 14 hours and 30 minutes, newArr will contain all the dates with a time before 2:30 pm.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_STR_1 16
#define MAX_STR_2 32
#define MAX_ARR 64
#define C_OK 0
#define C_NOK -1

typedef struct {
/* TimeType not shown */
} TimeType;

typedef struct {
/* DateType not shown */
} DateType;

typedef struct {
int size;
DateType* elements[MAX_ARR];
} ArrayType;

void initArray(ArrayType*);
void addDate(ArrayType*, DateType*);
void selectTimes(ArrayType*, ArrayType*, int, int);
void initDate(char*, int, int, int, int, int, DateType*);
void printArray(ArrayType*);
void printDate(DateType*);

int main()
{
ArrayType calendar, specialCal;
DateType* newDate;
int num1, num2, num3, num4, num5;
char str[MAX_STR_1];

initArray(&calendar);
initArray(&specialCal);

while(1) {
printf("Enter month, day, year, hours, minutes, and seconds: ");
scanf("%s %d %d %d %d %d", str, &num1, &num2, &num3, &num4, &num5);
if (strcmp(str, "-1") == 0)
break;
newDate = malloc(sizeof(DateType));
initDate(str, num1, num2, num3, num4, num5, newDate);
addDate(&calendar, newDate);
}

printf("Enter cut-off time (hours and minutes): ");
scanf("%d %d", &num1, &num2);
selectTimes(&calendar, &specialCal, num1, num2);

printf(" FULL CALENDAR:");
printArray(&calendar);

printf(" MORNING CALENDAR:");
printArray(&specialCal);

return(0);
}

/*
Purpose: initialize an array structure
*/
void initArray(ArrayType *a) { a->size = 0; }

/*
Purpose: populate newArr with all dates from orgArr that have
a time before the given hours and minutes, for
example, if we want a calendar with all the morning
appointments, we would call this function with
h and m parameters set to 12 and 0, respectively
*/
void selectTimes(ArrayType *orgArr, ArrayType *newArr, int h, int m)
{
/* Implementation not shown */
}

/*
Purpose: add a date structure to the back of the array
*/
void addDate(ArrayType *arr, DateType *newDate)
{
if (arr->size >= MAX_ARR)
return;

arr->elements[arr->size] = newDate;
++(arr->size);
}

/*
Purpose: initialize a date structure with the given parameters
*/
void initDate(char *mm, int d, int y, int h, int m, int s, DateType *newDate)
{
/* Implementation not shown */
}

void printDate(DateType *d)
{
char str[MAX_STR_2];

sprintf(str, "%s %d, %d", d->month, d->day, d->year);

printf("%25s @ %2d:%2d:%2d ", str,
d->time.hours, d->time.minutes, d->time.seconds);

}

void printArray(ArrayType *arr)
{
int i;

printf(" ");

for (i=0; i<arr->size; ++i)
printDate(arr->elements[i]);
}

Solutions

Expert Solution

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_STR_1 16
#define MAX_STR_2 32
#define MAX_ARR 64
#define C_OK 0
#define C_NOK -1

typedef struct {
   int hours;
   int minutes;
   int seconds;
} TimeType;

typedef struct {
   TimeType time;
   char month[MAX_STR_1];
   int day;
   int year;
} DateType;

typedef struct {
int size;
DateType* elements[MAX_ARR];
} ArrayType;

void initArray(ArrayType*);
void addDate(ArrayType*, DateType*);
void selectTimes(ArrayType*, ArrayType*, int, int);
void initDate(char*, int, int, int, int, int, DateType*);
void printArray(ArrayType*);
void printDate(DateType*);

int main()
{
ArrayType calendar, specialCal;
DateType* newDate;
int num1, num2, num3, num4, num5;
char str[MAX_STR_1];

initArray(&calendar);
initArray(&specialCal);

while(1) {
printf("Enter month, day, year, hours, minutes, and seconds: ");
scanf("%s %d %d %d %d %d", str, &num1, &num2, &num3, &num4, &num5);
if (strcmp(str, "-1") == 0)
break;
newDate = malloc(sizeof(DateType));
initDate(str, num1, num2, num3, num4, num5, newDate);
addDate(&calendar, newDate);
}

printf("Enter cut-off time (hours and minutes): ");
scanf("%d %d", &num1, &num2);
selectTimes(&calendar, &specialCal, num1, num2);

printf(" FULL CALENDAR:");
printArray(&calendar);

printf(" MORNING CALENDAR:");
printArray(&specialCal);

return(0);
}

/*
Purpose: initialize an array structure
*/
void initArray(ArrayType *a) { a->size = 0; }

/*
Purpose: populate newArr with all dates from orgArr that have
a time before the given hours and minutes, for
example, if we want a calendar with all the morning
appointments, we would call this function with
h and m parameters set to 12 and 0, respectively
*/
void selectTimes(ArrayType *orgArr, ArrayType *newArr, int h, int m)
{
   int i;
   newArr->size = 0;
    for(i=0;i<orgArr->size;i++){
        if(orgArr->elements[i]->time.hours < h || ((orgArr->elements[i]->time.hours == h) && (orgArr->elements[i]->time.minutes < m))){
            newArr->elements[newArr->size ++] = orgArr->elements[i];
       }
   }
}

/*
Purpose: add a date structure to the back of the array
*/
void addDate(ArrayType *arr, DateType *newDate)
{
if (arr->size >= MAX_ARR)
return;

arr->elements[arr->size] = newDate;
++(arr->size);
}

/*
Purpose: initialize a date structure with the given parameters
*/
void initDate(char *mm, int d, int y, int h, int m, int s, DateType *newDate)
{
   strcpy(newDate->month , mm);
   newDate->day = d;
   newDate->year = y;
   newDate->time.hours = h;
   newDate->time.minutes = m;
   newDate->time.seconds = s;
}

void printDate(DateType *d)
{
char str[MAX_STR_2];

sprintf(str, "%s %d, %d", d->month, d->day, d->year);

printf("%25s @ %2d:%2d:%2d ", str,
d->time.hours, d->time.minutes, d->time.seconds);

}

void printArray(ArrayType *arr)
{
int i;

printf(" ");

for (i=0; i<arr->size; ++i)
printDate(arr->elements[i]);
}


Related Solutions

4 Q5 Total Marks 8 Marks 8 Marks 8 Marks 8 Marks 8 Marks 40 Marks...
4 Q5 Total Marks 8 Marks 8 Marks 8 Marks 8 Marks 8 Marks 40 Marks               Q1: A cantilever beam having span ‘L’ m was subjected to a uniformly distributed load of magnitude 8 kN/m for a distance of ‘0.6 L’ from the free end and two concentrated loads one of magnitude 15 kN at a distance ‘0.25 L’ m from the free end while the other of magnitude ‘22’ kN at a distance ‘0.6 L’ m from the...
I need to update this program to follow the these requirements please and thank you :...
I need to update this program to follow the these requirements please and thank you : Do not use packages Every class but the main routine class must have a toString method . The toString method for Quadrilateral should display its fields: 4 Points. All instance variables should be declared explicitly private . Area incorrect : enter points no: 1 1 3 enter points no: 2 6 3 enter points no: 3 0 0 enter points no: 4 5 0...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more members. *Application must have at least one user-defined function *Declare an array of your struct using a size of 10 or more *Load the date for each element in your array from a text file *Display the data in your array in the terminal *provide brief comments for each line of code
Define a class Fraction3YourName as follows, /** * Program Name: Fraction3YourName.java * Discussion: Fraction3Yourname class *...
Define a class Fraction3YourName as follows, /** * Program Name: Fraction3YourName.java * Discussion: Fraction3Yourname class * written By: * Date: 2019/09/19 */ public class Fraction3YourName { private int sign; private int num; private int denom; public Fraction3YourName() { //sign = ; //denom = ; } public Fraction3YourName(int n) { //sign = ; //num = n; //denom = ; } public Fraction3YourName(int s, int n, int d) { //sign = s; //num = n; //denom = d; } } You are...
(8 marks) Write a program to ask user to enter an integer that represents the number...
Write a program to ask user to enter an integer that represents the number of elements, then generate an ArrayList containing elements which are all random integers in range [75, 144] , and finally display index and value of each element. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use ArrayList. Your program must use only printf(…) statements to adjust the alignment of your output. Your code must display the index in descending...
5. Briefly define the following option strategies (2 marks each =8 marks) 5.1. Protective put 5.2....
5. Briefly define the following option strategies (2 marks each =8 marks) 5.1. Protective put 5.2. Covered call 5.3. Straddle 5.4. Spread 6. The manager of a large portfolio includes $100 million worth of long-term bonds paying an average coupon rate of 7%. The manager believes that interest rates are about to rise. Explain n how he can address his concerns using interest rates swaps. In your answer, include a clear definition of an interest rate swap (5)
Task 8 - Hamming & SECDED Code (5+20= 25 marks) (a) (5 marks) For data, using...
Task 8 - Hamming & SECDED Code (5+20= 25 marks) (a) For data, using 3 Hamming code parity bits determine the maximum number of data bits that can be protected. (b) A SECDED encoded character has been retrieved, with the hexadecimal value of 409 to power of 16. You may assume that the SECDED parity is even. 1. (1 + 4 marks) Was there an error in transmission? Explain your answer. 2. If there was an error, either correct it...
C PROGRAM ..... NOT C++ PROGRAM 3 (UPDATE TO TWO PRIOR PROGRAMS FOR REFERENCE OF PRIOR...
C PROGRAM ..... NOT C++ PROGRAM 3 (UPDATE TO TWO PRIOR PROGRAMS FOR REFERENCE OF PRIOR PROGRAM INSTRUCTIONS PLEASE SEE BELOW) PROGRAM 3 Adjust program II to make use of functions. All the same rules from the previous program specifications still apply, for example input gathering, output formatting and breaking on -1 still apply. Additional requirements. Write a function that prompts the user for hours worked, rate and name. Use parameter passing, and pass by reference. Write a function that...
- List the benefits of deploying Windows Server Update Services in the network - Define the...
- List the benefits of deploying Windows Server Update Services in the network - Define the new features In the latest version of Windows Server Update Services
22. When you attempt to update a Web page and data is received by your system,...
22. When you attempt to update a Web page and data is received by your system, how can this be determined by an email the is received by a software such as Outlook? A. The system used the trailer of the packet with the number assignment. B. A combination of the IP address and the computer name is used. C. The service of DNS is used to apportion the correct information. D. Port numbers are used 23. Which of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT