bucket sort with values from txt file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ^cypis^ vel. SQ9JTI

    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

  • Andre Kostur

    #2
    Re: bucket sort with values from txt file

    "^cypis^ vel. SQ9JTI" <sq9jti@gmail.c omwrote in
    news:1168289772 .699280.293760@ v33g2000cwv.goo glegroups.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.

    Comment

    • ^cypis^ vel. SQ9JTI

      #3
      Re: bucket sort with values from txt file

      I have the part of code:

      #include "stdafx.h"

      void readData(int *numbers[20]){
      FILE *file;
      int i=0;
      file=fopen("dan e.txt","r");
      while(!feof(fil e)){
      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

      Comment

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

        #4
        Re: bucket sort with values from txt file

        On Jan 9, 8:00 am, "^cypis^ vel. SQ9JTI" <sq9...@gmail.c omwrote:
        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

        Comment

        • Andre Kostur

          #5
          Re: bucket sort with values from txt file

          "^cypis^ vel. SQ9JTI" <sq9jti@gmail.c omwrote in
          news:1168326036 .182421.85740@q 40g2000cwq.goog legroups.com:
          I have the part of code:
          >
          #include "stdafx.h"
          >
          void readData(int *numbers[20]){
          FILE *file;
          int i=0;
          file=fopen("dan e.txt","r");
          while(!feof(fil e)){
          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.

          Comment

          • ^cypis^ vel. SQ9JTI

            #6
            Re: bucket sort with values from txt file

            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(t able);
            for(i=0;i<many; i++)
            printf("%d", table[i]);

            bucketsort(tabl e, 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_ST DAFX_H__A9DB83D B_A9FD_)
            #define AFX_STDAFX_H__A 9DB83DB_A9FD_ // tu kazdy moze miec inna
            wartosc

            // Windows Header Files:
            #include

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

            #include

            #endif // !defined(AFX_ST DAFX_H__A9DB83D B_A9FD_)
            -------------------
            Is it ok? I have an error
            10:9 C:\Dev-Cpp\include\std afx.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

            Comment

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

              #7
              Re: bucket sort with values from txt file

              On Jan 10, 9:03 am, "^cypis^ vel. SQ9JTI" <sq9...@gmail.c omwrote:
              Hello again,
              I make this night thes code:
              >
              #include <iostream>
              #include <conio.h>
              #include <string.h>
              Make that #include <stringperhap s?
              #include "stdafx.h"
              //#include <stdio.h>
              [snip]
              Is it ok? I have an error
              10:9 C:\Dev-Cpp\include\std afx.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

              Comment

              • ^cypis^ vel. SQ9JTI

                #8
                Re: bucket sort with values from txt file

                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

                Comment

                Working...