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: 3589371 • 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.

Explanation / Answer

Listed below may be the causes of a few of your errors:

1. When you declare local variables i and j, such that,

int i,j=0,

only j is 0, i remains undefined, which means it has no value.

You should rewrite this as int i=0,j=0;

2. Inside some methods, you have used multiple for loops. But you have initialized i=0 only before beginning of first for loop. This may lead to loop reading wrong array values as i may be wrong. To prevent this, either initialize i=0 before each loop, or use:

for(int i=0; i<size;i++) instead of just for(i;i<size;I++).

Hopefully all your errors should be rectified once you correct these 2 things everywhere in your code. If some errors remain, reach out to me in comments. Please upvote if you found this helpful.