bucket sort with values from txt file

Discussion in 'C++' started by ^cypis^ vel. SQ9JTI, Jan 8, 2007.

  1. 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
    ^cypis^ vel. SQ9JTI, Jan 8, 2007
    #1
    1. Advertising

  2. ^cypis^ vel. SQ9JTI

    Andre Kostur Guest

    "^cypis^ vel. SQ9JTI" <> wrote in
    news::

    > 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.
    Andre Kostur, Jan 8, 2007
    #2
    1. Advertising

  3. 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
    ^cypis^ vel. SQ9JTI, Jan 9, 2007
    #3
  4. On Jan 9, 8:00 am, "^cypis^ vel. SQ9JTI" <> wrote:
    > 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
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 9, 2007
    #4
  5. ^cypis^ vel. SQ9JTI

    Andre Kostur Guest

    "^cypis^ vel. SQ9JTI" <> wrote in
    news::

    > 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.
    Andre Kostur, Jan 9, 2007
    #5
  6. 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
    ^cypis^ vel. SQ9JTI, Jan 10, 2007
    #6
  7. On Jan 10, 9:03 am, "^cypis^ vel. SQ9JTI" <> wrote:
    > Hello again,
    > I make this night thes code:
    >
    > #include <iostream>
    > #include <conio.h>
    > #include <string.h>


    Make that #include <string> perhaps?

    > #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
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 10, 2007
    #7
  8. 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
    ^cypis^ vel. SQ9JTI, Jan 10, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C-man
    Replies:
    6
    Views:
    529
    P.Hill
    Dec 6, 2003
  2. Sameen
    Replies:
    2
    Views:
    440
    Victor Bazarov
    Aug 29, 2005
  3. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,547
    Roedy Green
    Sep 15, 2011
  4. Navin
    Replies:
    1
    Views:
    690
    Ken Schaefer
    Sep 9, 2003
  5. $_@_.%

    Bit Bucket?

    $_@_.%, Jan 9, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    94
    Tore Aursand
    Jan 10, 2004
Loading...

Share This Page