random fill array

This is a discussion on random fill array within the C Programming forums, part of the General Programming Boards category; Hi, i need a function that will fill the arrary with random values, so far is what i have done ...

  1. #1
    Registered User zeromx's Avatar
    Join Date
    Oct 2006
    Location
    uk
    Posts
    6

    random fill array

    Hi,
    i need a function that will fill the arrary with random values, so far is what i have done below, im a newbie so any help appricated, thank you.

    Code:
    void fill_array (int array [], int arraySize)
    {
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    use a rand function.
    Code:
    srand(time(0));
    for(....)
    array[i] = rand() %100;
    ssharish2005

  3. #3
    Registered User zeromx's Avatar
    Join Date
    Oct 2006
    Location
    uk
    Posts
    6
    i get a error for

    Code:
    for(....)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *sigh*

    Come on man, use your head! No, that doesn't mean lay it on your keyboard. Think once in a while! Obviously the "...." was just saying "fill in what you need here". That's clearly not a valid for loop, and wasn't even intended for you to think it was.

    Read the FAQ on random numbers. Read your chapter covering loops again, because clearly you slept through it. Post your actual attempt, not just the function fragment they gave you for your homework.

    I suppose I'll have to start assuming everyone is even stupider than I think they are. How depressing.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Read your chapter covering loops again, because clearly you slept through it.
    Doh, what's a loop?

  6. #6
    Registered User zeromx's Avatar
    Join Date
    Oct 2006
    Location
    uk
    Posts
    6
    here is my full code:

    i get this error on line 52: 52 naivesort.c `for' loop initial declaration used outside C99 mode
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    const int RANGE = 200; // random data used in range 0-199
    
    // Functions
    void bubble_sort (int array [], int arraySize, int * count);
    void swap (int * v1, int * v2);
    void fill_array (int array [], int arraySize);
    void print_array(int array[], int sizeOfData);
    
    int main()
      {
         const int arraySize = 100;
         int a[arraySize];
         int datasize = 1;
         FILE *resultsFile;
         
         // open the file for writing ("w")
         resultsFile = fopen("nsort.dat", "w");  
    
         if (resultsFile != NULL)      // file successfully opened
          {
             // seed the random number generator so it gives different
           // random numbers each time
           srand ( time (0) );
    
    
           // Table headings
           printf ("Found\t Data Size\t Comparisons Needed\n"  );
           
           //\t puts in a tab character, \n a new line
           fprintf (resultsFile, "Data Size\t Comparisons Needed\n"  );
    
           // Generate table for data of different sizes
           // giving number of comparisons each time
    
           for (datasize; datasize <= arraySize; datasize = datasize + 1)
    		{
    	  		int count = 0;  // number of comparisons this time
    
    	  		fill_array(a, datasize);
    
    	  		bubble_sort (a, datasize, & count);
    
    	  		// print results in a table
              	printf("%d  \t\t\t  %d\n", datasize, count);
              	fprintf(resultsFile, "%d  \t\t\t  %d\n", datasize, count);
    		}
    
            print_array(a, arraySize); // check final version at least was sorted
    
           
         }
       else
          fprintf(resultsFile, "File not opened\n");
    
       fclose(resultsFile); return 0;
      }
    /*
    void bubble_sort (int array [], int arraySize, int * count)
    {
         
    }*/
    void swap (int * v1, int * v2)
    {
    
    }
    void fill_array (int array [], int arraySize)
    {
         
    }
    void print_array(int array[], int sizeOfData)
    {
         
    }
    Last edited by zeromx; 11-06-2006 at 07:30 PM.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I don't see anything about line 52.
    Code:
            print_array(a, arraySize); // check final version at least was sorted
    stdc.c
    stdc.c:5:24: warning: C++ style comments are not allowed in ISO C90
    stdc.c:5:24: warning: (this will be reported only once per input file)
    stdc.c: In function `main':
    stdc.c:16: warning: ISO C90 forbids variable-size array `a'
    stdc.c:39: warning: statement with no effect
    Linking...
    ide/Debug/stdc.o: In function `main':
    J:/Forum/stdc.c:45: undefined reference to `bubble_sort'
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > const int arraySize = 100;
    > int a[arraySize];
    This isn't valid C either.

    It's OK in C++ and in C99, but not normal (C89) C.

    Use
    #define arraySize 100
    int a[arraySize];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Filling an array with random numbers
    By euclid in forum C Programming
    Replies: 21
    Last Post: 11-28-2007, 06:53 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Fill Positions of one Array into another Array
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 05-06-2002, 10:10 PM
  5. How to fill a 2d array from a file?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2002, 06:01 PM

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