"^cypis^ vel. SQ9JTI" <sq****@gmail.comwrote in
news:11*********************@q40g2000cwq.googlegro ups.com:
I have the part of code:
#include "stdafx.h"
void readData(int *numbers[20]){
FILE *file;
int i=0;
file=fopen("dane.txt","r");
while(!feof(file)){
fscanf(file,"%d", &numbers[i]);
i++;
}
fclose(file);
//return i;
}
void Bucketsort(int *liczby[20]){
int tab [10][20];
WHAT TO DO HERE???:)
You implement your bucket sort here.
>
}
int main(int argc, char* argv[])
{
int *t[20];
readData(t);
printf("Read values:\n");
for(int i=0;i<20;i++) printf("%d ",t[i]);
printf("\n\n");
char z;
printf("Choose the mode : ");
printf("[r]A-Z, [m]Z-A \n"); scanf("%c",&z);
if (z=='r')
{
Sortr(t);
//licznik=Sortr(t, ile);
}
//else if (z=='m')
//{
//Sortm(t, ile);
//Sortm(t);
//}
printf("Sorted values:\n");
for(i=0;i<20;i++) printf("%d ",t[i]);
printf("\n\n");
return 0;
}
What should i put in the place of: "WHAT TO DO HERE???:)"
That's where you implement your bucket sort. How to implement a bucket
sort is precisely the point behind your homework assignment. (And isn't
specifically a C++ question, it's an algorithm question.)
Now, for C++ comments about your code:
1) This is only technically C++ code. So far you haven't done anything
C++ specific, so far it's all C.
2) I think the datatype is wrong for the variable "t" in main(). You
have it declared as an array of 20 pointers to int. Isn't that supposed
to be an array of 20 ints? You seem to be using it elsewhere in the code
as an array of ints.
3) You call a function named Sortr in main() (and will eventually call
Sortm), but you don't have either function defined.
4) This one is a little more stylistic in nature (and a bad habit): Magic
Numbers. You have the number 20 in various places around your code.
What you should do is assign that to a const int, and use that variable
wherever you're using that 20. This way if you end up changing that
bound (say now you want to sort 40 numbers), you only need to change the
number in one place instead of the 5 places you have now, plus whatever
extra instances you add in your bucket sort.
5) Even better than #4, don't use a "naked" array, use std::vector. Have
your readData() function populate the vector, and then wherever else you
need to know how many numbers you're dealing with, you can use the size()
method on the vector to ask it how big it is. Additionally this makes
your program even more generic in that it doesn't depend on there being
exactly 20 numbers in your source file, it will simply sort how ever many
numbers are in the file.