Thread: How to printf a function

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    5

    Question How to printf a function

    Hello, I want to enter 4 numbers and display the largest possible number. Can anybody help me with this please? I have tried multiply different approaches, so do not wonder why there are so many libraries. Thanks in advance

    Code:
    #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        #include <math.h>           //pow
        #include <limits.h>         //char max
        #include <ctype.h>          //
    
    
    int sort_alg(const void *a, const void *b)
    {
        char ab[32], ba[32];
        sprintf(ab, "%d%d", *(int*)a, *(int*)b);
        sprintf(ba, "%d%d", *(int*)b, *(int*)a);
        return strcmp(ba, ab);
    }
    
    void max_numb(int *a, int len)
    {
        int i;
        qsort(a, len, sizeof(int), sort_alg); 
        for (i = 0; i < len; i++)
            printf("%d", a[i]);
        putchar('\n');
    }
    
    int main(void)
    {
      int numbers[4];
      for(int count = 0; count < 4; count++)
      {
          scanf("%d", &numbers[count]);
          printf("%d", numbers);
      }
    
        max_numb(numbers, sizeof(numbers)/sizeof(numbers[0]));

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    What are you doing in sort_alg()? Wouldn't a simple "return *(int*)a < *(int*)b;" work just fine?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    Another way:
    Code:
    #include <stdio.h>
    
    #define NUMBER_COUNT  4
    
    int max_numb(int a[], int len)
    {
      int max = a[0], i;
      for (i = 1; i < len; i++)
        if (a[i] > max)
          max = a[i];
    
      return max;
    }
    
    int main(void)
    {
      int numbers[NUMBER_COUNT];
      for (int count = 0; count < NUMBER_COUNT; count++)
      {
        scanf("%d", &numbers[count]);
        //printf("%d", numbers[count]);
      }
    
      int max = max_numb(numbers, NUMBER_COUNT);
    
      printf("\nMax: %d", max);
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    5
    @OldGuy2
    Sorry, but I am not trying to find the largest entered number. My code is intended to create the largest combined number with any numbers given: if you enter 1, 2, 99, 919 the number would be 9991921.

    Thanks anyway

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    5

    Post Input problem

    Thank you for your effort, but I want to enter some random numbers which then should be put together to create the largest possible number:
    Code:
     1, 22, 77, 8, 9 -> 9877221.
    The sorting and the algorithm would have worked just fine, but they only work if I use pre set numbers like:
    Code:
     int x[] = {1, 99, 8, 444} and then max_numb(, sizeof(x)/sizeof(x[0]));

  6. #6
    Registered User
    Join Date
    Nov 2017
    Posts
    5
    Please see my post below...

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I didn't actually read your code, so my post might just be gibbering madness. Sorry about that.

    From what I understand you are trying to concatenate numbers. By your example:
    1. User enters 1, 2, 99, 919.
    2. Computer sorts entries by digit values. 1, 2, 99, 919 become 99, 919, 2, 1
    3. Computer concatenates sorted entries 99 + 919 + 2 + 1
    4. Computer outputs result 9991921.

    If I understand you correctly the functions that you want to use are strcmp to identify and sort strings and strcat to concatenate strings. I think it is important that you think of these as strings and not integers. If you want to convert to an integer when you are done, you can use strtol. If you want to store all the data in a single string, you would want to deliminate it with something like a space, and then iterate through it with strtok. Otherwise, you might want to store every entry in an array of strings. You are correct that there are many different approaches, but I think the string approaches are the best ones here.

    ***
    Edit: Actually I think strcmp might not give you the correct answer and you would just be better iterating over two strings yourself.
    Last edited by jack jordan; 11-19-2017 at 12:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf-like function ?
    By Algar in forum C Programming
    Replies: 3
    Last Post: 01-02-2013, 01:35 PM
  2. Debug function like printf()
    By Mordor in forum C Programming
    Replies: 7
    Last Post: 08-19-2010, 03:37 PM
  3. printf() function definition
    By saswatdash83 in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 07:34 AM
  4. printf like function
    By tyouk in forum C Programming
    Replies: 5
    Last Post: 11-17-2003, 07:26 PM
  5. printf like function
    By argon in forum C Programming
    Replies: 2
    Last Post: 10-07-2003, 03:12 PM

Tags for this Thread