Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Requirements: These are the lines that need to be included somewhere in

ID: 3765644 • Letter: P

Question

Problem Requirements: These are the lines that need to be included somewhere in the program. The details of each will be provided in the sections following this one.

int date_binary_search( char *haystack[ ], int sizeof_haystack, char search_string );

----------------------------------------------------------------------------------------------------------------------------------------------------

const int NUMBER_OF_WORDS = sizeof word / sizeof word[0];

----------------------------------------------------------------------------------------------------------------------------------------------------char *fgets(char *s, int size, FILE *stream);

----------------------------------------------------------------------------------------------------------------------------------------------------

char linein[80];

fgets( linein, 80, stdin );

printf( "You entered %s. ", linein );

----------------------------------------------------------------------------------------------------------------------------------------------------

#define CLOSINGS_SIZE 100

double closings[ CLOSINGS_SIZE ];

----------------------------------------------------------------------------------------------------------------------------------------------------

int combine_arrays( double destination[], int destination_ends, int destination_size, double source[ ], int source_size );

----------------------------------------------------------------------------------------------------------------------------------------------------

int jan_size = sizeof january_2015_closing / sizeof january_2015_closing[0];

int feb_size = sizeof february_2015_closing / sizeof february_2015_closing[0];

int mar_size = sizeof march_2015_closing / sizeof march_2015_closing[0];

int last = 0;

last = combine_arrays( closings, last, CLOSINGS_SIZE, january_2015_closing, jan_size );

last = combine_arrays( closings, last, CLOSINGS_SIZE, february_2015_closing, feb_size );

last = combine_arrays( closings, last, CLOSINGS_SIZE, march_2015_closing, mar_size );

Problem Specification:

Parallel arrays are a common way to organize basic data. A header file has been created for you containing twelve arrays of data. There are four types

date_recorded - The date the data was recorded

opening_average - the Dow Jones average when the market opened in the morning

closing_average - the Dow Jones average when the marked closed in the afternoon

volume - the volume of transactions processed during the day

These four arrays appear with data for each month of January, February, and March 2015. View the contents of the header file for details.

Obtain the file as noted below under Helpful Hints from the public github.

We want to know:

• The date in January when the opening average, closing average, and volume was highest, and then lowest.

• The date in February when the opening average, closing average, and volume was highest, and then lowest.

• The date in March when the opening average, closing average, and volume was highest, and then lowest.

• The date in all data collected when the opening average, closing average, and volume was highest and lowest.

• The average and median and value for January, February, March, and all the data together as a whole.

Create appropriate functions to calculate each of these amounts, and have a single function that performs the display of all the above information. Only one function should handle all the display information, so it will be necessary to pass the calculations in to the display function for it to do the job.

Task:

Write a program to display summary information about the data collected to date in 2015. You may choose to display it either as a table or in sentence form. Either one will be acceptable, but the output should be displayed as nicely as possible.

Once the summary data is displayed to the user, prompt them to enter a date, and have your program display the information for that date, such as

Enter a date to display or type "end" to exit: March 6

March 6, 2015

Open Close Volume Month to Date Year to Date

18,135.72 17,856.78 113,350,000 463,710,000 4,388,220,000

If the user enters an invalid date, let them know

Enter a date to display or type "end" to exit: March 8

March 8, 2015

The market was closed on March 8, 2015.

To display this data, create new parallel arrays that contain all the data from the arrays in the header. That is, you would have

#define CLOSINGS_SIZE 100

double closings[ CLOSINGS_SIZE ];

which would contain all the closings from January, February, and March. To do this, create a function such as

int combine_arrays( double destination[], int destination_ends, int destination_size, double source[ ], int source_size );

which would copy all the elements from the source array into the destination array. Then you could copy as follows:

int jan_size = sizeof january_2015_closing / sizeof january_2015_closing[0];

int feb_size = sizeof february_2015_closing / sizeof february_2015_closing[0];

int mar_size = sizeof march_2015_closing / sizeof march_2015_closing[0];

int last = 0;

last = combine_arrays( closings, last, CLOSINGS_SIZE, january_2015_closing, jan_size );

last = combine_arrays( closings, last, CLOSINGS_SIZE, february_2015_closing, feb_size );

last = combine_arrays( closings, last, CLOSINGS_SIZE, march_2015_closing, mar_size );

