Thread: Help with formatted printing with arrays

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    Help with formatted printing with arrays

    I am needing to print my array in 5 lines of 10 numbers evenly spaced. this is what I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int compare (void *a, void *b);
    
    void main(void) {
    
    	int x, y, ia[50];
    
    	srand(time(NULL));
    	
    	for(x=0; x<50; x++) {
    		
    	        y=rand()%1000+1;
    		ia[x]=y;
    		printf("%d", ia[x]);
    		qsort(ia, 50, sizeof(int), compare);
    	
    	}
    }
    
    int compare(void *a, void *b) {
    
    	return *(int*)a-*(int*)b;
    
    }
    I not exactly sure how to do this.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    After every ten, (hint: when x is divisible by ten evenly), print a new line. Also sort the array after it has been filled, not while you are filling it, then you will need to print it again in the same manner.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am needing to print my array in 5 lines of 10 numbers evenly spaced.
    Test the remainder of division with 10:
    Code:
    for (x = 0; x < 50; x++) {
      printf("%-4d", ia[x]);
      if ((x + 1) % 10 == 0)
        printf("\n");
    }
    >int compare (void *a, void *b);
    qsort expects the comparison function to take pointers to const void:
    Code:
    int compare(const void *a, const void *b);
    >void main(void)
    On a hosted implementation, main must return int. We usually claim this because the standard says so in exquisite detail, but there are practical reasons as well. If the run-time startup code uses the return value for main, and you defined main as returning void, the result will be an indeterminate value. Aside from being undefined behavior if accessed, it also causes untold confusion for programs that use it. Even worse, due to different call/return sequences expected by the C run-time, the program may simply crash when it tries to call main, thus giving you an incredibly difficult bug to track down and fix.

    That said, your template for main without arguments is:
    Code:
    int main(void)
    {
      return 0;
    }
    And your template for main with arguments is:
    Code:
    int main(int argc, char *argv[])
    {
      return 0;
    }
    Or anything equivalent. The two biggies are the proper declaration and a valid return value.

    >srand(time(NULL));
    This will probably work (even though time(NULL) should be cast to unsigned int), but keep in mind that due to the restrictions on time_t, it's not portable. If you really want to use the current time as a seed for rand--a reasonable desire--then you can do so fairly easily by punning the time_t result of time(NULL) to a pointer to unsigned char and create a hash from the bytes:
    Code:
    #include <limits.h>
    
    unsigned int random_seed(void)
    {
      int i;
      unsigned seed = 0;
      time_t now = time(NULL);
      unsigned char *p = (unsigned char *)&now;
    
      for (i = 0; i < sizeof now; i++)
        seed = seed * (UCHAR_MAX + 2U) + p[i];
    
      return seed;
    }
    >qsort(ia, 50, sizeof(int), compare);
    qsort should be called outside of the loop, after all of the values have been assigned. At present, you're forcing qsort to access indeterminate values, which has undefined behavior.

    >return *(int*)a-*(int*)b;
    This is an exceptionally dangerous construct. You risk signed integer underflow (another thing with undefined behavior). You could make them unsigned integers, but that opens up new problems when (the now well defined) underflow reverses the desired effect.

    Compare this with what you had:
    Code:
    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    unsigned int random_seed(void);
    int compare(const void *a, const void *b);
    
    int main(void)
    {
      int x, y, ia[50];
    
      srand(random_seed());
    
      for (x = 0; x < 50; x++) {
        y = rand() % 1000 + 1;
        ia[x] =y ;
      }
    
      qsort(ia, 50, sizeof(int), compare);
    
      for (x = 0; x < 50; x++) {
        printf("%-4d", ia[x]);
        if ((x + 1) % 10 == 0)
          printf("\n");
      }
    
      return 0;
    }
    
    unsigned int random_seed(void)
    {
      int i;
      unsigned seed = 0;
      time_t now = time(NULL);
      unsigned char *p = (unsigned char *)&now;
    
      for (i = 0; i < sizeof now; i++)
        seed = seed * (UCHAR_MAX + 2U) + p[i];
    
      return seed;
    }
    
    int compare(const void *a, const void *b)
    {
      const int *ia = a;
      const int *ib = b;
    
      if (*ia < *ib)
        return -1;
      else if (*ia > *ib)
        return +1;
      else
        return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing arrays to curses screen
    By trev456 in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 12:46 AM
  2. printing arrays with concatenation
    By derek23 in forum C Programming
    Replies: 1
    Last Post: 07-17-2005, 03:02 AM
  3. New to C, printing arrays
    By kidglove14 in forum C Programming
    Replies: 1
    Last Post: 02-26-2002, 03:32 PM
  4. formatted printing with fprintf
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 11-20-2001, 09:04 AM
  5. Printing back arrays
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 12:50 PM