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) { }
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) { }
use a rand function.
ssharish2005Code:srand(time(0)); for(....) array[i] = rand() %100;
*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.
>Read your chapter covering loops again, because clearly you slept through it.
Doh, what's a loop?
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.
I don't see anything about line 52.Code:print_array(a, arraySize); // check final version at least was sortedstdc.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.*
> 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.