bucket sort with values from txt file

  • Thread starter ^cypis^ vel. SQ9JTI
  • Start date
C

^cypis^ vel. SQ9JTI

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
 
A

Andre Kostur

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.
 
C

^cypis^ vel. SQ9JTI

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++;
}
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);
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);
printf("\n\n");

return 0;
}


What should i put in the place of: "WHAT TO DO HERE???:)"
Thanks in advance.
Regards.
Luke
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 
A

Andre Kostur

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++;
}
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);
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);
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.
 
C

^cypis^ vel. SQ9JTI

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++;

}

return i;
}

int MAX=8;
int bucket[8];

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

i=0;
while(i<MAX) {
bucket = 0;
i=i+1;
}


i=0;
while(i<n) {
bucket[numbers] = bucket[numbers] + 1;
i=i+1;
}


m=0;
i=0;
while(i<MAX) {
while(bucket>0) {
numbers[m] = i;
m=m+1;
bucket = bucket-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);

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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hello again,
I make this night thes code:

#include <iostream>
#include <conio.h>
#include <string.h>

Make that #include said:
#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?
 
C

^cypis^ vel. SQ9JTI

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top