Random numbers and put them at a table

hello guys, i need your help at this exercise:
i have to write a program which reads random N numbers from the file clocktest.dat (i did it in the previous exercise) and put them at a table. At the table it will use bubble short for the numbers. Also, it have to counts the time of the bubble short ( sorry for the bad english guys)
i solve this but it doesn't run. Can you help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{
clock_t start, finish;
double duration;
int N,i,j,h;
int *num;
FILE *f;

f=fopen("clocktest.dat","r");
fscanf(f,"%d",&N);
num = (int *)malloc(N * sizeof(int));
for (i=0; i < N; i++)
{
fscanf(f, "%d", &num[i]);
printf("%d\n",num[i]);
}
start = clock();
for (j=0; j < N; j++)
for (i=0;i<N-1;i++)
if (num[i] >num[i+1]){
h=num[i];
num[i]=num[i+1];
num[i+1]=h;}

free(num);
fclose(f);

finish = clock();
duration = (double) (finish - start) / CLOCKS_PER_SEC;
printf("Total time is %5.2f seconds\n",duration);

system ("pause");
return 0;
}
Last edited on
int N (not plithos)
You can edit your post and change it yourself. There's a small window icon with a pencil on it next to your name on the top of your post. Click on it and you'll be able to modify the post.

Also, put your code between [code] and [/code] tags. This will make it look like this:

1
2
3
4
int main()
{
    return 0;
}

Are you sure you want to count the time in seconds?
How big is N? My guess is that this way you'll always get 0 seconds.
Maybe you want to count the number of comparisons or something...
Last edited on
ok, done...

N at first will be 1000.... yeah our teacher said to count it in that way.
So, what's the problem exactly? It compiles and runs fine here.
I tested it with a file with 5000 numbers and it took 0.17 seconds to sort them.
Last edited on
it dowsn't run to me.....


it shows me that

debug assertion failed!
program:C:\Users\user\Desktop\lab1ask2\Debug\lab1ask2.exe
File:f:\dd\vctools\crt_bld\self_x86\crt\src\fscanf.c
Line:52

Expression:(stream != NULL)

For information onhow your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press retry to debug the application)


you changed the N and put it 5000, right?
Does the file "clocktest.dat" exist? Is it in the correct folder?
i found it, you were right!!! i don't how to thank you!!!
Open your window and scream "I AM THE LORD OF THE WIND" five times.

EDIT: Also, tell me your opinion on this -> http://cplusplus.com/forum/lounge/38059/
Last edited on
hahaha

edit: i really like it, i think it's a good idea for a marriage proposal!
Last edited on
Thanks :D
Topic archived. No new replies allowed.