Thread: simple function question

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    112

    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. #2
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    112
    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. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,498
    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. #4
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    112
    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. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #6
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    112
    Thanks tabstop, that worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple function question
    By dhuan in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2010, 03:25 AM
  2. A simple question of a C++ function
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2007, 06:49 PM
  3. C Beep Function Simple Question
    By Matt3000 in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 06:43 PM
  4. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  5. simple question about function/random #'s
    By jemer in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2004, 10:08 PM

Tags for this Thread