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 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...
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 creates a two-dimensional array initialized with test data. Use any primitive data...
Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: -getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array. -getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array. -getRowTotal. This method should accept a two-dimensional array as...
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
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT