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

stocks.c #include <stdio.h> #include\"stats.h\" #define MAXIMUM_SIZE 100 void ma

ID: 3589597 • Letter: S

Question

stocks.c

#include <stdio.h>
#include"stats.h"
#define MAXIMUM_SIZE 100
void main() {

int data[MAXIMUM_SIZE];
int output[MAXIMUM_SIZE];
int size;
char choice ,choice1;
int i, j, temp;
float average ,median,max,min,variance;

  
printf("Do you want to enter the number of stocks: Y/N ");
//taking input for character
choice = getchar( );
switch ( choice ) {
case Y:
printf("Enter number of stocks: ");
scanf("%d", &size);
/* Input elements in array */
printf("Please Enter Price for the Stocks: ");
for(i=0; i<size; i++)
{
scanf("%d", &data[i]);
}
printf("How you want to sort the Price: A for Ascending/ D for Decending ");
//taking input for character
choice1 = getchar( );
switch ( choice1 ) {
case A:
average= get_average(const float data[], const int size);
variance = get_variance(const float data[], const int size);
max= get_max(const float data[], const int size);
min get_min(const float data[], const int size);
output= sort(const float input[], float output[], const int size,const char order);
median= get_median(const float input[], const int size);
break;
case D:
/* Code */
break;

}
break;
case N:
/* Input elements in array */
printf("Please Enter Price for the Stocks: ");
for(i=0; i<MAXIMUM_SIZE; i++)
{
scanf("%d", &data[i]);
}
printf("How you want to sort the Price: A for Ascending/ D for Decending ");
//taking input for character
choice1 = getchar( );
switch ( choice1 ) {
case A:
average= get_average(const float data[], const int size);
variance = get_variance(const float data[], const int size);
max= get_max(const float data[], const int size);
min get_min(const float data[], const int size);
output= sort(const float input[], float output[], const int size,const char order);
median= get_median(const float input[], const int size);
break;
case D:
/* Code */
break;

}
break;
  
}

printf("", );


}

Stats.h

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

}

float get_variance(const float data[], const int size){

float average= 0.0;
float sum = 0.0;
float sum_one= 0.0;
float variance = 0.0;
float temp = 0.0;
int i=0;
for (i; i < size ; i++) {
sum = sum + data[i];
}
average = sum / size;
for (i; i < size ; i++)
{
temp = data[i] - average;
sum_one = sum_one + temp * temp;
}
variance = sum_one / (float)size;
return variance;

}

float get_max(const float data[], const int size){

int i=0;
int position = 0;
float max_value = data[0];


for (i ; i < size; i++)
{
if (data[i] > max_value)
{
max_value = data[i];
position = i+1;
}
}
  
return data[position];

}

float get_min(const float data[], const int size){

int i=0;
int position = 0;
float min_value = data[0];


for (i ; i < size; i++)
{
if (data[i] < min_value)
{
min_value = data[i];
position = i+1;
}
}
  
return data[position];

}
float get_median(const float input[], const int size){
  
float median,temp = 0.0;
int i,j=0;


// Sorting the array - Ascending Order
for (i ; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (input[i] > input[j])
{
temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
}

if( size % 2 == 0){
median = (input[size/2] + input[size/2+1])/2.0 ;
}
else{
median = input[size/2 + 1];
}

return median;
}

void sort(const float input[], float output[], const int size,
const char order){

float temp = 0.0;
int i,j=0;


// Sorting the array - Ascending Order
if(order == 'a'){
for (i ; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (input[i] > input[j])
{
temp = input[i];
input[i] = input[j];
input[j] = temp;
output[j] =temp;
}
}
}
}

// Sorting the array - Decending Order
if(order == 'a'){
for (i ; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (input[i] < input[j])
{
temp = input[i];
input[i] = input[j];
input[j] = temp;
output[j] =temp;
}
}
}
}
return output[];
}

something in my array is giving me a few errors

stats.h:89:1: error: assignment of read-only location '*(input + (sizetype)((unsigned int)i * 4u))'
stats.h:90:1: error: assignment of read-only location '*(input + (sizetype)((unsigned int)j * 4u))'
D:Usersdn4AppDataRoamingMicrosoftWindowsRecentstats.h: In function 'sort':
stats.h:121:1: error: assignment of read-only location '*(input + (sizetype)((unsigned int)i * 4u))'
stats.h:122:1: error: assignment of read-only location '*(input + (sizetype)((unsigned int)j * 4u))'
stats.h:138:1: error: assignment of read-only location '*(input + (sizetype)((unsigned int)i * 4u))'
stats.h:139:1: error: assignment of read-only location '*(input + (sizetype)((unsigned int)j * 4u))'
stats.h:145:15: error: expected expression before ']' tokenstats.h:145:1: warning: 'return' with a value, in function returning void [enabled by default]

how do I go about fixing them to make the code to work. having a bit of trouble fixing it.

