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

For this programming assignment, you will develop a program that will analyze th

ID: 3809749 • Letter: F

Question

For this programming assignment, you will develop a program that will analyze the monthly closing stock prices for five (5) companies in 2015. The stock price data for these companies will be provided in an input file named stocks.txt, which includes the company's name and the closing stock prices for each month. You will use a total of three(3) arrays: (1) a one- dimensional array to store the companies' names; (2) a (parallel) two-dimensional array to store the monthly closing stock prices; and (3) a (parallel) two-dimensional array to store the quarterly averages for closing stock prices. Your program must contain at least the following functions: a function to read and store data into two of the arrays a function to calculate and display the companies with the highest and lowest monthly averages a function to calculate, store, and display the quarter averages for all companies Any function that needs to "store" data should write to an output file (named .txt). Successful completion of this assignment includes (but is no limited to): Use of 1-D and 2-D arrays Properly named .cpp and .txt files Inclusion of the algorithm used to develop your program as comments within your program A program that compiles and executes properly with an input file A program that properly computes and displays the outputs listed above

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <algorithm>
using namespace std;

char **stockArray;
int monthlyAverage[5][12];
int quaterlyAverage[5][4];

void readFile()
{
// Reading file
string line;
ifstream myfile ("stocks.txt");
if (myfile.is_open())
{
int iCount = 0;
stockArray = new char*[5];
while ( getline (myfile,line) )
{
  
char *str=new char[line.size()+1];
str[line.size()]=0;
memcpy(str,line.c_str(),line.size());
  
char * pch;
pch = strtok (str," ,.-");
stockArray[iCount] = pch;
pch = strtok (NULL, " ,.-");
int j = 0;
int quaterIndex=0;
int temp=1;
int sum = 0;
  
while (pch != NULL)
{
monthlyAverage[iCount][j] = atoi(pch);
  
sum = sum+monthlyAverage[iCount][j];
  
if(temp == 3){
float average = sum/3;
quaterlyAverage[iCount][quaterIndex] = average;
sum=0;
quaterIndex++;
temp = 1;
}else{
temp++;
}
  
j++;
pch = strtok (NULL, " ,.-");
}
iCount++;
}
  
  
  
myfile.close();
}
  
else cout << "Unable to open file";
  
}

void highest_lowest_Monthlyavr(){
int minAverage=0;
int maxAverage=0;
int minStockIndex;
int maxStockIndex;
  
for(int i=0;i<5;i++){
  
if(minAverage == 0){
minAverage = *std::min_element(monthlyAverage[i],monthlyAverage[i]+12);
minStockIndex = i;
}
else{
int val = *std::min_element(monthlyAverage[i],monthlyAverage[i]+12);
if(val < minAverage){
minAverage = val;
minStockIndex = i;
}
}
  
}
  
for(int i=0;i<5;i++){

if(maxAverage == 0){
maxAverage = *std::max_element(monthlyAverage[i],monthlyAverage[i]+12);
maxStockIndex = i;
}
else{
int val = *std::max_element(monthlyAverage[i],monthlyAverage[i]+12);
if(val > maxAverage){
maxAverage = val;
maxStockIndex = i;
}
}
}
cout << "Lowest Monthly average is : " << minAverage << " for the stock : " << stockArray[minStockIndex] << endl;
cout << "Highest Monthly average is : " << maxAverage << " for the stock : " << stockArray[maxStockIndex] << endl;
  
}

int main()
{
readFile();

cout << "Stocks array is " <<endl;
for(int i=0;i<5;i++){
  
cout << stockArray[i] << " ";
  
}
cout << endl;

cout << "Monthly Average array is " <<endl;
for(int i=0;i<5;i++){
for(int j=0;j<12;j++){
cout << monthlyAverage[i][j] << " ";
}
cout << endl;
}

cout << "Quaterly Average array is " <<endl;
for(int i=0;i<5;i++){
for(int j=0;j<4;j++){
cout << quaterlyAverage[i][j] << " ";
}
cout << endl;
}

highest_lowest_Monthlyavr();

return 0;
}

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

stocks.txt

A 100 110 115 110 120 122 125 120 120 105 110 120
B 100 100 105 115 110 112 115 120 110 115 113 121
C 100 110 135 130 140 152 155 150 160 175 160 160
D 100 100 105 110 110 112 115 110 110 115 110 110
E 100 110 125 110 120 122 115 110 120 115 120 110

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

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