Thread: Reordering numbers

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    39

    Reordering numbers

    helllo guys. I'm having problem with this question:
    Write a program in C involving the use of pointers to reorder a list of numbers so that any of the following arrangements can be carried out :

    a) smallest to largest by magnitude
    b) smallest to largest, algebraic (by sign)
    c) largest to smallest, by magnitude
    d) largest to smallest, algebraic

    Here is my coding:
    Code:
    #include <stdio.h>
    #include<math.h>
    
    void main()
    {
    	int selection,count;
    	float nums[10];
    
    	printf("\n**************************MENU********************");
    	printf("\n 1. reorder from smallest to largest, by magnitude");
    	printf("\n 2. reorder from smallest to largest, algebraically");
    	printf("\n 3. reorder from largest to smallest, by magnitude");
    	printf("\n 4. reorder from largest to smallest, algebraically");
    	printf("\n****************************************************");
    	printf("\n Enter your selection please:");
    	scanf("%d",&selection);
    
    	
    	for(count=0;count<=9;++count)
    	{
    		printf("\nEnter a number:");
    		scanf("%d",&nums[count]);
    	}
    
    	switch (selection)
    	{
    	case 1:
    		reorder1(nums);
    		break;
    
    	case 2:
    		reorder2(nums);
    		break;
    
    	case 3:
    		reorder3(nums);
    		break;
    
    	case 4:
    		reorder4(nums);
    		break;
    	}
    	getchar();
    }
    int reorder1(float nums[])
    {
    	float item,temp;
    	int count,a;
    
    	for (item=0;item<=9;++item)
    	for (a=item+1;a<=9;++a)
    	if(abs(nums[item])>abs(nums[a]))
    	{
    		temp=nums[item];
    		(nums[item])=(nums[a]);
    		(nums[a])=temp;
    	}
    	for (count=0;count<=9;++count)
    	{
    		printf("%f",nums[count])
    		printf("\n");
    	}
    	return;
    }
    
    	int reorder2(float nums[])
    {
    	float item,temp;
    	int count,a;
    
    	for (item=0;item<=9;++item)
    	for (a=item+1;a<=9;++a)
    	if((nums[item])>(nums[a]))
    	{
    		temp=nums[item];
    		nums[item]=nums[a];
    		nums[a]=temp;
    	}
    	for (count=0;count<=9;++count)
    	{
    		printf("%f",nums[count])
    		printf("\n");
    	}
    	return;
    }	
    
    	int reorder3(float nums[])
    {
    	float item,temp;
    	int count,a;
    
    	for (item=0;item<=9;++item)
    	for (a=item+1;a<=9;++a)
    	if(abs(nums[item])<abs(nums[a]))
    	{
    		temp=nums[item];
    		nums[item]=nums[a];
    		nums[a]=temp;
    	}
    	for (count=0;count<=9;++count)
    	{
    		printf("%f",nums[count])
    		printf("\n");
    	}
    	return;
    }	
    int reorder4(float nums[])
    {
    	float item,temp;
    	int count,a;
    
    	for (item=0;item<=9;++item)
    	for (a=item+1;a<=9;++a)
    	if(nums[item]<nums[a])
    	{
    		temp=nums[item];
    		nums[item]=(nums[a]);
    		nums[a]=temp;
    	}
    	for (count=0;count<=9;++count)
    	{
    		printf("%f",nums[count])
    		printf("\n");
    	}
    	return;
    }
    Im having about 30 errors while im trying to build the program...
    Someone please help....

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    Yes, I get a lot of errors too
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c:5: warning: return type of 'main' is not `int'
    foo.c: In function `main':
    foo.c:22: warning: int format, float arg (arg 2)
    foo.c:28: warning: implicit declaration of function `reorder1'
    foo.c:32: warning: implicit declaration of function `reorder2'
    foo.c:36: warning: implicit declaration of function `reorder3'
    foo.c:40: warning: implicit declaration of function `reorder4'
    foo.c: In function `reorder1':
    foo.c:52: warning: implicit declaration of function `abs'
    foo.c:52: error: array subscript is not an integer
    foo.c:54: error: array subscript is not an integer
    foo.c:55: error: array subscript is not an integer
    foo.c:61: error: parse error before "printf"
    foo.c:63: warning: `return' with no value, in function returning non-void
    foo.c: In function `reorder2':
    foo.c:73: error: array subscript is not an integer
    foo.c:75: error: array subscript is not an integer
    foo.c:76: error: array subscript is not an integer
    foo.c:82: error: parse error before "printf"
    foo.c:84: warning: `return' with no value, in function returning non-void
    foo.c: In function `reorder3':
    foo.c:94: error: array subscript is not an integer
    foo.c:96: error: array subscript is not an integer
    foo.c:97: error: array subscript is not an integer
    foo.c:103: error: parse error before "printf"
    foo.c:105: warning: `return' with no value, in function returning non-void
    foo.c: In function `reorder4':
    foo.c:114: error: array subscript is not an integer
    foo.c:116: error: array subscript is not an integer
    foo.c:117: error: array subscript is not an integer
    foo.c:123: error: parse error before "printf"
    foo.c:125: warning: `return' with no value, in function returning non-void
    > Someone please help.
    Sure, you delete the source file and start again.
    But before you sit down and try and "eat the whole elephant at one sitting", have a good read of this.
    http://cboard.cprogramming.com/showthread.php?t=88495

    Then consider a more measured approach where you
    - write a bit
    - compile it
    - test it

    Rather than this "big bang" approach you have at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    39
    hahhaah.. ya thanks for ur advance

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    Well that really depends on whether you want to learn how to write programs doesn't it?

    Smashing your face into the keyboard until you have a "complete" program, then trying to compile the result (and failing miserably), then posting the whole sorry mess on a message board for some other schmoe to debug for you is NOT a long term strategy.

    So you may as well buckle down and learn how to write code in such a way that you can cope with whatever the compiler throws at you. This means learning the mantra of "compile often".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. Replies: 4
    Last Post: 03-03-2003, 03:52 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM