Thread: Selection Sort

  1. #31
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    Quote Originally Posted by Andersonsacanno View Post
    fwrite sucks < fprintf rocks
    Not that I need to defend fwrite here, but you seem to misunderstand that fwrite and fprintf do two completely different things. fwrite writes the contents of a buffer in memory directly to a file stream, byte for byte. fprintf formats the output as text. Their uses don't overlap much. Generally speaking, fwrite is used for writing to a binary file, and fprintf is used for writing to a text file.

  2. #32
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    Thank you.


    Next up: Reading numbers from file

  3. #33
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    My friend and I have been able to read the numbers from the file, change them from ascii (char) into ints, then sort them using the same functions I used my original sort and fprintf to a file. I know this code isn't perfect or pretty, but it does take numbers from a file, sort, and then print them to a file.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void append(char* s, char c)
    {
            int len = strlen(s);
            s[len] = c;
            s[len+1] = '\0';
    }
    void swap (int *a, int *b) 
    {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
    }
    int sort(int a[], int x)
    {
    int i, j;
     for (i = -1; i < x-1; i++)
     {
      
     
      for(j=i; j >= 0; j--)
     
      { 
            printf(" if %d > %d \n",a[j], a[j+1]);
       if (a[j] > a[j+1])
       {
            printf("\n\nthen swap them\n");
       swap(&a[j], &a[j+1]);
       }
      }
     }
     
    }
    int main()
    {
        int a;
        int i = 0;
        int x = 100;
        int p = 100;
        int j = 0;
        int q = 100;
        int y = 14;
        int g = 0;
        int numbers[p];
       // memset(numbers, 0, sizeof(int), 100);
        int inner;
        int outer;
        char buffer[x];
        char array[p][q];
      // memset(array, 0, sizeof(array));
      
      for (i = 0; i < 100; i++)
      {
          numbers[i] = 0;
           
          }
          
        
      
        
      FILE *f;
      
     
      if (f = fopen("C:\\users\\hello\\desktop\\test.txt", "rt"))
      {
        fread(buffer, 1,  x, f);
        fclose(f);
        printf("\n%s", buffer);
        
      }
     for (i = 0; i < 100; i++)
     {
         if (buffer[i] >= 48 && buffer[i] <= 56)
         {
         
         append(array[j], buffer[i]);
         
         } 
         else if (buffer[i] == 44)
         {
              j++;
          }
         
          }
         
       
       for (i =0; i < 10; i++)
         {
             printf("\n Spot %d: \n %s \n\n", i, array[i]);
             }
            
             
             
       for(outer = 0; outer < 10; outer++)
    {
    for(inner = 0; inner < 10; inner++)
    {
           int digit = 1;
           
            
          if (array[outer][inner] == 48)
          {
                 digit = 0;       } 
                
          else if (array[outer][inner] == 49)
          { 
                 digit = 1;       } 
          else if (array[outer][inner] == 50)
          { 
                 digit = 2;       } 
          else if (array[outer][inner] == 51)
          { 
                 digit = 3;       } 
          else if (array[outer][inner] == 52)
          { 
                 digit = 4;       } 
          else if (array[outer][inner] == 53)
          { 
                 digit = 5;       } 
          else if (array[outer][inner] == 54)
          { 
                 digit = 6;       }
          else if (array[outer][inner] == 55)
          { 
                 digit = 7;       } 
          else if (array[outer][inner] == 56)
          {
                 digit = 8;       } 
          else if (array[outer][inner] == 57)
          { 
                 digit = 9;       } 
          else
          {break;}
          printf("\n\nThe current digit is %d, and the current value in numbers[outer] is %d",digit, numbers[outer]);
           numbers[outer] = numbers[outer]*10 + digit;
                 printf("\nin number %d, in digit %d, i found value %d",outer,inner, digit);
          
           
          
           }
           
         printf("the final integer is %d\n", numbers[outer]);
           }
    sort(numbers, length);
    printf("Final array is : \n");
    for(i = 0; i < 10; i++)
    {
    printf(" %d, ",numbers[i]);
    }
    printf("\n\n");
    FILE *fp;
    fp=fopen("c:\\users\\hello\\desktop\\test1.txt", "w");
    for (i = 0; i < 10; i++){
    fprintf (fp, "%d, ", numbers[i]);
    }       
             
    system("Pause");
      return 0;
    }

  4. #34
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You know that C has functions that do that for you, right?

    strtoi, strtol, strtod, etc.

    I'm curious why not convert the strings to numbers with fscanf() using a "%d" format specifier? Stick the numbers right in an array, sort it, and write it out.

  5. #35
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Lines 101 to 136 should be written as:
    Code:
    int digit;
    if (isdigit(array[outer][inner]))
        digit = array[outer][inner] - '0';
    else
        break;
    Copy and paste coding is evil. It would be very good for you to force yourself not to do it the braindead copy and paste way, but to instead look for, or think about the simple concise way.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #36
    Registered User Andersonsacanno's Avatar
    Join Date
    Oct 2012
    Posts
    25
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void append(char* start, char temp)
    {
     int len = strlen(start);
     start[len] = temp;
     start[len+1] = '\0';
    }
    int main()
    {
     int emptyIterator = 0;
     int arrayIterator = 0;
     int numberSize = 10;
     int fileSize;
     int x = 100;
     int numbers[x];
     // memset(numbers, 0, sizeof(int), 100);
     int inner;
     int outer;
     int worstArraysize;
     char arrayMalloc;
     char *buffer;
     int *array;
     int bufferSize;
     // memset(array, 0, sizeof(array));
     FILE *filePointer;
     for (emptyIterator = 0; emptyIterator < 100; emptyIterator++)
     {
      numbers[emptyIterator] = 0;
     }
     filePointer = fopen("C:\\users\\hello\\desktop\\test.txt", "rt");
     
     fseek(filePointer, 0, SEEK_END);
     fileSize = ftell(filePointer);
     rewind(filePointer);
     bufferSize = sizeof(char) * (fileSize + 1);
     buffer = malloc(bufferSize);
     memset(buffer, '\0', bufferSize);
     printf("\n%d filesize\n", fileSize);
     printf("\n%d buffersize\n", bufferSize);
     fread(buffer, 1, fileSize, filePointer);
     fclose(filePointer);
     worstArraysize = sizeof(char) * ((fileSize / 2) + 1 ); 
     array = malloc(worstArraysize);
     memset(array, '\0', worstArraysize);
     
     for(emptyIterator = 0; emptyIterator <= worstArraysize; emptyIterator++)
     {
      void * temp;
      temp = malloc(sizeof(char) * (numberSize));
      array[emptyIterator] = (int)temp;
      memset((char *) array[emptyIterator], '\0', numberSize);
      ((char *)array[emptyIterator])[0] = 's';
      printf("\ntemp is : %x array location %x  string num%d ram location %x:       %d\n", temp, &array[emptyIterator], emptyIterator, array[emptyIterator], (((char*)array[emptyIterator])[0]));
     }
     printf("Time to double check out work\n\n");
     for(emptyIterator = 0; emptyIterator <= worstArraysize; emptyIterator++)
     {
      printf("poisition %d , array ram : %x , string location %x, first char %c\n", emptyIterator, &array[emptyIterator], array[emptyIterator], *((char *)array[emptyIterator]));
     }
     return 0;
     system("Pause");
    }
    Needless to say the code has gotten pretty crazy.

    Here is output
    Code:
    poisition 0 , array ram : 660f88 , string location 660fa0, first char 0poisition 1 , array ram : 660f8c , string location 660fb8, first char Ąpoisition 2 , array ram : 660f90 , string location 660fd0, first char spoisition 3 , array ram : 660f94 , string location 660fe8, first char spoisition 4 , array ram : 660f98 , string location 661000, first char spoisition 5 , array ram : 660f9c , string location 661018, first char spoisition 6 , array ram : 660fa0 , string location 661030, first char spoisition 7 , array ram : 660fa4 , string location 661048, first char spoisition 8 , array ram : 660fa8 , string location 661060, first char spoisition 9 , array ram : 660fac , string location 661078, first char spoisition 10 , array ram : 660fb0 , string location 661090, first char spoisition 11 , array ram : 660fb4 , string location 6610a8, first char spoisition 12 , array ram : 660fb8 , string location 6610c0, first char spoisition 13 , array ram : 660fbc , string location 6610d8, first char spoisition 14 , array ram : 660fc0 , string location 6610f0, first char spoisition 15 , array ram : 660fc4 , string location 661108, first char spoisition 16 , array ram : 660fc8 , string location 661120, first char s
    This is still a major work in progress, I know a LOT has to be done to it still. But what I am mostly concerned with now is why the two loops are overlapping. I like all the different pointers and see this technique as being useful (don't want to hear how it isn't, i'm going through this with someone else)...I just want to know what the problem is here for why they are overlapping ram locations.
    Last edited by Andersonsacanno; 11-27-2012 at 06:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Selection Sort
    By Drew in forum C++ Programming
    Replies: 16
    Last Post: 09-18-2003, 07:19 AM
  2. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM
  3. Selection Sort
    By volk in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2003, 06:54 AM
  4. Selection Sort
    By Bleueyes515 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2002, 08:33 PM
  5. Selection-sort
    By Nutshell in forum C Programming
    Replies: 10
    Last Post: 01-11-2002, 04:32 AM