Thread: My run-time code isn't outputting anything?

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    13

    My run-time code isn't outputting anything?

    This program is supposed to sort an array of random numbers and print out the time it took for the sort to take place. I'm currently not getting any output. Help would be appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int swap,temp,i,j,s,a[];
    float function_time;
    clock_t start;
    clock_t end;
    
    int main()
    {
     printf("Enter total number of elements: ");
     scanf("%d",&s);
    
     a[i] = rand()%10000;
     for(i=0;i<s;i++)
     {
      scanf("%d", &a[i]);
     }
     start = clock();
     for(i=1;i<s;i++)
     {
      temp=a[i];
      j=i;
      for(j=i-1;j>=0 && a[j]>temp; j--)
      {
       a[j+1]=a[j];
      }
      a[j+1]=temp;
     }
     
     end = clock();
     function_time = (float)(end-start)/(CLOCKS_PER_SEC); // Time in seconds
     printf("\nTime for Insertion Sort is %f seconds\n", function_time);
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    your code has lots of problems:

    1. you never specify a size for your array, and it is assumed to have 1 element.
    2. all of your variables are uninitialized, making line 15 particularly troublesome.
    3. you use global variables for everything. why? you have one function, and no external code needs to access them. make them local to main.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Elkvis View Post
    your code has lots of problems:

    1. you never specify a size for your array, and it is assumed to have 1 element.
    GCC assumes 1 element in such cases. Do you know if that is guaranteed by the standard, and if so, in what section?
    Quote Originally Posted by Elkvis View Post
    2. all of your variables are uninitialized, making line 15 particularly troublesome.
    Not quite. Since they're all global, and have static storage duration, thus are all initialized. They're just initialized to zero (except for the clock_t, whose initialization depends on how it's implemented)
    Quote Originally Posted by Elkvis View Post
    3. you use global variables for everything. why? you have one function, and no external code needs to access them. make them local to main.
    Yes. Global variables are bad.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by anduril462 View Post
    GCC assumes 1 element in such cases. Do you know if that is guaranteed by the standard, and if so, in what section?
    I don't know what the standard says. I used GCC to compile it, and that's what it said. Just a guess, but it's probably implementation-defined or unspecified.

    Not quite. Since they're all global, and have static storage duration, thus are all initialized. They're just initialized to zero (except for the clock_t, whose initialization depends on how it's implemented)
    fair enough, but line 15 still has a problem, because depending on how the standard defines arrays like this, even accessing index zero may be undefined.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anduril462
    GCC assumes 1 element in such cases. Do you know if that is guaranteed by the standard, and if so, in what section?
    The type is an incomplete type:
    Quote Originally Posted by C99 Clause 6.2.5 Paragraph 22a
    An array type of unknown size is an incomplete type. It is completed, for an identifier of that type, by specifying the size in a later declaration (with internal or external linkage).
    Quote Originally Posted by C99 Clause 6.7.5.2 Paragraph 4a
    If the size is not present, the array type is an incomplete type.
    Unless C11 changed something here, I believe that we are in the realm of undefined behaviour since the type is never completed yet the object is used. I reason that since the array has static storage duration, gcc automatically does something equivalent to:
    Code:
    int a[] = {0};
    hence the corresponding warning about assuming that the array has one element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-28-2012, 12:40 PM
  2. Outputting Current Time in C++
    By SeeForever in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2010, 06:29 PM
  3. Outputting the code
    By Jailan in forum C Programming
    Replies: 14
    Last Post: 10-23-2007, 07:50 PM
  4. outputting characters one at a time
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 08-11-2002, 09:26 AM

Tags for this Thread