Hey guys I have this program that gives me "Segmentation Fault". I have posted the code below please can anyone tell me what's wrong ?....I am compileing it in munro using gcc.
Thanks
Code:
#include<stdio.h> /*Including Header Files */
#include<stdlib.h>
/*Function Prototypes */
void allocat(float *ptr_x,int size);
void read_num(float *ptr_x, int size);
float average(float *ptr_x,int size);
float range(float *ptr_x,int size);
void display(float *ptr_x, int size,float mean,float rang);
/* Main Function Starts here */
int main(int argc, char *argv[]) /* Passing Comand line Arguments */
{
int size = atoi(argv[1]); /* Converting char string to integer */
float rang;
float *ptr_x;
float mean;
allocat(ptr_x,size);
read_num(ptr_x,size); /* Function Calls */
mean = average(ptr_x,size);
rang= range(ptr_x,size);
display(ptr_x,size,mean,rang);
return 0; /* Returning 0 */
}
/*********************************************
* Purpose: To dynamically allocate the memory for values *
* Pre Condition: Passing the pointer for memory allocation *
* and passing the integer size of array *
* Post Condition: None *
**********************************************/
void allocat(float *ptr_x,int size)
{ /* allocating the memory dynamically by malloc */
ptr_x = (float*)malloc(size * sizeof(float));
} /* type cast to avoid void pointers */
/*********************************************
* Purpose: To read the values entered from the keyboard *
* Pre Condition: Passing the pointer for memory allocation *
* and passing the integer size of array *
* Post Condition: None *
*********************************************/
void read_num(float *ptr_x,int size)
{
int i;
printf("Enter the Values:");
for(i=0; i<size; i++)
{
scanf("%f",&ptr_x[i]);
}
}
/*********************************************
* Purpose: Calculating the average of the values entered *
* from the keyboard *
* Pre Condition: Passing the pointer for array *
* and passing the integer size of array *
* Post Condition: Returning the average as float *
*********************************************/
float average(float *ptr_x,int size)
{
int i;
float sum = 0;
float mean;
for(i=0; i<size; i++)
{
sum += ptr_x[i]; /* taking the sum */
}
mean = sum/size; /* calculating the average */
return mean; /* returning the average */
}
/*******************************************
*Purpose: Calculating the range of the values entered *
* from the keyboard *
* Pre Condition: Passing the pointer for array *
* and passing the integer size of array *
* Post Condition: Returning the range as float *
********************************************/
float range(float *ptr_x,int size)
{
int i;
float rang;
float temp[2];
temp[0] = ptr_x[0];
for(i=0; i<size; i++)
{
if(temp[0]<ptr_x[i])
{
temp[0] = ptr_x[i]; /* largest value */
}
}
for(i=0; i<size; i++)
{
if(temp[1]>ptr_x[i])
{
temp[1] = ptr_x[i]; /* Smallest Value */
}
}
rang = temp[0] - temp[1]; /* calculatin the range */
return rang; /* returning the range */
}
/********************************************
* Purpose: Displaying the values, the average of the *
* values, the range of the values entered *
* from the keyboard *
* Pre Condition: Passing the pointer for array, its average*
* and its range and passing the integer *
* size of array *
* Post Condition: None *
********************************************/
void display(float *ptr_x, int size,float mean,float rang)
{
int i;
printf("The entered Values:\n");
for(i=0; i<size; i++)
{
if((i%5)==0) printf("\n");
printf("%6.2f",ptr_x[i]);
}
printf("\nThe average is:%f\n",mean);
printf("The range is :%f\n",rang);
}