The above sequence should result in a copy of all the elements from the January, February, and March closing arrays in the destination array. Do the same for opening and volume.

Use a binary search, as described in the book on page 450, to find the matching date in the combined array. Once you find the date in the array, that same index is how you access the volume and other data in the other arrays. You will need to calculate a new array for the year-to-date value, which you should store in it's own array (and not calculate each time though).

The binary search function should have a prototype such as

int date_binary_search( char *haystack[ ], int sizeof_haystack, char search_string );

You'll have to put a little logic into setting yp the binary search, since it's not just simple numbers that you're searching on. But it's easy enough to know that if the search_string date is 'March 6', when you go to the middle of the haystack you might be around Feb 9, so you know the string you're searching for is closer to the end of the haystack, so go towards the bottom.

Often, when working on a function like this, it can be useful to draw out the haystack and hand-trace how the binary search is doing the search. I'll leave that to you.

Helpful Hints:

DYNAMICALLY (AT RUN TIME) CALCULATE WORDS IN THE HEADER

Use the following line to create a global constant representing the number of words in the word[ ] array declared in the header. By counting them this way, you can easily have the header changed at any time (adding words, deleting words, etc.) and still know how many there are, and what the random number has to be between to select a valid word.

const int NUMBER_OF_WORDS = sizeof word / sizeof word[0];

PROMPTING AND READING IN DATA

When you prompt the user and accept their input, you’ll need to read it in as a string, but not using scanf( ). Use the standard function fgets, which has the following prototype, instead:

char *fgets(char *s, int size, FILE *stream);

To read in an entire line from the keyboard, that is, until the user presses return, try

char linein[80];

fgets( linein, 80, stdin );

printf( "You entered %s. ", linein );

Then you can use an IF statement to see if they entered 'end' to exit the program or if they entered a date you need to binary search for.

No NO NOOOOOO!

******* Do not use multidimensional arrays or structures in this lab assignment. ********

Source File Required for this program: You can also get this from github

#ifndef MARKET_H

#define MARKET_H

/* March 2015 market numbers */

/* Source: http://finance.yahoo.com/q/hp?s=^DJI+Historical+Prices */

double march_2015_opening_average[] = {

17989.56,

17856.56,

18135.72,

18096.90,

18203.37,

18281.95,

18134.05,

};

char *march_2015_date_recorded[] = {

"Mar 10, 2015",

"Mar 9, 2015",

"Mar 6, 2015",

"Mar 5, 2015",

"Mar 4, 2015",

"Mar 3, 2015",

"Mar 2, 2015",

};

double march_2015_volume[] = {

120450000,

85820000,

113350000,

75840000,

80900000,

83830000,

89790000,

};

double march_2015_closing[] = {

17662.94,

17995.72,

17856.78,

18135.72,

18096.90,

18203.37,

18288.63,

};

/* February 2015 market numbers */

double february_2015_opening_average[] = {

18213.26,

18224.41,

18208.67,

18112.57,

18140.76,

17985.77,

18028.67,

18045.72,

18019.80,

17968.65,

17862.14,

17867.86,

17736.15,

17821.49,

17881.54,

17677.26,

17664.99,

17369.97,

17169.99,

};

char *february_2015_date_recorded[] = {

"Feb 27, 2015",

"Feb 26, 2015",

"Feb 25, 2015",

"Feb 24, 2015",

"Feb 23, 2015",

"Feb 20, 2015" ,

"Feb 19, 2015" ,

"Feb 18, 2015" ,

"Feb 17, 2015" ,

"Feb 13, 2015" ,

"Feb 12, 2015" ,

"Feb 11, 2015" ,

"Feb 10, 2015" ,

"Feb 9, 2015" ,

"Feb 6, 2015" ,

"Feb 5, 2015" ,

"Feb 4, 2015" ,

"Feb 3, 2015" ,

"Feb 2, 2015" ,

};

double february_2015_volume[] = {

101110000,

81500000,

80480000,

79310000,

83670000,

111390000,

79130000,

75090000,

98760000,

85230000,

117160000,

89890000,

89930000,

81590000,

93610000,

79890000,

102560000,

112860000,

108090000,

};

double february_2015_closing[] = {

18132.70,

18214.42,

18224.57,

18209.19,

18116.84,

18140.44,

17985.77,

18029.85,

18047.58,

18019.35,

17972.38,

17862.14,

17868.76,

17729.21,

17824.29,

17884.88,

17673.02,

17666.40,

17361.04,

};

/* January 2015 market numbers */

double january_2015_opening_average[] = {

17416.85,

17195.29,

17402.91,

17638.53,

17668.11,

17812.50,

17557.29,

17509.96,

17516.96,

17320.00,

17436.30,

17609.06,

17645.02,

17742.05,

17911.02,

17591.97,

17374.78,

17504.18,

17821.30,

17823.07,

};

char *january_2015_date_recorded[] = {

"Jan 30, 2015" ,

"Jan 29, 2015" ,

"Jan 28, 2015" ,

"Jan 27, 2015" ,

"Jan 26, 2015" ,

"Jan 23, 2015" ,

"Jan 22, 2015" ,

"Jan 21, 2015" ,

"Jan 20, 2015" ,

"Jan 16, 2015" ,

"Jan 15, 2015" ,

"Jan 14, 2015" ,

"Jan 13, 2015" ,

"Jan 12, 2015" ,

"Jan 9, 2015" ,

"Jan 8, 2015" ,

"Jan 7, 2015" ,

"Jan 6, 2015" ,

"Jan 5, 2015" ,

"Jan 2, 2015" ,

};

double january_2015_volume[] = {

168560000,

111690000,

115980000,

135940000,

87220000,

97110000,

111980000,

95530000,

119600000,

140480000,

94520000,

109180000,

99360000,

92500000,

93390000,

114890000,

91030000,

101870000,

116160000,

76270000,

};

double january_2015_closing[] = {

17164.95,

17416.85,

17191.37,

17387.21,

17678.70,

17672.60,

17813.98,

17554.28,

17515.23,

17511.57,

17320.71,

17427.09,

17613.68,

17640.84,

17737.37,

17907.87,

17584.52,

17371.64,

17501.65,

17832.99,

};

#endif

Explanation / Answer

It is pretty big question, I would say.

I tried to cover as much as possible. Hope you will appreciate it.

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

#define CLOSINGS_SIZE 100

int combine_arrays( double destination[], int destination_ends, int destination_size, double source[], int source_size );
int date_binary_search( char *haystack[ ], int sizeof_haystack, char *search_string );

int main()
{
double closings[CLOSINGS_SIZE];

int jan_size = sizeof january_2015_closing / sizeof january_2015_closing[0];
int feb_size = sizeof february_2015_closing / sizeof february_2015_closing[0];
int mar_size = sizeof march_2015_closing / sizeof march_2015_closing[0];

int last = 0;
last = combine_arrays( closings, last, CLOSINGS_SIZE, january_2015_closing, jan_size );
last = combine_arrays( closings, last, CLOSINGS_SIZE, february_2015_closing, feb_size );
last = combine_arrays( closings, last, CLOSINGS_SIZE, march_2015_closing, mar_size );

char linein[80];
fgets( linein, 80, stdin );
printf( "You entered %s. ", linein );

while (strncmp(linein,"end",strlen("end")) != 0)
{
int index;
index = date_binary_search(closings, last, linein);
printf("%s", closings[index]);
}

printf("Hello world! ");
return 0;
}

int combine_arrays(double destination[], int destination_ends, int destination_size, double source[], int source_size)
{
int i = destination_ends;
for (i = destination_ends; i < destination_ends+source_size; i++)
{
destination[i] = source[i-destination_ends];
}

return i;
}

int date_binary_search( char *haystack[], int sizeof_haystack, char *search_string )
{
return binarySearchImplementation(haystack, 0 , sizeof_haystack-1, *search_string);
}

int binarySearchImplementation(char *arr[], int l, int r, char *search_string)
{
if (r >= l)
{
int mid = l + (r - l)/2;

// If the element is present at the middle itself
if (strcmp(arr[mid],search_string) == 0) return mid;

// If element is smaller than mid, then it can only be present
// in left subarray
if (strcmp(arr[mid],search_string) > 0) return binarySearch(arr, l, mid-1, search_string);

// Else the element can only be present in right subarray
return binarySearch(arr, mid+1, r, search_string);
}

// We reach here when element is not present in array
return -1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote