Thread: Programming question.

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    7

    Question Programming question.

    When writing my code, I can get to sort my numbers as long as I list the first number to be the lowest. if I use random numbers like 1, 456,and 12 it will work but when I change them up say to 456,1,and 12 it breaks. Here is part of my code.
    insert
    Code:
    void myfunction(double *a1, double *b1, double *c1)
    {    double small, middle, large;
        if (*a1 <= *b1)
        {
        if (*a1 <= *c1)
        {
        small = *a1;
        if (*b1 <= *c1)
        {
        middle = *b1;
        large = *c1; }
        else {
        middle = *c1;
        large = *b1;
        } } }
        else
        myfunction(b1, a1, c1);
        *a1 = small;
        *b1 = middle;
        *c1 = large; }
    // Unsorted and sorted numbers
    void sortandprint(double a1, double b1, double c1)
    {    printf("Static before numbers: %2.lf, %2.lf, %2.lf\n", a1, b1, c1);
        myfunction(&a1, &b1, &c1);
        printf("Sorted static numbers: %2.lf, %2.lf, %2.lf\n", a1, b1, c1); }
    // Print and sort
    int main(void)
    {     sortandprint(9, 125, 789);
        system("PAUSE");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2016
    Posts
    7
    Run-Time Check Failure #3 - The variable 'small' is being used without being initialized.
    Static before numbers: 72874, 44105, 25311
    Sorted static numbers: -92559631349317831000000000000000000000000000000000 000000000000, -92559631349317831000000000000000000000000000000000 000000000000, -92559631349317831000000000000000000000000000000000 000000000000

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's easier if you use a swap().
    Code:
    if (*a1 > *c1) swap(a1, c1);
    if (*a1 > *b1) swap(a1, b1);
    if (*b1 > *c1) swap(b1, c1);
    If you don't swap, you essentially have to code for all of the possible permutations (of which there are 6)

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    7
    I need to use double per the assignment.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Then implement a swap function that uses doubles.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Unrelated to your question, you should strive for good formatting and indentation in your code. This makes it easier to read, understand, and find possible mistakes.

    Quote Originally Posted by Thermal View Post
    Code:
        } } }
    Three closing braces on a single line is definitely as sign of poor formatting.

  7. #7
    Registered User
    Join Date
    Nov 2016
    Posts
    7
    Thanks, I corrected the formatting to a better ascetically pleasing version. For the Swap double, I have never used it, so I do not know how it works. I am trying to figure it out. Sorry new to this.

  8. #8
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    How to swap two values....

    declare temp variable.
    set temp to value1
    set value 1 to value 2
    set value 2 to temp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about XMS programming
    By jinhao in forum C++ Programming
    Replies: 9
    Last Post: 08-25-2005, 09:11 AM
  2. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM
  3. C Programming Question
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 12-07-2001, 06:44 AM
  4. Programming question
    By face_master in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 07:00 AM

Tags for this Thread