Skip to content

Commit

Permalink
Considered -Wconversion-extra compilation flag. Synced other files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramy-Badr-Ahmed committed Sep 27, 2024
1 parent 10f3103 commit 4602fd7
Show file tree
Hide file tree
Showing 28 changed files with 178 additions and 184 deletions.
4 changes: 2 additions & 2 deletions examples/maths/factorial.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ program factorial_program
use factorial_module
implicit none

Print*, factorial(5)
Print*, recursive_factorial(5)
Print *, factorial(5)
Print *, recursive_factorial(5)

end program factorial_program
2 changes: 1 addition & 1 deletion examples/maths/numerical_integration/monte_carlo.f90
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ program example_monte_carlo
! Set the integration limits and number of random samples
a = -1.0_dp
b = 1.0_dp
n = 1E6 !! Number of random samples
n = 1000000 !! 1E6 Number of random samples

! Call Monte Carlo integration
call monte_carlo(integral_result, error_estimate, a, b, n, func)
Expand Down
3 changes: 2 additions & 1 deletion examples/maths/numerical_integration/trapezoid.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
!> Example Program for Trapezoidal Rule
!!
!! This program demonstrates the use of the Trapezoidal rule for numerical integration.
!!
!! It sets the integration limits and number of panels, and calls the
Expand All @@ -17,7 +18,7 @@ program example_tapezoid
! Set the integration limits and number of panels
a = -1.0_dp
b = 1.0_dp
n = 1E6 !! Number of subdivisions
n = 1000000 !! 1E6 Number of subdivisions

! Call the trapezoidal rule with the function passed as an argument
call trapezoid(integral_result, a, b, n, func)
Expand Down
6 changes: 3 additions & 3 deletions examples/searches/example_linear_search.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ program linear_search_program

integer, dimension(5) :: array

array = (/ 540, 6, 10, 100, 3 /)
array = (/540, 6, 10, 100, 3/)

!! Search for the number 6 in array.
print*, "Target = 6: ", linear_search(array, 6) !! Prints 2.
print *, "Target = 6: ", linear_search(array, 6) !! Prints 2.

!! Search for the number 5 in array.
print*, "Target = 5: ", linear_search(array, 5) !! Prints -1 because item 5 is not found.
print *, "Target = 5: ", linear_search(array, 5) !! Prints -1 because item 5 is not found.

end program linear_search_program
6 changes: 3 additions & 3 deletions examples/searches/recursive_linear_search.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ program recursive_linear_search_example

integer, dimension(5) :: array

array = (/ 306, 1005, 5, 62, 0 /)
array = (/306, 1005, 5, 62, 0/)

!! Search for the number 62 in the array
print*, "Target = 62: ", recursive_linear_search(array, size(array), 62) !! Prints 4.
print *, "Target = 62: ", recursive_linear_search(array, size(array), 62) !! Prints 4.

!! Search for the number 10 in the array
print*, "Target = 10: ", recursive_linear_search(array, size(array), 10) !! Prints -1 because item 10 is not found.
print *, "Target = 10: ", recursive_linear_search(array, size(array), 10) !! Prints -1 because item 10 is not found.

end program recursive_linear_search_example
4 changes: 2 additions & 2 deletions examples/sorts/example_recursive_bubble_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ program recursive_bubble_sort_example
!! Fill the array with random numbers.
call random_number(array)

print*, "Before:", array
print *, "Before:", array

!! Bubble sort subroutine call.
call recursive_bubble_sort(array, size(array))

print*, "After:", array
print *, "After:", array

end program recursive_bubble_sort_example
4 changes: 2 additions & 2 deletions examples/sorts/example_usage_bubble_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ program bubble_sort_example
!! Fill the array with random numbers
call random_number(array)

print*, "Before:", array
print *, "Before:", array

!! Call the bubble_sort subroutine to sort the array
call bubble_sort(array)

print*, "After:", array
print *, "After:", array

end program bubble_sort_example
2 changes: 1 addition & 1 deletion examples/sorts/example_usage_gnome_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ program test_gnome_sort
integer :: n, i

! Initialize the test array
array = (/ -5, 2, 9, 1, 5, 6, -7, 8, 15, -20 /)
array = (/-5, 2, 9, 1, 5, 6, -7, 8, 15, -20/)
n = size(array)

! Call gnome_sort from the module to sort the array
Expand Down
2 changes: 1 addition & 1 deletion examples/sorts/example_usage_heap_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ program test_heap_sort
integer, dimension(n) :: array(n) ! Test array

! Initialize the test array
array = (/ 12, 11, 13, 5, 6, 7, 3, 9, -1, 2, -12, 1 /)
array = (/12, 11, 13, 5, 6, 7, 3, 9, -1, 2, -12, 1/)

! Print the original array
print *, "Original array:"
Expand Down
2 changes: 1 addition & 1 deletion examples/sorts/example_usage_merge_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ program test_merge_sort
integer :: n, i

! Initialize the test array
array = (/ -2, 3, -10, 11, 99, 100000, 100, -200 /)
array = (/-2, 3, -10, 11, 99, 100000, 100, -200/)
n = size(array)

! Call merge_sort from the module to sort the array
Expand Down
2 changes: 1 addition & 1 deletion examples/sorts/example_usage_quick_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ program test_quick_sort
integer :: n, i

! Initialize the test array
array = (/ 10, 7, 8, 9, 1, 5, -2, 12, 0, -5 /)
array = (/10, 7, 8, 9, 1, 5, -2, 12, 0, -5/)
n = size(array)

! Print the original array
Expand Down
6 changes: 3 additions & 3 deletions examples/sorts/example_usage_radix_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ program test_radix_sort

! Test for base 10
print *, "Testing Radix Sort with base 10:"
array = (/ 170, 45, 75, 90, 802, 24, 2, 66, 15, 40 /)
array = (/170, 45, 75, 90, 802, 24, 2, 66, 15, 40/)
n = size(array)
call radix_sort(array, n, base10)
print *, "Sorted array in base 10:"
Expand All @@ -23,7 +23,7 @@ program test_radix_sort

! Test for base 2
print *, "Testing Radix Sort with base 2:"
array = (/ 1010, 1101, 1001, 1110, 0010, 0101, 1111, 0110, 1000, 0001 /) ! Binary values whose decimal: (/ 10, 13, 9, 14, 2, 5, 15, 6, 8, 1 /)
array = (/1010, 1101, 1001, 1110, 0010, 0101, 1111, 0110, 1000, 0001/) ! Binary values whose decimal: (/ 10, 13, 9, 14, 2, 5, 15, 6, 8, 1 /)
n = size(array)
call radix_sort(array, n, base2)
print *, "Sorted binary array in Decimal:"
Expand All @@ -33,7 +33,7 @@ program test_radix_sort

! Test for base 16
print *, "Testing Radix Sort with base 16:"
array = (/ 171, 31, 61, 255, 16, 5, 211, 42, 180, 0 /) ! Hexadecimal values as decimal
array = (/171, 31, 61, 255, 16, 5, 211, 42, 180, 0/) ! Hexadecimal values as decimal
n = size(array)
call radix_sort(array, n, base16)
print *, "Sorted hexadecimal array in Decimal:"
Expand Down
4 changes: 2 additions & 2 deletions modules/maths/factorial.f90
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function factorial(number) result(factorial_number)

factorial_number = 1
do while (counter > 1)
factorial_number = factorial_number * counter
factorial_number = factorial_number*counter
counter = counter - 1
end do

Expand All @@ -33,7 +33,7 @@ recursive function recursive_factorial(number) result(factorial_number)
if (number .lt. 1) then
factorial_number = 1
else
factorial_number = number * recursive_factorial(number - 1)
factorial_number = number*recursive_factorial(number - 1)
end if

end function recursive_factorial
Expand Down
4 changes: 2 additions & 2 deletions modules/maths/numerical_integration/midpoint.f90
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ end function func
end interface

! Step size
h = (b - a)/(1.0_dp*n)
h = (b - a)/real(n, dp)

! Allocate array for midpoints
allocate (x(1:n), fx(1:n))

! Calculate midpoints
x = [(a + (i - 0.5_dp)*h, i=1, n)]
x = [(a + (real(i, dp) - 0.5_dp)*h, i=1, n)]

! Apply function to each midpoint
do i = 1, n
Expand Down
4 changes: 3 additions & 1 deletion modules/maths/numerical_integration/monte_carlo.f90
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module monte_carlo_integration
subroutine monte_carlo(integral_result, error_estimate, a, b, n, func)
implicit none
integer, intent(in) :: n
real(dp) :: n_dp !! Hold n as double precision
real(dp), intent(in) :: a, b
real(dp), intent(out) :: integral_result, error_estimate

Expand Down Expand Up @@ -64,7 +65,8 @@ end function func
integral_result = (b - a)*(sum_fx/real(n, dp))

! Estimate the error using the variance of the function values
error_estimate = sqrt((sum_fx_squared/n - (sum_fx/n)**2)/(n - 1))*(b - a)
n_dp = real(n, dp)
error_estimate = sqrt((sum_fx_squared/n_dp - (sum_fx/n_dp)**2)/(n_dp - 1))*(b - a)

! Deallocate arrays
deallocate (uniform_sample, fx)
Expand Down
4 changes: 2 additions & 2 deletions modules/maths/numerical_integration/simpson.f90
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ end function func
end if

! Step size
h = (b - a)/(1.0_dp*n)
h = (b - a)/real(n, dp)

! Allocate arrays
allocate (x(0:n), fx(0:n))

