Like Tree2Likes
  • 2 Post By iMalc

Problem with output after sorting *Help*

This is a discussion on Problem with output after sorting *Help* within the C Programming forums, part of the General Programming Boards category; recently i have wrote a program. But unfortunately the program doesn't run well and i have no idea what should ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    4

    Problem with output after sorting *Help*

    recently i have wrote a program. But unfortunately the program doesn't run well and i have no idea what should i do.....can any one so kind to help me? Thank!


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define N 7
    
    
    typedef struct Date
    {
        unsigned  day;
        unsigned  month;
        unsigned  year;
    }DATE;
    
    
    typedef struct Video
    {
    unsigned id;
    char title[90];
    char producer[60];
    DATE releaseDate;
    } VIDEO;
    
    
    VIDEO v[N]={};
    unsigned noOfVideo=0;
    
    
    void purgeKBBuffer() /* delete characters left in the keyboard buffer */
    {
     int dummychar;
     /* read and drop characters from the buffer
     until the closing return ('\n') is reached */
     do dummychar = getchar();
     while (dummychar != '\n');
     }
    
    
    char* inputString(char x[], int SIZE)
    {
     int i=-1;
    do
     {
     x[++i] =getchar();
     }
     while(x[i]!='\n'&&i<SIZE);
    
    
    x[i]='\0';
    return x;
    }
    
    
    DATE inputDate()
    {
     DATE d;
     printf("Enter Day:");
     scanf("%u", &d.day);
     purgeKBBuffer();
     printf("Enter Month:");
     scanf("%u", &d.month);
     printf("Enter Year:");
     scanf("%u", &d.year);
     putchar('\n');
    
    
    return d;
    }
    
    
    VIDEO inputVideo()
    {
    VIDEO v;
    printf("ID:");
    scanf("%u",&v.id);
    purgeKBBuffer();
    printf("Title:");
    inputString(v.title,90);
    printf("Producer:");
    inputString(v.producer,60);
    printf("The released date:\n");
    v.releaseDate=inputDate();
    return v;
    }
    
    
    void addVideo(VIDEO v[N])
    {
     if(noOfVideo<N)
     {
     v[noOfVideo]=inputVideo();
     noOfVideo++;
     }
    
    
    else
     {
     printf("The database can only store up to 7 videos and there is no space left!\n\n");
     }
    }
    
    
    void printVideo(VIDEO v)
    {
        printf("\nID:%u\tTitle:%s\t\tProducer:%s\tRelease Date:%u.%u.%u", v.id, v.title, v.producer, v.releaseDate.day, v.releaseDate.month, v.releaseDate.year);
        printf("\n");
    }
    
    
    void listVideos(VIDEO v[noOfVideo])
    {
    int i;
     for(i=0;i<noOfVideo;i++)
     {
     printVideo(v[i]);
     }
     }
    
    
    void sortById(VIDEO v[N])
    {
    VIDEO temp;
    int lower,Min,i;
    for (lower=0;lower<=N-2;lower++)
       {
         Min=lower;
         for (i=lower+1;i<N;i++)
            {
              if (v[i].id<v[Min].id)
                {
                  Min=i;
                }
            }
         temp=v[lower];
         v[lower]=v[Min];
         v[Min]=temp;
       }
    }
    
    
    void findAndPrintVideoByTitle(VIDEO v[N])
    {
    int i, j = 0;
    char title[90];
    printf("\tPlease enter title to search: ");
    inputString(title,90);
    putchar('\n');
    for(i = 0; i <= N-1; i++)
    {
    if (strcmp (v[i].title, title) == 0)
    {
    j = 1;
    printVideo(v[i]);
    }
    }
    if (j == 0)
    {
    printf("\tVideo not found!\n\n");
    }
    }
    
    
    char actionMenu()
    /* show the menu and return a valid selection of the menu */
    {
    char z;
    printf("\t\t\t**************************\n\n");
    printf("\t\t\t*     Video Database     *\n\n");
    printf("\t\t\t**************************\n\n");
    printf ("\t(a) Add a video.\n");
    printf ("\t(b) List all videos\n");
    printf ("\t(c) Sort videos by ID\n");
    printf ("\t(d) Search a video by title\n");
    printf ("\t(e) Quit\n\n\t");
    // get user input
    do z = (char) getchar();
    // continue while c not found in the string of menu chars
    while (strchr("abcde", z) == 0);
    printf("\n");
    purgeKBBuffer();
    return z;
    }
    
    
    
    
    int main()
    {
     VIDEO v[N];
     char actionMenu();
     char z;
     unsigned Count = 0;
    while (z != 'e')
    {
    z = actionMenu();
    
    
    switch (z)
    {
    case 'a': addVideo(v); // TODO add a new video
    break;
    case 'b': listVideos(v); // TODO list all videos
    break;
    case 'c': sortById(v); // TODO sort video by id
    break;
    case 'd': findAndPrintVideoByTitle(v); // TODO find video by title
    break;
    case 'e': printf("\tBye!\n\n"); //end program
    break;
    default: printf("invalid input\n");break;
    }
    }
    
    
    
    
    return 0;
    }

    in order to sort you need to enter "c" and to print out you need to type in "b" again. But that's the problem....

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    You need to indent your code.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by jc1992 View Post
    in order to sort you need to enter "c" and to print out you need to type in "b" again. But that's the problem....
    And why is this a problem?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    somehow it didnt show the data after sorting. it show 0 0 0 Name:  Capture.JPG
Views: 60
Size:  56.6 KB

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,692
    I'm pretty sure that your sort is just broken.

    Code:
    typedef bool (*CompFunc)(const VIDEO*, int, int);
    
    void swap(VIDEO* array, int min, int i)
    {
      VIDEO temp = array[i];
      array[i] = array[min];
      array[min] = temp;
    }
    
    bool idcomp(const VIDEO* array, int currMin, int k)
    {
      return array[k].id < array[currMin].id;
    }
    
    void selectionSort(VIDEO* array, int size, CompFunc comp)
    {
      int min, i, j;
      for (i = 0; i < size - 1; i++) {
        min = i;
        for (j = i+1; j < size; j++) {
          if (comp(array, min, j))
            min = j;
        }
        
        if (min != i) 
          swap(array, min, i);
      }
    }
    Try something like that.
    Last edited by whiteflags; 07-01-2013 at 12:35 AM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    The sort code is actually correct, and it is sorting perfectly. It's sorting all 7 items exactly as it's told to, and the three items with and ID of zero are correctly being moved to earlier in the array than those with higher IDs.

    So you only wanted to sort 4 items and not 7 huh?!
    Better tell it how many then by pass that in an another parameter.

    Line 109 is something I have never seen done before. Presumably that is some kind of VLA syntax. I recommend you don't do that, instead pass the array size in separately there as well.
    Salem and whiteflags like this.
    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"

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    Quote Originally Posted by whiteflags View Post

    Code:
    bool idcomp(const VIDEO* array, int currMin, int k)
    {
      return array[k].id < array[currMin].id;
    }
    .
    Thank by the way, What is the usage of this?

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,692
    Well separating the comparison from the algorithm like that helps you extend the usefulness of the algorithm.

    You pass it in like this in main:
    Code:
    selectionSort(array, n, compid);
    Inside the algorithm, compid will be called, under a different name. You can also create functions that compare differently, and use those the same way.

    More information can be found at The Function Pointer Tutorials.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Problem
    By GravyBoatCaptn in forum C++ Programming
    Replies: 1
    Last Post: 07-23-2010, 08:39 AM
  2. problem with sorting
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 10:06 AM
  3. sorting characters and output to text file
    By odb1 in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2003, 04:46 PM
  4. sorting output on student info program
    By indigo0086 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 10:29 AM
  5. Sorting array output problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 12:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21