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.