Question 1 Write a function called \"toArrayOfPointers\" that takes as arguments
ID: 3540602 • Letter: Q
Question
Question 1
Write a function called "toArrayOfPointers" that takes as arguments an array of integers and an integer representing the size of the array. The function should return an array of pointers to integers. The integers that the pointers in the returned array point to should be numerically equal to its counterpart in the input array, but not be stored in the same memory location. You can assume that the size of the array wil lbe larger than 0.
Question 2
Write a function called append that takes in two c-strings as intput and returns a new c-string that is obtained by appending the second input to the first input. You cannot use any standard library functions.
Question 3
a) Define a structure called TimeOfDay that has three members of type integer called hour, minute, and second.
b) Define a structure called Restaurant that has three members: A member of type (C++) string called name, and two members of type TimeOfDay called openingTime and closingTime.
c) Write a function called compareTimeOfDay that takes as input two pointers to TimeOfDay, and returns -1if the first is earlier than second, 0 if they are represent the same time, and 1 if the first is later than the secon. You can assume the inputs are not NULL.
d) Write a function with the following prototype :
Restaurant * openDuring(TimeOfDay * time, Restaurant * restList [], int size)
The function returns a pointer to the first restaurant in restList that is open at time. If no restaurant is open at time, NULL is returned. You can assume that none of the pointers in the input are NULL and that the closing times of every restaurant is after their opening times.
Explanation / Answer
Question 1
Write a function called "toArrayOfPointers" that takes as arguments an array of integers and an integer representing the size of the array.
The function should return an array of pointers to integers.
The integers that the pointers in the returned array point to should be numerically equal to its counterpart in the input array,
but not be stored in the same memory location. You can assume that the size of the array wil lbe larger than 0.
int** toArrayOfPointers(int array[],int size)
{
int** new_array = (int**) calloc(size, sizeof(int*));
for(int i=0; i<size; i++)
{
*(new_array+i) = (int*) malloc(sizeof(int));
*(*(new_array+i)) = array[i];
}
return new_array;
}
Question 2
Write a function called append that takes in two c-strings as intput and returns a new c-string that is obtained by appending the second input to
the first input.
char *append_1(char* str1,char* str2)
{
char* new_string= new char[strlen(str1)+strlen(str2)+1];
int i=0;
while(str1[i]!=0)
{
new_string[i] = str1[i];
i++;
}
int j=0;
while(str2[j]!=0)
{
new_string[i] = str2[j];
j++;
i++;
}
new_string[i] = '';
return new_string;
}
Question 3
a) Define a structure called TimeOfDay that has three members of type integer called hour, minute, and second.
structure TimeOfDay
{
int hour;
int minute;
int second;
};
b) Define a structure called Restaurant that has three members: A member of type (C++) string called name, and two members of type TimeOfDay
called openingTime and closingTime.
structure Restaurant
{
string name;
TimeOfDay openingTime;
TimeOfDay closingTime;
};
c) Write a function called compareTimeOfDay that takes as input two pointers to TimeOfDay, and returns -1if the first is earlier than second,
0 if they are represent the same time, and 1 if the first is later than the secon. You can assume the inputs are not NULL.
int compareTimeOfDay(TimeOfDay T1, TimeOfDay T2)
{
if(T1.hour < T2.hour)
{
return 1;
}
else if(T1.hour == T2.hour)
{
if(T1.minute < T2.minute)
{
return 1;
}
else if(T1.minute ==T2.minute)
{
if(T1.second < T2.second) return 1;
else if(T1.second ==T2.second) return 0;
}
}
return -1;
}
d) Write a function with the following prototype :
Restaurant * openDuring(TimeOfDay * time, Restaurant * restList [], int size)
The function returns a pointer to the first restaurant in restList that is open at time. If no restaurant is open at time, NULL is returned.
You can assume that none of the pointers in the input are NULL and that the closing times of every restaurant is after their opening times.
Restaurant * openDuring(TimeOfDay * time, Restaurant * restList [], int size)
{
for(int i=0; i<size; i++)
{
if(compareTimeOfDay(time,restList[i]->openingTime)==0)
return restList[i];
}
return NULL;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.