# Thread: simple function question

1. ## simple function question

Hey all, i've made considerable progress since February working on my C programming since February.

Code:
```/* #5. ON YOUR OWN: Write a program that uses pointers to type double variables to accept 10 numbers from the user, sort them, and print them to the screen. (Hint: See Listing 16.3.)

p 226

#5. ON YOUR OWN: Modify the program in the previous exercise to allow the user to specify whether the sort order is ascending or descending. */

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

double number[MAX];

void func1(void);
void sort(void);

int main( void )
{
int selection = 0;

int my_var = 0;

while (1)
{
printf("Enter sort method\n(0) to Exit \n(1) for Ascending \n(2) for Descending\n Enter: ");
scanf("%d", &selection);

switch (selection)
{
case 0:
puts("Exiting program...");
exit(0);
case 1:
puts("\nYou entered 1.");
my_var = 1;
break;
case 2:
puts("\nYou entered 2.");
my_var = 2;
break;
default:
puts("Out of range, try again");
break;
}

func1();
}
}

void func1(void)
{
int count;

count = 0;

for(count = 0; count < MAX; count++)
{
puts("\nEnter a number between 1 and 100: ");
scanf("%lf", &number[count]);

if (number[count] == 0)
{
puts("Error!\n");
break;
}

if (number[count] > 100)
{
puts("Error!\n");
break;
}

if (number[count] < 0)
{
puts("Error!\n");
break;
}

}

sort();
}

void sort(void)
{
int a, b, count;

a = 0;
b = 0;

for(count = 0; count < MAX; ++count)
{
for(a = 0; a < MAX -1; a++)
{
if(number[count] < number[a])
{
b = number[count];
number[count] = number[a];
number[a] = b;
}
}
}

puts("\nOutput:\n");

for(count = 0; count < MAX; ++count)
{
printf("%f\n", number[count]);
}
}```
The program works, in a way, however I need to figure out, whether it be sending case 1 & 2 to a separate function after each case is pressed to alternate this greater than or less than sign.

Code:
`if(number[count] < number[a])`
Thanks Salem for previous support on previous questions.

2. this is the updated code right here, pasted wrong one earlier:

Code:
```/* #6. ON YOUR OWN: Write a program that uses pointers to type double variables to accept 10 numbers from the user, sort them, and print them to the screen. (Hint: See Listing 16.3.)

p 226

#5. ON YOUR OWN: Modify the program in the previous exercise to allow the user to specify whether the sort order is ascending or descending. */

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

double number[MAX];

void func1(void);
void sort(void);

int main( void )
{
int selection = 0;

int my_var = 0;

while (1)
{
printf("Enter sort method\n(0) to Exit \n(1) for Ascending \n(2) for Descending\n Enter: ");
scanf("%d", &selection);

switch (selection)
{
case 0:
puts("Exiting program...");
exit(0);
case 1:
puts("\nYou entered 1.");
my_var = 1;
break;
case 2:
puts("\nYou entered 2.");
my_var = 2;
break;
default:
puts("Out of range, try again");
break;
}

func1();
}
}

void func1(void)
{
int count;

count = 0;

for(count = 0; count < MAX; count++)
{
puts("\nEnter a number between 1 and 100: ");
scanf("%lf", &number[count]);

if (number[count] == 0)
{
puts("Error!\n");
break;
}

if (number[count] > 100)
{
puts("Error!\n");
break;
}

if (number[count] < 0)
{
puts("Error!\n");
break;
}

}

sort();
}

void sort(int option)
{
int a, b, count;

a = 0;
b = 0;

if(option = 1)
{
for(count = 0; count < MAX; ++count)
{
for(a = 0; a < MAX -1; a++)
{
if(number[count] < number[a])
{
b = number[count];
number[count] = number[a];
number[a] = b;
}
}
}
}

if(option = 2)
{
for(count = 0; count < MAX; ++count)
{
for(a = 0; a < MAX -1; a++)
{
if(number[count] > number[a])
{
b = number[count];
number[count] = number[a];
number[a] = b;
}
}
}
}

puts("\nOutput:\n");

for(count = 0; count < MAX; ++count)
{
printf("%f\n", number[count]);
}
}```

3. Write a program that uses pointers to type double variables to accept 10 numbers from the user, sort them, and print them to the screen. (Hint: See Listing 16.3.)
Why the global array? You should be passing the array to and from the functions as required, this is probably the whole point of this exercise.

4. Hey all...i'm stuck...need support with this line

Code:
`sort(1);`
Should be returning a variable from func1() which is equal to 1 or 2 for Ascending or Descending sort. Thanks all!

Code:
```/* #6. ON YOUR OWN: Write a program that uses pointers to type double variables to accept 10 numbers from the user, sort them, and print them to the screen. (Hint: See Listing 16.3.)

p 226

#5. ON YOUR OWN: Modify the program in the previous exercise to allow the user to specify whether the sort order is ascending or descending. */

#include <stdio.h>
#include <stdlib.h>

#define MAX 5

double number[MAX];

int func1(void);
void func2(void);
void sort(int option);

int main(void)
{
int a;

func1();
func2();
sort(1);

return 0;
}

int func1(void)
{
int selection = 0;

int my_var;

while (1)
{
printf("Enter sort method\n(0) to Exit \n(1) for Ascending \n(2) for Descending\n Enter: ");
scanf("%d", &selection);

switch (selection)
{
case 0:
puts("Exiting program...");
exit(0);
case 1:
puts("\nYou entered 1.");
my_var = 1;
break;
case 2:
puts("\nYou entered 2.");
my_var = 2;
break;
default:
puts("Out of range, try again");
break;
}
break;
}
return my_var;
}

void func2(void)
{
int count;

count = 0;

for(count = 0; count < MAX; count++)
{
puts("\nEnter a number between 1 and 100: ");
scanf("%lf", &number[count]);

if (number[count] == 0)
{
puts("Error!\n");
break;
}

if (number[count] > 100)
{
puts("Error!\n");
break;
}

if (number[count] < 0)
{
puts("Error!\n");
break;
}

}
// place in main()
// sort(2);
}

void sort(int option)
{
int a, b, count;

a = 0;
b = 0;

// 1 = Ascending sort

if(option == 1)
{
for(count = 0; count < MAX; ++count)
{
for(a = 0; a < MAX -1; a++)
{
if(number[count] < number[a])
{
b = number[count];
number[count] = number[a];
number[a] = b;
}
}
}
}

// 2 = Descending sort

if(option == 2)
{
for(count = 0; count < MAX; ++count)
{
for(a = 0; a < MAX -1; a++)
{
if(number[count] > number[a])
{
b = number[count];
number[count] = number[a];
number[a] = b;
}
}
}
}

puts("\nOutput:\n");

for(count = 0; count < MAX; ++count)
{
printf("%f\n", number[count]);
}

exit(0);
}```

5. sort(1) just means that you always use option one, despite whatever your user might say or want.

If you want to use the option that your user typed in you have to, instead of deliberately throwing away their input as you do now, store it in a variable and then use that variable later. (Since the function call returns the number, you can use it on the right-hand side of an assignment, as in
Code:
`type = func1();`
Also, you shouldn't be calling a function "func1"; choose a meaningful function name, like get_sort_type or similar, and use that instead.)

6. Thanks tabstop, that worked.

Popular pages Recent additions