I have a bit of a challenging programming problem involving structs and function
ID: 3547449 • Letter: I
Question
I have a bit of a challenging programming problem involving structs and function writing. The program should be able to accurately read in the input test cases that are provided.
Here is a link to the problem:
https://www.dropbox.com/s/xef0kyf9fvlmmjg/program_directions.docx
Here are links to some test cases for the program:
https://www.dropbox.com/s/p7bqesvn1ji7yys/stock_input.docx
https://www.dropbox.com/s/n05g95vg9ynwkj2/volatile_input.docx
https://www.dropbox.com/s/0tsgrwwpdhpk9yi/change_data.docx
https://www.dropbox.com/s/a177c32pqphwd1j/average_data.docx
If it is possible, please provide a link to a webpage where you were able to show your problem with indentations. Thank you so much!
Explanation / Answer
/************************** main.c *********************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 20
typedef struct stock {
int id;
char ticker[6];
float value[20];
float change, avg, vol;
} stock;
float delta(stock S)
{
return S.value[SIZE-1]-S.value[0];
}
float avg(stock S)
{
int i;
float sum = 0;
for(i=0; i<SIZE; i++) {
sum += S.value[i];
}
return sum / SIZE;
}
float vol(stock S)
{
int i;
float sum = 0;
for(i=0; i<SIZE-1; i++) {
sum += fabs(S.value[i+1] - S.value[i]);
}
return 100 * (sum / avg(S));
}
void writechange(int n, stock *stockp, FILE *out)
{
int i, j;
stock swap;
for (i = 0; i < n; i++) {
for (j = 0; j < n-1; j++) {
if(stockp[j].change < stockp[j+1].change) {
swap = stockp[j];
stockp[j] = stockp[j+1];
stockp[j+1] = swap;
}
}
}
for (i = 0; i < n; i++) {
fprintf(out, "%s %.2f ", stockp[i].ticker, stockp[i].change);
}
}
void writeaverage(int n, stock *stockp, FILE *out)
{
int i, j;
stock swap;
for (i = 0; i < n; i++) {
for (j = 0; j < n-1; j++) {
if(stockp[j].avg < stockp[j+1].avg) {
swap = stockp[j];
stockp[j] = stockp[j+1];
stockp[j+1] = swap;
}
}
}
for (i = 0; i < n; i++) {
fprintf(out, "%s %.2f ", stockp[i].ticker, stockp[i].avg);
}
}
void writevolatile(int n, stock *stockp, FILE *out)
{
int i, j;
stock swap;
for (i = 0; i < n; i++) {
for (j = 0; j < n-1; j++) {
if(stockp[j].vol > stockp[j+1].vol) {
swap = stockp[j];
stockp[j] = stockp[j+1];
stockp[j+1] = swap;
}
}
}
for (i = 0; i < n; i++) {
fprintf(out, "%s %.2f ", stockp[i].ticker, stockp[i].vol);
}
}
int main()
{
FILE *input, *output;
int i, j, n, choice;
char filename[40];
stock *stockp;
input = fopen("input.txt", "r");
if(!input) {
printf("Error: coud not load the file ");
return -1;
}
fscanf(input, "%d", &n);
stockp = (stock *) malloc (n * sizeof(stock));
for(i=0; i<n; i++) {
fscanf(input, "%s", stockp[i].ticker);
for(j=0; j<20; j++) {
fscanf(input, "%f", &stockp[i].value[j]);
}
stockp[i].change = delta(stockp[i]);
stockp[i].avg = avg(stockp[i]);
stockp[i].vol = vol(stockp[i]);
}
printf("The stock data has been loaded. ");
fclose(input);
printf("How would you like to sort the data? ");
printf("1) change (highest positive change first) ");
printf("2) average value (highest average first) ");
printf("3) volatility (lowest volatility first) ");
scanf("%d", &choice);
printf("What file would you like the output to be stored? ");
scanf("%s", filename);
output = fopen(filename, "w");
fprintf(output, "%d ", n);
switch(choice) {
case 1: writechange(n, stockp, output); break;
case 2: writeaverage(n, stockp, output); break;
case 3: writevolatile(n, stockp, output); break;
default: break;
}
fclose(output);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.