Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Memory leak in C program. I used valgrind tool. I think it is about pointer issue on for loop but I am not sure and

Memory leak in C program. I used valgrind tool.

I think it is about pointer issue on for loop but I am not sure and why it is happening.

I made only one function which is float *discard_outliers(float *measurements, int size, float median, int *new_size).

valgrind says

==27399== LEAK SUMMARY:

==27399== definitely lost: 0 bytes in 0 blocks

==27399== indirectly lost: 0 bytes in 0 blocks

==27399== possibly lost: 72 bytes in 3 blocks

==27399== still reachable: 5,133 bytes in 16 blocks

==27399== suppressed: 22,093 bytes in 154 blocks

==27399== Reachable blocks (those to which a pointer was found) are not shown.

task1_main.c

#include

#include

#include "task1.c"

int main()

{

int i, size1, size2;

// reading the number of measurements in group1

// scan user's first input which means size of group1

scanf("%d", &size1);

float *measurements1 = malloc(size1*sizeof(float));

//malloc(size1*4)

// reading the measurements in group1

for(i=0; i

scanf("%f", measurements1+i);

// reading the number of measurements in group2

scanf("%d", &size2);

float *measurements2 = malloc(size2*sizeof(float));

// reading the measurements in group1

for(i=0; i

scanf("%f", measurements2+i);

//get median of measurements in group1

float median1 = sort_and_find_median(measurements1, size1);

int new_size1;

printf(" ");

for(i=0; i

printf("%.2f ", measurements1[i]);

float *measurements1_wo_outliers = discard_outliers(measurements1, size1, median1, &new_size1);

float median2 = sort_and_find_median(measurements2, size2);

printf(" ");

for(i=0; i

printf("%.2f ", measurements2[i]);

int new_size2;

float *measurements2_wo_outliers = discard_outliers(measurements2, size2, median2, &new_size2);

// writing measurements for group1 after discarding the outliers

printf("%d ", new_size1);

for(i=0; i

printf("%.2f ", measurements1_wo_outliers[i]);

printf(" ");

// writing measurements for group2 after discarding the outliers

printf("%d ", new_size2);

for(i=0; i

printf("%.2f ", measurements2_wo_outliers[i]);

free(measurements1);

free(measurements2);

free(measurements1_wo_outliers);

free(measurements2_wo_outliers);

return 0;

}

task1.c

// function to sort the array in ascending order

float sort_and_find_median(float *measurements , int size)

{

int i=0 , j=0;

float temp=0;

for(i=0 ; i

{

for(j=0 ; j

{

if(measurements[j]>measurements[j+1])

{

temp = measurements[j];

measurements[j] = measurements[j+1];

measurements[j+1] = temp;

}

}

}

return measurements[size/2];

}

float *discard_outliers(float *measurements, int size, float median, int *new_size)

{

int cnt=0;

printf("value of *measurements: %f ", *measurements);

printf("value of size: %d ", size);

printf("value of median: %f ",median);

int n=0;

int m=size; //n= boundary of outliers

for(int i=0; i

float t = *(measurements+i);

if(t<(0.5*median)){

//it could be a boundry of outlier or not

printf("outlier: %f ",t);

cnt++;

n=i+1;

printf("value of n is: %d in loop ",n);

}

else printf("value of n is: %d ",n);

if(t>(1.5*median)){

printf("outlier: %f ",t);

cnt++;

m=i;

printf("value of m is: %d ",m);

}

}

printf("Min max: %d,%d ",n,m);

*new_size=size-cnt;

printf("value of *new_size: %d ", *new_size);

float *measurements_wo_outliers = malloc( (*new_size) * sizeof(float) );

// for(int j=0; j<*new_size; j++){

// int i = n;

// i++;

// float t = *(measurements+i);

// *((measurements_wo_outliers)+j)=t;

//printf("%f",t);

//}

int cnt1=0;

for(int i=n; i<*new_size+n+1; i++){

cnt1++;

float t = *(measurements+i);

if(cnt1<=*new_size){

*((measurements_wo_outliers)+(i-n))=t;

printf("%2f ",t);

}

}

return measurements_wo_outliers;

}

measurement input

25 23.0 21.5 27.6 2.5 19.23 21.0 23.5 24.6 19.5 19.23 26.01 22.5 24.6 20.15 18.23 19.73 22.25 26.6 45.5 5.23 18.0 24.5 23.26 22.5 18.93 20 11.12 10.32 9.91 14.32 12.32 20.37 13.32 11.57 2.32 13.32 11.22 12.32 10.91 8.32 14.56 10.16 35.32 12.91 12.58 13.32

Expected output

22 18.00 18.23 18.93 19.23 19.23 19.50 19.73 20.15 21.00 21.50 22.25 22.50 22.50 23.00 23.26 23.50 24.50 24.60 24.60 26.01 26.60 27.60

17 8.32 9.91 10.16 10.32 10.91 11.12 11.22 11.57 12.32 12.32 12.58 12.91 13.32 13.32 13.32 14.32 14.56

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building The Data Warehouse

Authors: W. H. Inmon

4th Edition

0764599445, 978-0764599446

More Books

Students also viewed these Databases questions