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

I have some program directions here, even though the program has been solved. Ca

ID: 3547723 • Letter: I

Question

I have some program directions here, even though the program has been solved. Can somebody please put comments throughout the program to specify what is happening at each step? That is all I need done. Thank you so much!


Here is the solved program:

https://www.dropbox.com/s/tqrzcxwj6wh2z3t/code_stock.docx


Here is a link to the problem directions (just to clarify what the program is doing):

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


Explanation / Answer

#include <stdio.h>      //including the various header files required in the program
#include <stdlib.h>
#include <math.h>
#define SIZE 20     //defining the Size to be 20. Thus, any change here can be reflected for the whole code for SIZE.

typedef struct stock    //defining the structure stock
{
    int id;                 //with the various datatypes used.
    char ticker[6];
    float value[20];
    float change, avg, vol;
} stock;

float delta(stock S)        // delta = value in array - value at array[0].
{
    return S.value[SIZE-1]-S.value[0];
}

float avg(stock S)      //finding average by callin gavg function
{
    int i;
    float sum = 0;      //initializing sum to be 0.
    for(i=0; i<SIZE; i++) {
        sum += S.value[i];      //adding all values to sum.
    }
    return sum / SIZE;      //now dividing sum by total number of values to get avg.{ avg = total sum/total values}
}

float vol(stock S)      // finding the volatitlity using the formula defined in the question
{
    int i;
    float sum = 0;      //initially sum is 0.
    for(i=0; i<SIZE-1; i++) {
        sum += fabs(S.value[i+1] - S.value[i]);     // then we add the absolute value of difference, i.e |s[i+1]-s[i]|
    }
    return 100 * (sum / avg(S));        //finally multiplying by 100 and dividing by avg. (following the formula given in the question.
}

void writechange(int n, stock *stockp, FILE *out)       //function to write change
{
    int i, j;
    stock swap;                                         //basically this first sorts the array in ascending order according to change.
    for (i = 0; i < n; i++) {
        for (j = 0; j < n-1; j++) {
            if(stockp[j].change < stockp[j+1].change) {         //by using bubble sort.
                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);      //now printing the change
    }
}

void writeaverage(int n, stock *stockp, FILE *out)      //function to write average
{
    int i, j;
    stock swap;
    for (i = 0; i < n; i++) {               //this too sorts by bubble sort, but according to the average.
        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);     //printing thr averages
    }
}

void writevolatile(int n, stock *stockp, FILE *out)// function to write volatile
{
    int i, j;
    stock swap;         //again sort through bubble sort but according to volatile values.
    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);     // printing the volatile values.
    }
}

int main()
{
    FILE *input, *output;           //defining the input and output pointers
    int i, j, n, choice;        //variables declarations
    char filename[40];
    stock *stockp;
    input = fopen("input.txt", "r");

    if(!input) {        //error to produce if input file not found.
        printf("Error: coud not load the file ");
    return -1;
    }

    fscanf(input, "%d", &n);
    stockp = (stock *) malloc (n * sizeof(stock));     //creating the struct address to fill the pointer for every input found.
    for(i=0; i<n; i++) {
        fscanf(input, "%s", stockp[i].ticker);          //scanning and adding ticker to the data
        for(j=0; j<20; j++) {
            fscanf(input, "%f", &stockp[i].value[j]);      //scanning and adding value array.
        }
        stockp[i].change = delta(stockp[i]);            //scanning nad adding stockp, avg, and colatile values
        stockp[i].avg = avg(stockp[i]);
        stockp[i].vol = vol(stockp[i]);
    }

    printf("The stock data has been loaded. ");
    fclose(input);      //closing the input file after data is loaded.
    printf("How would you like to sort the data? ");       //displaying menu
    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? ");      //selection of output file
    scanf("%s", filename);
    output = fopen(filename, "w");
    fprintf(output, "%d ", n);

    switch(choice) {            //choosing function according to operation 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); //closing the output file
    return 0;
}


In the above code, I have tried my best to explain you things with comments.

Hope this helps you. If you have any specific doubts, I will be glad to help you further.

Comment for any queries.