Explanation / Answer

You were gettting that error because you were trying to modify the input[] array in sort () function. The input is declared as const which means we can't modify its out. You intended to modify the output array actually. So I have modified the code to modify output array. Also initailly the values from input are copied onto output and then output array is sorted.

I have modifeid variance function as well to resue the code for average. Also in main , instead of passing an unsorted array, we pass the sorted array to median function. Cleaned up some code in main so there is no code repetition.

Also you don't need to use the keywords for const or other datatype while calling the functions in main(). You just pass the variable names.

Check the output. Let me know if any issues. If happy with the answer, please don't forget to rate it. Thank you.

stats.h

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

float get_variance(const float data[], const int size){
float average= 0.0;
float sum= 0.0;
float variance = 0.0;
float temp = 0.0;
int i=0;
  
average = get_average(data, size);
  
for (i = 0; i < size ; i++)
{
temp = data[i] - average;
sum = sum + temp * temp;
}
variance = sum / (float)size;
return variance;
}
float get_max(const float data[], const int size){
int i=0;
float max_value = data[0];
  
for (i = 0 ; i < size; i++)
{
if (data[i] > max_value)
{
max_value = data[i];
}
}
return max_value;
}
float get_min(const float data[], const int size){
int i=0;
float min_value = data[0];
  
for (i = 0; i < size; i++)
{
if (data[i] < min_value)
{
min_value = data[i];
}
}
return min_value;
}

//input is a sorted array
float get_median(const float input[], const int size){
  
float median;
int mid = size / 2;
  
if( size % 2 == 0){
//since array index start at 0, mid - 1 and mid should be considerd, for e.g. for size = 6, mid = 3, so elements at index 2 and 3 should be considered
median = (input[mid - 1] + input[mid])/2.0 ;
}
else{
median = input[mid];
}
  
return median;
}
void sort(const float input[], float output[], const int size,
const char order){
float temp = 0.0;
int i,j=0;
  
//copy all values to output
for(i = 0; i < size; i++)
output[i] = input[i];
  
// Sorting the array - Ascending Order
if(order == 'a' || order == 'A'){
for (i = 0 ; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (output[i] > output[j])
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
}
}
// Sorting the array - Decending Order
else if(order == 'd' || order == 'D'){
for (i =0 ; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (output[i] < output[j])
{
temp = output[i];
output[i] = output[j];
output[j] = temp;
}
}
}
}
}

stocks.c

#include <stdio.h>
#include"stats.h"
#define MAXIMUM_SIZE 100
int main() {
float data[MAXIMUM_SIZE];
float output[MAXIMUM_SIZE];
int size = MAXIMUM_SIZE; //default value when user does not input size
char choice, order;
int i, j, temp;
float average ,median,max,min,variance;
  
printf("Do you want to enter the number of stocks: Y/N ");
choice = getchar();
  
if(choice == 'Y' || choice =='y')
{
printf("Enter number of stocks: ");
scanf("%d", &size);
}
  
/* Input elements in array */
printf("Please Enter Price for the Stocks: ");
for(i=0; i<size ; i++)
{
scanf("%f", &data[i]);
}
printf("How you want to sort the Price: A for Ascending/ D for Decending ");
  
getchar();//discard the newline, otherwise next character read will be newline and will not wait for user input
  
//taking input for character
order = getchar( );
average= get_average(data, size);
variance = get_variance(data, size);
max = get_max(data, size);
min = get_min(data, size);
sort(data, output, size, order);
median= get_median(output, size); //use the sorted array to calculate median
  
  
//display the sorted array
for(i = 0; i < size; i++)
printf("%f ", output[i]);
printf(" =================== ");
printf("Min: %f ", min);
printf("Max: %f ", max);
printf("Average: %f ", average);
printf("Median: %f ", median);
printf("Variance: %f ", variance);
printf("=================== ");
  
}

output

amoeba-2:Test2 raji$ gcc stocks.c
amoeba-2:Test2 raji$ ./a.out
Do you want to enter the number of stocks: Y/N y
Enter number of stocks: 5
Please Enter Price for the Stocks:
17
12
19
15
10
How you want to sort the Price: A for Ascending/ D for Decending A
10.000000
12.000000
15.000000
17.000000
19.000000


===================
Min: 10.000000
Max: 19.000000
Average: 14.600000
Median: 15.000000
Variance: 10.640000
===================
amoeba-2:Test2 raji$ ./a.out
Do you want to enter the number of stocks: Y/N y
Enter number of stocks: 6
Please Enter Price for the Stocks:
19
15
18
16
14
20
How you want to sort the Price: A for Ascending/ D for Decending D
20.000000
19.000000
18.000000
16.000000
15.000000
14.000000


===================
Min: 14.000000
Max: 20.000000
Average: 17.000000
Median: 17.000000
Variance: 4.666667
===================