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

Write a program that inputs data on the command line stock.x runs normal program

ID: 3598270 • Letter: W

Question

   Write a program that
inputs data on the command line

stock.x runs normal program
stock.x 13 is on command line
stock.x 13 26 13 is on command line but with spaces
stock.x 13, 26, 13, 40 is on command line but with commas

how do I implement this in the code and it knows how to decipher is user inputs commas or spaces or 1 number or execute regular program also still do the calculations mean median min max etc.

need to add to code below.

io.h:

void sort(const float input[], float output[], const int size,
const char order);
void read_data(float array[], int size);
void print_results(const float array[], int size, float mean,
float median, float min, float max, float variance);
void print_greeting();

io.c:

#include <stdio.h>
#include "io.h"




void read_data(float array[], int size)
{
int i = 0;
printf("Please enter the stocks you wish to analyze: ");
for(i = 0; i < size; i++)
{
printf("Please enter the stock price #%d: ", i + 1);
scanf("%f", &array[i]);
}
}


void print_results(const float array[], int size, float mean,
float median, float min, float max, float variance)
{
printf("Mean = $%.2f ", mean);
printf("Median = $%.2f ", median);
printf("Min = $%.2f ", min);
printf("Max = $%.2f ", max);
printf("Variance = $%.2f ", variance);
}


void print_greeting()
{
printf(" This application will compute the statistics of and sort ");
printf("a series of stock prices. ");
}

stats.h:

float get_average(const float data[], const int size);
float get_variance(const float data[], const int size);
float get_max(const float data[], const int size);
float get_min(const float data[], const int size);
void sort(const float input[], float output[], const int size,
const char order);
float get_median(const float input[], const int size);
void read_data(float array[], int size);
//void print_results(const float array[], int size, float mean, float
//median, float min, float max, float variance);
void print_greeting();

stats.c:

#include <float.h>
#include <limits.h>
#include "stats.h"


void sort(const float input[], float output[], const int size, const char order)
{
int i = 0,
j = 0;

float temp;
for(i = 0; i < size; i++)
output[i] = input[i];
for(i = 0; i < size - 2; i++)
{
for(j = i + 1; j <= size - 1; j++)
{
if(order == 'A')
{
if (output[i] > output[j])
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
else if(order == 'D')
{
if(output[i] < output[j])
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
}
}
}

float get_average(const float data[], const int size)
{
int i = 0;
float sum = 0.0,
average = 0.0;
for (i = 0; i < size; i++)
sum += data[i];
average = sum/size;
return average;
}

float get_max(const float data[], const int size)
{
    float max = data[0];
    for(int i = 1; i < size; i++)
        if(data[i] > max)
            max = data[i];
    return max;      
}
float get_min(const float data[], const int size)
{
    float min = data[0];
    for(int i = 0; i < size; i++)
        if(data[i] < min)
            min = data[i];
    return min;      
}
float get_median(const float input[], const int size)
{
    float output[size];
    sort(input, output, size, 'a');
    if(size % 2 == 0)
        return (output[size/2] + output[size/2+1]) / 2;
    else
        return output[size/2];  
}
float get_variance(const float data[], const int size)
{
    double mean = get_average(data, size);
    double variance = 0;
    for(int i = 0; i < size; i++)
        variance += (data[i] - mean) * (data[i] - mean);
    variance /= size;
    return variance;  
}

And stocks.c:

#include <stdio.h>
#include <float.h>
#include <limits.h>
#include "io.c"
#include "stats.c"


int main()
{
int size = 0;
float median = 0.0f,
variance = 0.0f,
max = 0.0f,
min = 0.0f,
average = 0.0f;
  
float array[size],
data[size],
input[size];

  
char order;
  
//print greeting
print_greeting();

printf("Enter the number of stocks we will be analyzing: ");
scanf("%d", &size);
//read data one at a time
read_data(data, size);
//Ask user if they want to sort their prices
printf("Do you want to sort the prices in ascending, ");
printf("or descending order? (A/D). ");
scanf(" %c", &order);
//get the stats
average = get_average(data, size);
median = get_median(data, size);
variance = get_variance(data, size);
max = get_max(data, size);
min = get_min(data, size);
//print the results
print_results(data, size, average, median, min, max, variance);
return 0;
}

Algorithms ata: Sck pnces input in several s Kesult: m,var anct, min.x und medizn hegin input dza irstr ¥re' .eskrn aiinine. '." "w.'wreau ,' then ylsr corm the numser uf ecuries; ne the put sin he values ino icTJys:

Explanation / Answer

Hi,
To read command line arguements we need to use the args vector into the main function like below,

int main(int argc,char *argv[] ) {

  for(i = 0; i < argc; i++)//argc holds the number of arguements passed on command line
{
array[i]= atof(argv[1]); // this converts the character to double
}
//once array is filld, all other code can be run
}
Thumbs up if this was helpful, otherwise let me know in comments

  

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