c - Why am I getting a value of 0 for my min and max? -
#include <stdio.h> #include <stdlib.h> #include <time.h> /* initialize random generator */ #define buff_size 10 #define 5 five #define ten 10 #define 0 0 #define 1 one float min=0; float max=0; float average=0; float input_buffer[buff_size+2] = {0}; float output_buffer[five] = {0}; float local_buffer[ten]; int no_of_data_pts=0; int function_id=0; // function generating data size, function id , // data, , putting of input_buffer void generate_data() { /* initialize random seed: */ srand ( time(null) ); /* generate random number: */ input_buffer[0] = floor(1 + 10*(float)rand()/rand_max); input_buffer[1] = floor(1 + 4*(float)rand()/rand_max); int i; (i=0; < (int)input_buffer[0]; i++ ){ input_buffer[i+2]=(float)rand()/rand_max; } } // function copying content of input_buffer // local buffer (called here my_buffer) void reading() { no_of_data_pts= (int)input_buffer[0]; function_id= (int)input_buffer[1]; int i; (i=0; < no_of_data_pts; i++ ){ local_buffer[i]=input_buffer[2+i]; } } // function processing content of local buffer; // reads function_id , number of data points my_buffer // , saves results my_buffer void processing() { float num=0; int i; float sum = 0; float min=0; (i=0; i<no_of_data_pts; i++){ num = local_buffer[i+1]; if (num < min) { min=num; } } (i=0;i<no_of_data_pts;i++){ num = local_buffer[i+1]; if (num < max) { max=num; } } (i=0;i<no_of_data_pts;i++) { sum = sum + local_buffer[i]; } average = sum/no_of_data_pts; } // function copying content of my_buffer // output_buffer (according patter explained earlier) void writing() { switch (function_id){ case 1: output_buffer[0]= min; printf ("minimum value is: %f ",output_buffer[0]); break; case 2: output_buffer[0]= max; printf ("maximum value is: %f ",output_buffer[0]); break; case 3: output_buffer[0]= average; printf ("average value is: %f ",output_buffer[0]); break; case 4: output_buffer[0]= min; output_buffer[1]= max; output_buffer[2]= average; printf ("minimum, maximum , average value is: %f,%f,%f ",output_buffer[0],output_buffer[1],output_buffer[2]); break; } } int main () { setvbuf(stdout, null, _ionbf, 0); setvbuf(stderr, null, _ionbf, 0); generate_data(); reading(); processing(); writing(); return 0; }
so c assignment.
so when run code, it's supposed generate random numbers 0 1 , calculate min, max , average.
the reading of data input buffer involves:
- reading number first element of input buffer, if non-zero number (and should be), program performs 2 following operations, number represent number of data points read (no_of_data_points)
- reading id of processing function (function_id)
- reading data points id of data processing function , points read local buffer
the processing of data involves:
- based on read id of processing function specific calculations performed, 4 different processing can take place:
- if id 1: minimum of data points determined
- if id 2: maximum of data points determined
- if id 3: average of data points determined
- if id 4: minimum, maximum , average of data points determined results should placed local buffer.
the writing of data output buffer involves:
- writing number indicating how many data points written output buffer (it includes id of processing function, , obtained results)
writing number, called id of processing function, indicating operation performed on data (range 1 4)
writing result, , depends functions was:
§ if id 1: value of minimum written
§ if id 2: value of maximum written
§ if id 3: value of average written
§ if id 4: values of minimum, maximum , average written
i print results @ end of program.
the code runs fine. there no errors reason, cannot figure out why values of min , max 0! value average fine max , min values 0 reason , that's not right.
for starters, this
if (num < max) { max = num; }
shall
if (num > max) { max = num; }
at least.
also using above approach initialising min
, max
0
might not work kind of input.
to sure detect possiblities of input initialise
min = flt_max;
and
max = -flt_max;
if smallest/largest possible values unknown reason, change way how min
, max
being detected:
void processing() { float num=0; int i; float sum = 0; min = local_buffer[1]; (i = 1; < no_of_data_pts; i++){ num = local_buffer[i + 1]; if (num < min) { min = num; } } max = local_buffer[1]; (i = 1; < no_of_data_pts; i++){ num = local_buffer[i + 1]; if (num > max) { max = num; } } ...
also^2 code defines min
twice:
- globally
- local
processing()
, shadowing 1.
remove 2nd definition.
the code misses protoype floor()
. that's why compiler assume return int
. might invoke undefined behaviour.
to fix add
#include <math.h>
also^3 when reading out local_buffer
in processing()
code use wrong indexing. index starts @ 0
not @ 1
. reading out last value had not been set generator. that's 0
min
comes from.
correcting make above snippet processing()
like:
void processing() { float num=0; int i; float sum = 0; min = local_buffer[0]; (i = 1; < no_of_data_pts; i++){ num = local_buffer[i]; if (num < min) { min = num; } } max = local_buffer[0]; (i = 1; < no_of_data_pts; i++){ num = local_buffer[i]; if (num > max) { max = num; } } ...
final notes:
- listen compiler, take warnings serious.
- you might take consideration learning how use debugger, able figure such issues on own next time.
Comments
Post a Comment