I'd think if you're going to simulate a disk drive, the inputs should simply be disk locations of reads/writes, and possibly an initial head location (with some sensible default, such as track 0). This particular loop also seems to be asking for data that it seems like it has essentially no business asking for in the first place. For example: current_direction:Įven in C, you'd normally prefer a do/ while loop to a goto for this: do while (direction != 0 & direction != 1)
#Look scheduling algorithm code
Contrary to Morwenn's comments, some parts of your code look more like ancient Fortran or BASIC rather than C. Morwenn has already given feedback I'd consider quite good, so I'll restrict myself to areas he hasn't already covered.įirst, you have some code I think could be structured quite a bit more nicely. Overall, you should really consider looking at the C++ standard library and using its classes and algorithms to replace many things in your code. There is a typo in recieve_request: it should be receive_request.if(direction=1) is probably a bug, it should be if(direction=1), using proper compiler flags ( -Wall, -Wextra, etc.) should tell you when you use an assignment instead of a comparison in if.Such user interaction should not appear at construction, but be handled later by the client code of the class. I'm not at ease with using std::cin in the constructor of disk.Therefore, your functions CSCAN and LOOK (and the other ones) should be named cscan and look instead. Upper case names should only be used for preprocessor macros.I can't cover all your code because it would have to be refactored to use more standard library features beforehand, but here are some notes: You can replace this line: qsort(sorted_request+1,number_of_request,sizeof(int),compare) īy this one: std::sort(std::begin(sorted_request), std::end(sorted_request)) It should be at least as fast (if not faster) and easier to read and write. Instead of the old qsort, which requires to take a function pointer with void* pointers and do some ugly casts, you should use std::sort in the standard header. For example, a fixed sized array int arr can be replaced by the equivalent std::array in C++11 (this container is not directly available in the older versions of the standard). Instead of using old C arrays and pointers, you can use standard library containers. That helps to check whether some header has already been included or not, and to avoid including twice some header. Also, it's good practice to organize your include directives in alphabetical ordering.For input/output operations, you should generally use and stick with it. is a C header, in C++, you should include instead, which is its C++ equivalent.You should just add std:: before every function/calss/variable from the standard library. Wirting using namespace std is generally considered bad practice, especially when written in the global namesapce: it leads to potential name clashes and namespace pollution. There are numerous ways to improve your code readability by replacing C standard features by C++ ones. You are using C++, but overall, your code looks like C. Head_movement+=abs(sorted_request-sorted_request) Head_movement+=sorted_request-sorted_request Head_movement+=abs(request-request) Įlse if((request-request)>(request-request)) If(request>max||requestsorted_request&flag=0) Int compare (const void * a, const void * b) Aim is to provide total head movement in different disk scheduling algorithms namely FCFS, SSTF, LOOK, C-LOOK, SCAN, C-SCAN.