## c – My program is repeated indefinitely, and frankly, I'm not sure why

So the problem here was to write a program that uses pointers to point to functions and write it to collect 10 doubles, give feedback to the user of the program, sort them and print the sorted results as proof , The problem is that the program either prints the printf statement infinitely at the beginning or accumulates an infinite number of numbers.

Here is a code

``````#include

Invalid function1 (double x);
void below_five (void);
void above_five (void);
void others (void);
void sort (double * p[]int n);
void print_doubles (double * p[]int n);

int main (void) {

double * numbers;
int nbr;
printf (" nEnter 10 doubles that are less than 5 or greater than 5, and type 0 to finish");
for (int i = 0; i <10; i ++)
{

scanf ("% d", & nbr);
func1 (nbr);
numbers[i] = nbr;

if (nbr == 0)
break;

}
sort (numbers, 10);
print_doubles (numbers, 10);
return 0;
}

void func1 (double val)
{
double (* ptr) (void);

if (val <= 5.00){
ptr = below_five;
}else if((val > 5.00) && (val <= 10.00)) {
ptr = above_five;
}otherwise
ptr = other;
}

void below_five (void) {
puts ("You entered a number less than or equal to five");
}

void above_five (void) {
puts ("You entered a number above five");
}

void other (void) {
puts ("You entered a number well above five.");
}

void sort (double * p[]int n)
{
double * tmp;
for (int i = 0; i < n; i++)
{
if(p[i] > p[i+1]) {
tmp = p[i];
p[i] = p[i+1];
p[i + 1] = tmp;
}
}
}

void print_doubles (double * p[]int n)
{
int count;
for (count = 0; count <n; count ++)
printf ("% d  n", p.[count]);

}
``````

As I said, I expect it to be able to collect double in the scanf method and then print the numbers after sorting. In this case, the for loop seems to collect Double forever without end.

What exactly did I do wrong?

Posted on Categories Articles