Question

In: Physics

Write a program using multiple functions. Make use of an array to store data Make use...

Write a program using multiple functions. Make use of an array to store data Make use of searching techniques Read data from a file Write data to a file Instructions: In this lab, you will be examining a set of stock collected over a twenty four day period. Be sure to make use of an array to store these stocks. You will be required to read in the data points from a file. Write a function to read in the data. (See data details below.) After reading in the data, you will need to write a function that will calculate the average of the data set. You need to define a struct to contain stock investments, gain, etc. Then you should write a function to display the stocks in the table format shown below. Table Format: Hour stock Difference from Avg 1 35 -02.29 2 32 -05.29

Solutions

Expert Solution

#include<stdio.h>
#include<string.h>
#define SIZE 1000

struct Stock
{
   char stock_name[9],stock_symbol[9];
   float price, dividend,projected_increase,investment_gain;
};


void sortStock(Stock s[], int n)
{
   int i, j;
   char t[9];

   for(i=0; i<n-1; i++)
       for(j=0; j<n-1; j++)
           if (strcmp(s[j].stock_name, s[j+1].stock_name)>0)
           {
               strcpy(t, s[j].stock_name);
               strcpy(s[j].stock_name, s[j+1].stock_name);
               strcpy(s[j+1].stock_name, t);
           }
}

void saveText(Stock s[], int n)
{
   int j;
   FILE *f;
   f=fopen("c:\\stock_data.txt","w");
  
   for(j=0; j<n; j++)
   {
       fprintf(f, "%s\n", s[j].stock_name);
       fprintf(f, "Purchase Date: %s\n", s[j].stock_symbol);
       fprintf(f, "Intial Cost: $%0.2f\n", s[j].price);
       fprintf(f, "Current Cost: $%0.2f\n", s[j].dividend);
       fprintf(f, "Profit: $%0.2f\n\n", s[j].projected_increase);
   }
   fclose(f);
}

void retText(struct Stock s[], int n)
{
   FILE *f;
   int j;
  
   f=fopen("c:\\stock_data.txt","r");
   for(j=0; j< n; j++)
   {
       fgets(s[j].stock_name, sizeof(s[j].stock_name), f);
       fgets(s[j].stock_symbol, sizeof(s[j].stock_symbol),f);
       fscanf(f, "%f %f %f\n", s[j].price, s[j].dividend,s[j].projected_increase);      
s[j].investment_gain = s[j].dividend+ s[j].price * s[j].projected_increase/100;
   }

   fclose(f);
}
void print(Stock s[], int n)
{
   for(int j=0; j<n; j++)
   {
       printf("Stock Name: %s", s[j].stock_name);
       printf("\nPurchase Date: %s\n", s[j].purchase_date);
       printf("Initial Cost: $%0.2f\n", s[j].initial_cost);
       printf("Current Cost: $%0.2f\n", s[j].current_cost);
       printf("Profit: $%0.2f\n\n", s[j].profit);
   }
}

int main()
{
   Stock s[SIZE];  
   retText(s, SIZE);
   sortStock(s, SIZE);
   print(s, SIZE);
   saveText(s, SIZE);
system("pause");
return 0;

}


Related Solutions

Using JAVA Write a program that uses a 2-D array to store the highest and lowest...
Using JAVA Write a program that uses a 2-D array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods with their appropriate parameters: a.) getData: This method reads and stores the data in the 2-D array. b.) averageHigh: This method calculates and returns the average high temperature of the year. c.)...
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
Write a program that uses a structure to store the following data: (Remember to use proper...
Write a program that uses a structure to store the following data: (Remember to use proper formatting and code documentation) Member Name Description name student name idnum Student ID number Scores [NUM_TESTS] an array of test scores Average Average test score Grade Course grade Declare a global const directly above the struct declaration const int NUM_TESTS = 4; //a global constant The program should ask the user how many students there are and should then dynamically allocate an array of...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Write a C++ program that uses array to store the salaries of 10 employees working in...
Write a C++ program that uses array to store the salaries of 10 employees working in a small firm. The program should take average of the salaries and the max and min salaries being paid to the employees
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal fractions given the number of threads and perform Parallel MergeSort pThreads. Your algorithm should work for 2 threads. ParallelMergeSort.c #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #define SIZE 100 int array[SIZE]; void fillArrayWithRandomNumbers(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) array[i] = rand()%100; } void printArray(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) printf("%5d", arr[i]); printf("\n"); } typedef struct...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal fractions given the number of threads and perform Parallel MergeSort pThreads. Your algorithm should work for 2 threads. ParallelMergeSort.c #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #define SIZE 100 int array[SIZE]; void fillArrayWithRandomNumbers(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) array[i] = rand()%100; } void printArray(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) printf("%5d", arr[i]); printf("\n"); } typedef struct...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT