Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <dos.h>
- #define max 60
- #define g gotoxy
- #define p printf
- #define s scanf
- int r, ratings[max]={0};
- void menu();
- void input();
- void rating();
- void mean();
- void median();
- void sort_use();
- void selection_sort();
- void insertion_sort();
- void binary_search();
- void exit();
- void choose_again();
- void border();
- void main ()
- {
- clrscr();
- border();
- menu();
- getch();
- }
- //this function contains the choices of the menu
- void menu()
- {
- int choice;
- p ("\t\t\t\t\tMENU:\n");
- p ("\t\t\t1. Input Ratings\n");
- p ("\t\t\t2. Mean\n");
- p ("\t\t\t3. Median\n");
- p ("\t\t\t4. Output Ratings, Mean & Median\n");
- p ("\t\t\t5. Binary Search\n");
- p ("\t\t\t6. Exit\n");
- p ("\t\t\tAnswer: ");
- s ("%d",&choice);
- switch(choice)
- {
- case 1: input();
- choose_again();
- break;
- case 2: mean();
- choose_again();
- break;
- case 3: sort_use();
- median();
- choose_again();
- break;
- case 4: rating();
- mean();
- median();
- choose_again();
- break;
- case 5: binary_search();
- choose_again();
- break;
- case 6: exit();
- break;
- }
- }
- //this function is for the input of ratings, Option 1
- void input()
- {
- clrscr();
- p ("How many ratings would you like? ");
- s ("%d",&r);
- if (r > 60)
- {
- p ("Sorry, this program will only accomodate a maximum of 60 ratings.\n");
- choose_again();
- }
- else
- p ("Please Input %d ratings: ", r);
- for (int i=0; i<r; i++)
- s ("%d",&ratings[i]);
- }
- //this function if for displaying the ratings entered by the user
- void rating()
- {
- for (int i=0; i<r; i++)
- p ("\nRating %d: %d", i+1, ratings[i]);
- }
- //this function is for getting the mean of the ratings, Option 2
- void mean()
- {
- float sum=0, mean;
- for (int i=0; i<r; i++)
- sum = sum + ratings[i];
- mean = sum / r;
- p ("\nThe mean is: %.2f.\n", mean);
- }
- //this function is for the user to choose what type of sort will be used, Option 3
- void sort_use()
- {
- char sort, i, I;
- p ("\nWhat kind of sorting would you prefer? ");
- p ("\n(I is for Insertion sorting and S is for Selection sorting) ");
- s (" %c", &sort);
- if (sort=='I' || sort == 'i')
- insertion_sort();
- else
- selection_sort();
- }
- //this function is for insertion sort
- void insertion_sort()
- {
- int l, d, temp;
- for (l=1; l<=r-1; l++)
- {
- d = l;
- while ( d>0 && ratings[d-1] > ratings[d])
- {
- temp = ratings[d];
- ratings[d] = ratings[d-1];
- ratings[d-1] = temp;
- d--;
- }
- }
- p ("Sorted list in ascending order:\n");
- for (l=0; l<=r-1; l++)
- p ("%d\t", ratings[l]);
- }
- //this function is for selection sort
- void selection_sort()
- {
- int i, j, loc, temp=0, min;
- for (i=0; i<r-1; i++)
- {
- min=ratings[i];
- loc=i;
- for (j=i+1; j<r; j++)
- {
- if (min>ratings[j])
- {
- min=ratings[j];
- loc=j;
- }
- }
- temp=ratings[i];
- ratings[i]=ratings[loc];
- ratings[loc]=temp;
- }
- p ("Sorted list is as follows:\n");
- for (i=0; i<r; i++)
- p ("%d\t", ratings[i]);
- }
- //this function is for getting the median of the ratings entered by the user, Option 3
- void median()
- {
- float median=0;
- median= ( ratings[0] + ratings[r-1] ) / 2;
- p ("\nThe median is %.2f.\n", median);
- // gotoxy() c
- // printf("\nThe formula for solving the median is the lowest and highest array to be added then divided by 2");
- }
- //this function is for binary search, Option 5
- void binary_search()
- {
- int search, first, last, middle;
- p ("Enter element to find: ");
- s ("%d", &search);
- first=0;
- last=r-1;
- middle=(first+last)/2;
- while (first<=last)
- {
- if (ratings[middle]<search)
- first=middle+1;
- else if (ratings[middle]==search)
- {
- p ("%d found at location %d and array %d.\n", search, middle+1, middle);
- break;
- }
- else last=middle-1;
- middle=(first+last)/2;
- }
- if (first>last)
- p ("%d is not present on the list.\n", search);
- }
- //this function allows the user to choose another option from the menu again
- void choose_again()
- {
- char sagot;
- char Y, y;
- p ("Would you like to choose from our menu again? ");
- s (" %c", &sagot);
- if (sagot == 'y' || sagot == 'Y')
- {
- clrscr();
- menu();
- }
- else exit();
- }
- //this function exits the program
- void exit()
- {
- clrscr();
- p ("Thank you and have a good day!\n");
- p ("\t Bronola & Cuyong");
- }
- void border()
- {
- int top, left, right, bottom;
- clrscr();
- /*top*/ for (top=1; top<=78; top++)
- {
- g (top, 1);
- cprintf ("%c", 219);
- //delay (10);
- }
- /*left*/ for (left=1; left<=25; left++)
- {
- g (1, left);
- cprintf ("%c", 219);
- //delay (10);
- }
- /*bottom*/ for (bottom=25; bottom<=101; bottom++)
- {
- g (bottom, 40);
- cprintf ("%c", 219);
- //delay (10);
- }
- /*right*/ for (right=25; right>=1; right--)
- {
- g (79, right);
- cprintf ("%c", 219);
- //delay (10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement