urgently required help!!!

This is a discussion on urgently required help!!! within the C Programming forums, part of the General Programming Boards category; i need to know how to sort out four variables in ascending order via pointers w/o use of arrays, and ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    4

    Exclamation urgently required help!!!

    i need to know how to sort out four variables in ascending order via pointers w/o use of arrays, and && operators...
    do we make individual sorting cases...in this case 24 for 4 variables or sort the first three first and then introduce the third variable at the end...i've been trying for agaes...always get stuck and in the army of nested if and if else!!
    please urgent help required!!
    reply

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Okay . . . why don't you use a function like this?
    Code:
    void sort2(int *a, int *b) {
        
    }
    Then you could have a function like this.
    Code:
    void sort3(int *a, int *b, int *c) {
    
    }
    etc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    Find minimum of 4 value using min (it will be first)
    Find maximum of 4 value using max (it will be 4th)
    Fin 2 values that are different from two above
    minimum of them will be 2nd, maximum = 3rd
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    4

    Unhappy

    Code:
    #include<stdio.h>
    #include<conio.h>
    void sort (int*ptr1,int*ptr2,int*ptr3,int*ptr4);
    void main()
    {
    int a,b,c,d,*ptr1,*ptr2,*ptr3,*ptr4;
    clrscr();
    printf("enter four nos:");
    scanf("%d%d%d%d",&a,&b,&c,&d);
    sort(&a,&b,&c,&d);
    printf("\n\n%d-->%d-->%d-->%d\n\n",a,b,c,d);
    getch();
    }
    void sort(int*ptr1,int*ptr2,int*ptr3,int*ptr4)
    {
    int a=*ptr1,b=*ptr2,c=*ptr3,d=*ptr4;
    if(a<b);//-a-b- case
    {if(c<a);{*ptr1=c;*ptr2=a;*ptr3=b;}//-c-a-b-
    {if(d<c){*ptr1=d;*ptr2=c;*ptr3=a;*ptr4=b;}//dcab
    else if(d>c){*ptr2=d;*ptr1=c;*ptr3=a;*ptr4=b;}//cdab
    else if(d>a){*ptr1=c;*ptr2=a;*ptr3=d;*ptr4=b;}//cadb
    else(d>b);{*ptr1=c;*ptr2=a;*ptr3=b;*ptr4=d;}//cabd
    }
    if(b<c);//-a-b-c- case
    {if(d<a){*ptr1=d;*ptr2=a;*ptr3=b;*ptr4=c;}//dabc
    else if(d>a){*ptr2=d;*ptr3=b;*ptr4=c;}//adbc
    else if(d>b){*ptr3=d;*ptr4=c;}//abdc
    else(d>c);//abcd
    }
    if(c<b);{*ptr2=c;*ptr3=b;}//-a-c-b-
    {if(d<a){*ptr1=d;*ptr2=a;*ptr4=b;}//dacb
    else if(d>a){*ptr2=d;*ptr4=b;}//adcb
    else if(d>c){*ptr2=c;*ptr3=d;*ptr4=b;}//acdb
    else (d>b);{*ptr2=c;*ptr3=b;}//acbd
    }
    }
    
    if(c<b);{*ptr1=c;*ptr3=a;}//-c-b-a-
    {if(d<c){*ptr1=d;*ptr2=c;*ptr3=b;*ptr4=a;} //dcba
    else if(d>c){*ptr1=c;*ptr2=d;*ptr3=b;*ptr4=a;}//cdba
    else if(d>b){*ptr1=c;*ptr3=d;*ptr4=a;}//cbda
    else(d>a);{*ptr1=c;*ptr2=b;*ptr3=a;}//cbad
    }
    if(a<c);{*ptr1=b;*ptr2=a;}//-b-a-c-
    {if(d<b){*ptr1=d;*ptr3=a;*ptr4=c;}//dbac
    else if(d>b){*ptr1=b;*ptr2=d;*ptr3=a;*ptr4=c;}//bdac
    else if(d>a){*ptr1=b;*ptr2=a;*ptr3=d;*ptr4=c;}//badc
    else(d>c);{*ptr1=b;*ptr2=a;}//bacd
    }
    if(c<a);{*ptr1=b;*ptr2=c;*ptr3=a;}//-b-c-a-
    {if(d<b){*ptr1=d;*ptr4=a;}//dbca
    else if(d>b){*ptr1=b;*ptr2=d;*ptr4=a;}//bdca
    else if(d>c){*ptr1=b;*ptr2=c;*ptr3=d;*ptr4=a;}//bcda
    else(d>a);{*ptr1=b;*ptr2=c;*ptr3=a;*ptr4=d;}//bcad
    }
    }
    ...this is the code i worked out...isn't working anywyz..plz help!!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Don't use void main() . . . http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Break it up into functions like I suggested.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    4
    i've been asked to develop a single fuction for the task of sorting that should be usable in void main()...any help?

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    Maybe tell us what the problem is instead of saying 'isn't working anywyz..plz help!!'

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well, whoever told you to use void main is wrong. It doesn't matter if [s]he's your teacher or if they're a professional programmer. void main is wrong.

    This doesn't do what you think it does.
    Code:
    if(a<c);{*ptr1=b;*ptr2=a;}//
    The extra semicolon makes the compiler see that as
    Code:
    if(a<c){}
    *ptr1=b;*ptr2=a;
    so it's always executed.

    [edit] As for the problem . . .

    Code:
    if a > b then swap(a,b)
    if b > c swap(b,c)
    if c > d swap(c,d)
    // now d is the highest number
    if a > b then swap(a,b)
    if b > c swap(b,c)
    // now c is the second-highest
    // ...
    [/edit]
    Last edited by dwks; 01-10-2007 at 02:20 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Cheat.

    Build a singly linked list and sort the list. It fits all the requirements:
    - uses pointers and not arrays
    - you can code a sorting algorithm for linked lists without &&
    - one sort function that works in main.

    http://en.wikipedia.org/wiki/Linked_list
    http://en.wikipedia.org/wiki/Sorting_algorithm

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    4

    Talking

    thanks a lot guys..i got it right!!..i'm realy glad i joined this community so early in c...hope it'll be of great help and be a pleasant experience!
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Lvalue required error
    By eklavya8 in forum C Programming
    Replies: 5
    Last Post: 01-03-2009, 03:47 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21