! Create an array of x values, contains the endpoints and the midpoints.
x = [(a + i*h, i=0, n)]
x = [(a + (real(i, dp))*h, i=0, n)]

! Apply the function to each x value
do i = 0, n
Expand Down
4 changes: 2 additions & 2 deletions modules/maths/numerical_integration/trapezoid.f90
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ end function func
end interface

! Step size
h = (b - a)/(1.0_dp*n)
h = (b - a)/real(n, dp)

! Allocate arrays
allocate (x(0:n), fx(0:n))

! Create an array of x values
x = [(a + i*h, i=0, n)]
x = [(a + (real(i, dp))*h, i=0, n)]

! Apply the function to each x value
do i = 0, n
Expand Down
2 changes: 1 addition & 1 deletion modules/searches/linear_search.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module linear_search_module

!! This function searches for a target in a given collection.
!! Returns the index of the found target or -1 if target is not found.
function linear_search (collection, target) result(target_index)
function linear_search(collection, target) result(target_index)
integer, dimension(:), intent(in) :: collection !! A collection for elements of type integer
integer, intent(in) :: target !! Target value to be searched.
integer :: target_index !! Target's index in the collection to return.
Expand Down
4 changes: 2 additions & 2 deletions modules/searches/recursive_linear_search.f90
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ recursive function recursive_linear_search(collection, index, target) result(tar
target_index = index
else
!! Recursively search in the remaining part of the collection
target_index = recursive_linear_search(collection, index-1, target)
target_index = recursive_linear_search(collection, index - 1, target)
end if

end function recursive_linear_search

end module recursive_linear_search_module
end module recursive_linear_search_module
8 changes: 4 additions & 4 deletions modules/sorts/bubble_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module bubble_sort_module
contains

!! This subroutine sorts the collection using bubble sort.
subroutine bubble_sort (collection)
subroutine bubble_sort(collection)
real, dimension(:), intent(inout) :: collection !! A collection of real numbers to be sorted

integer :: i, j, collection_size
Expand All @@ -24,11 +24,11 @@ subroutine bubble_sort (collection)
swapped = .false.

do i = 1, j
if (collection(i) .gt. collection(i+1)) then
if (collection(i) .gt. collection(i + 1)) then
!! Swap values if they are out of order in [i, i+1] region
temp = collection(i)
collection(i) = collection(i+1)
collection(i+1) = temp
collection(i) = collection(i + 1)
collection(i + 1) = temp

swapped = .true. !! Set swapped flag to true
end if
Expand Down
8 changes: 4 additions & 4 deletions modules/sorts/heap_sort.f90
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ subroutine heap_sort(array, n)
integer :: i

! Build the max heap
do i = n / 2, 1, -1
do i = n/2, 1, -1
call heapify(array, n, i)
end do

Expand All @@ -50,8 +50,8 @@ recursive subroutine heapify(array, n, i)
integer :: largest, left, right

largest = i
left = 2 * i
right = 2 * i + 1
left = 2*i
right = 2*i + 1

! Is Left Child is larger than Root?
if (left <= n .and. array(left) > array(largest)) then
Expand Down Expand Up @@ -84,4 +84,4 @@ subroutine swap(array, i, j)

end subroutine swap

end module heap_sort_module
end module heap_sort_module
14 changes: 7 additions & 7 deletions modules/sorts/merge_sort.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
!> Merge Sort Algorithm

!!
!> This module implements the Merge Sort algorithm.
!!
!! Merge Sort is a divide-and-conquer algorithm. It divides the input array into two halves, recursively sorts them,
Expand All @@ -14,7 +14,7 @@
!! - A sorted array of integers.
!!
module merge_sort_module
implicit none
implicit none

contains

Expand All @@ -23,21 +23,21 @@ recursive subroutine merge_sort(array, n)
implicit none
integer, dimension(:), intent(inout) :: array ! Input/output array to be sorted
integer, intent(in) :: n ! Size of the array
integer :: middle, i
integer :: middle
integer, dimension(:), allocatable :: left_half, right_half, sorted_array

! Base case: return if the array has 1 or fewer elements
if (n <= 1) return

! Calculate the middle point to split the array
middle = n / 2
middle = n/2

! Allocate space for the two halves
allocate(left_half(middle), right_half(n - middle), sorted_array(n))
allocate (left_half(middle), right_half(n - middle), sorted_array(n))

! Split array into two halves
left_half = array(1:middle)
right_half = array(middle+1:n)
right_half = array(middle + 1:n)

! Recursively sort each half
call merge_sort(left_half, middle)
Expand All @@ -50,7 +50,7 @@ recursive subroutine merge_sort(array, n)
array = sorted_array

! Deallocate the temporary arrays
deallocate(left_half, right_half, sorted_array)
deallocate (left_half, right_half, sorted_array)

end subroutine merge_sort

Expand Down
Loading

0 comments on commit 4602fd7

Please sign in to comment.