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

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

ID: 1296422 • Letter: W

Question

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

Explanation / Answer

#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 ", s[j].stock_name);
       fprintf(f, "Purchase Date: %s ", s[j].stock_symbol);
       fprintf(f, "Intial Cost: $%0.2f ", s[j].price);
       fprintf(f, "Current Cost: $%0.2f ", s[j].dividend);
       fprintf(f, "Profit: $%0.2f ", 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 ", 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(" Purchase Date: %s ", s[j].purchase_date);
       printf("Initial Cost: $%0.2f ", s[j].initial_cost);
       printf("Current Cost: $%0.2f ", s[j].current_cost);
       printf("Profit: $%0.2f ", s[j].profit);
   }
}

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

}

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