473,394 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

bucket sort with values from txt file

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
7 5036
"^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
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
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
"^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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID),...
2
by: New Guy | last post by:
Hello, First, I want to apologize for posting this message to multiple groups. (1) I have a hard time finding any info and (2) this is not a school assignment. I am trying to create in my...
3
by: News | last post by:
Is it possible to delete a file by copying it to the "bit bucket" or "null device"? Back in my youth when I live in VMS-land you could delete a file by copying it to NL: ========== I have...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
1
by: jimyg10 | last post by:
I want to implement Bucket Sort in C using Posix Threads.... I've done the sequential algorithm. How to make the parallel one?? Anybody can help me??
4
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be...
3
by: Eric Lilja | last post by:
Hello, consider the following assignment and my code for it: /* Write a program that does the following: * Integer numbers shall be read from a textfile and stored in a std::vector. The name...
4
DeMan
by: DeMan | last post by:
I know I'll cop flak for these but......... What's RED and looks like a bucket? ----------------------------------------------> A Red Bucket What's BLUE and looks like a bucket? ...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.