What is a segmentation error and what are some possible causes? I'll post code, if necessary, but I not asking for someone to do my troubleshooting.
What is a segmentation error and what are some possible causes? I'll post code, if necessary, but I not asking for someone to do my troubleshooting.
Most of the time, a segmentation fault is when you try and use a pointer when it is uninitialized, or when you try and write past the end of an array, things like that....anything having to do with memory mostly. if you post the code, we may be able to point it out.
The code is as follows...
#include <stdio.h>
#define MAXEL 10
double find_max(double [], double);
double find_min(double [], double);
double get_min_range(double list[], double first, double last);
void sel_sort(double list[], int n);
int main(void){
double nums[MAXEL];
double newInfo;
int i = 0;
int j = 0;
int k = 0;
double sum = 0;
FILE *input = fopen("lab6.in", "r");
if (input == NULL)
{
printf("Cannot open file.\n");
return(0);
}
for (k = 0; fscanf(input, "%lf", &newInfo) != NULL; j++)
nums[j] = newInfo;
fclose(input);
sel_sort(nums, 10);
printf("\nThe descending order of the array is:\n\n");
while( i < 10){
printf("%6.2f\n", nums[i]);
i++;
}
printf("\nThe maximum value in the array is %6.2f. \n", find_max(nums, 10));
printf("\nThe minimum value in the array is %6.2f. \n\n", find_min(nums, 10));
return(0);
}
double find_max(double last[], double num_ele){
int i, max = last[0];
for(i = 1; i < num_ele; i++)
if(max < last[i]) max = last[i];
return (max);
}
double find_min(double first[], double num_ele){
int i, min = first[0];
for(i = 1; i < num_ele; i++)
if(min > first[i]) min = first[i];
return (min);
}
double get_min_range( double list[], double first, double last){
int i, small_sub;
small_sub = first;
for(i = first + 1; i <= last; ++i)
small_sub = i;
return(small_sub);
}
void sel_sort(double *list, int n){
int temp;
int i;
for(i = 0; i < n - 1; i++){
int max = i;
int j = 0;
for(j = i + 1; j < n; j++){
if(list[j] < list[max]) { max = j; }
}
temp = list[max];
list[max] = list[i];
list[i] = temp;
}
}
/***********************************************/
/*** The file lab6.in reads as follows... */
5.00
2.00
3.00
8.00
10.00
7.00
4.00
1.00
0.00
9.00
/**** Thanks for your input! *****/
Is this a typo
k = 0; fscanf(input, "%lf", &newInfo) != NULL; j++)
nums[j] =
you have "K = 0;" and then use J
There are macros
min() and max()
Watch how you pass the arrays. the compiler will not like it if you try to pass too much info NOT as pointers. Try sending ALL the arrays as a pointersCode:for(i=0;i<10;i++) { fMin=min(fMin,fValue[i]); }
ie
prototype
double find_max(double *, double num_ele);
call
fValue=find_max(&last[0], last[0]);
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill