By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,440 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,440 IT Pros & Developers. It's quick & easy.

bucket sort with values from txt file

P: n/a
Hi,
i need your help.
I have to prepare a homework, easy program, which will be sorting the
values from txt file and writing the solution to another txt file.
It has to be a bucket sort. Have anyone a source code for this sample?
Many thanks in advance!
Regards,
Luke

Jan 8 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"^cypis^ vel. SQ9JTI" <sq****@gmail.comwrote in
news:11**********************@v33g2000cwv.googlegr oups.com:
Hi,
i need your help.
I have to prepare a homework, easy program, which will be sorting the
values from txt file and writing the solution to another txt file.
It has to be a bucket sort. Have anyone a source code for this sample?
Many thanks in advance!
See section 5.2 of the FAQ (http://parashift.com/c++-faq-lite/index.html).

Show us that you have actually made an attempt to solve your own problem,
and ask specific C++ questions, and you'll get an answer.
Jan 8 '07 #2

P: n/a
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???:)

}

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???:)"
Thanks in advance.
Regards.
Luke

Jan 9 '07 #3

P: n/a
On Jan 9, 8:00 am, "^cypis^ vel. SQ9JTI" <sq9...@gmail.comwrote:
What should i put in the place of: "WHAT TO DO HERE???:)"
Google is your friend, for most algorithms you can find code or
pseudo-code with a quick search. For most common algorithms you can
find it on Wikipedia.

--
Erik Wikström

Jan 9 '07 #4

P: n/a
"^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.
Jan 9 '07 #5

P: n/a
Hello again,
I make this night thes code:

#include <iostream>
#include <conio.h>
#include <string.h>
#include "stdafx.h"
//#include <stdio.h>
//using namespace std;
int readData(int numbers[10])
{
FILE *pl;
int i=0;
pl=fopen("from.txt","r");
if(pl==NULL)
printf("File wasn't opened");
while(!feof(pl))
{
fscanf(pl,"%d", numbers[i]);
i++;

}

return i;
}

int MAX=8;
int bucket[8];

int bucketsort(int numbers[10], int n)
{
int i, m;

i=0;
while(i<MAX) {
bucket[i] = 0;
i=i+1;
}
i=0;
while(i<n) {
bucket[numbers[i]] = bucket[numbers[i]] + 1;
i=i+1;
}
m=0;
i=0;
while(i<MAX) {
while(bucket[i]>0) {
numbers[m] = i;
m=m+1;
bucket[i] = bucket[i]-1;
}
i=i+1;
}
return 0;}
int main(int argc, int* argv[])
{

int i,many;
int table[10];
many=readData(table);
for(i=0;i<many;i++)
printf("%d", table[i]);

bucketsort(table, many);

return 0;
}
-------------------------
And now i have a problem with #include "stdafx.h". I found the source
of file and prepared it (it wasn't able with compiler) and it looks so:

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_ // tu kazdy moze miec inna
wartosc

// Windows Header Files:
#include

// C RunTime Header Files
#include
#include
#include

#include

#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_)
-------------------
Is it ok? I have an error
10:9 C:\Dev-Cpp\include\stdafx.h #include expects "FILENAME" or
<FILENAME>
the same 13:9, 14:9, 15:9, 17,9 and 19:50.
What should I do?
Regards,
Luke

Jan 10 '07 #6

P: n/a
On Jan 10, 9:03 am, "^cypis^ vel. SQ9JTI" <sq9...@gmail.comwrote:
Hello again,
I make this night thes code:

#include <iostream>
#include <conio.h>
#include <string.h>
Make that #include <stringperhaps?
#include "stdafx.h"
//#include <stdio.h>
[snip]
Is it ok? I have an error
10:9 C:\Dev-Cpp\include\stdafx.h #include expects "FILENAME" or
<FILENAME>
the same 13:9, 14:9, 15:9, 17,9 and 19:50.
What should I do?
Well, I have not read all your code so I'm not sure what you do or how,
but do you really need the stdafx.h-file included?

--
Erik Wikström

Jan 10 '07 #7

P: n/a
Hi,
...
Well, I have not read all your code so I'm not sure what you do or how,
but do you really need the stdafx.h-file included?
yes, it isn't needed. But now, if I compile and want to run the
program, windows (XP Pro) find some errors and it's impossible to check
is everything ok. Could anyuone check it an own desktop?
Luke

Jan 10 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.