mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-09 05:29:13 +08:00
feat: Add ncr mod p code (#1325)
* feat: Add ncr mod p code (#1323) * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Added all functions inside a class + added more asserts * updating DIRECTORY.md * clang-format and clang-tidy fixes forf6df24a5* Replace int64_t to uint64_t + add namespace + detailed documentation * clang-format and clang-tidy fixes fore09a0579* Add extra namespace + add const& in function arguments * clang-format and clang-tidy fixes for8111f881* Update ncr_modulo_p.cpp * clang-format and clang-tidy fixes for2ad2f721* Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes for5b69ba5c* updating DIRECTORY.md * clang-format and clang-tidy fixes fora8401d4bCo-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -3,25 +3,27 @@
|
||||
* @todo Add documentation
|
||||
* @warning The sorting algorithm is erroneous
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
|
||||
struct list {
|
||||
std::array<int, 50> data{};
|
||||
int top = 0;
|
||||
bool isSorted = false;
|
||||
|
||||
int BinarySearch(const std::array<int, 50>& dataArr, int first, int last, int x) {
|
||||
int BinarySearch(const std::array<int, 50>& dataArr, int first, int last,
|
||||
int x) {
|
||||
if (last < first) {
|
||||
return -1;
|
||||
}
|
||||
int mid = (first + last) / 2;
|
||||
if (dataArr[mid] == x)
|
||||
if (dataArr[mid] == x) {
|
||||
return mid;
|
||||
else if (x < dataArr[mid])
|
||||
} else if (x < dataArr[mid]) {
|
||||
return (BinarySearch(dataArr, first, mid - 1, x));
|
||||
else if (x > dataArr[mid])
|
||||
} else if (x > dataArr[mid]) {
|
||||
return (BinarySearch(dataArr, mid + 1, last, x));
|
||||
}
|
||||
|
||||
std::cerr << __func__ << ":" << __LINE__ << ": Undefined condition\n";
|
||||
return -1;
|
||||
@@ -38,7 +40,7 @@ struct list {
|
||||
}
|
||||
|
||||
int Search(int x) {
|
||||
int pos;
|
||||
int pos = 0;
|
||||
|
||||
if (isSorted) {
|
||||
pos = BinarySearch(data, 0, top - 1, x);
|
||||
@@ -55,7 +57,7 @@ struct list {
|
||||
}
|
||||
|
||||
void Sort() {
|
||||
int i, j, pos=0;
|
||||
int i = 0, j = 0, pos = 0;
|
||||
for (i = 0; i < top; i++) {
|
||||
int min = data[i];
|
||||
for (j = i + 1; j < top; j++) {
|
||||
@@ -119,8 +121,8 @@ struct list {
|
||||
|
||||
int main() {
|
||||
list L;
|
||||
int choice;
|
||||
int x;
|
||||
int choice = 0;
|
||||
int x = 0;
|
||||
do {
|
||||
// Choices for operations on the list_array.
|
||||
std::cout << "\n0.Exit";
|
||||
|
||||
@@ -7,19 +7,20 @@
|
||||
* values, which can be added to the end line (enqueue), removed from
|
||||
* head of line (dequeue) and displayed.
|
||||
* ### Algorithm
|
||||
* Values can be added by increasing the `rear` variable by 1 (which points to
|
||||
* the end of the array), then assigning new value to `rear`'s element of the array.
|
||||
*
|
||||
* Values can be removed by increasing the `front` variable by 1 (which points to
|
||||
* the first of the array), so it cannot reached any more.
|
||||
*
|
||||
* Values can be added by increasing the `rear` variable by 1 (which points to
|
||||
* the end of the array), then assigning new value to `rear`'s element of the
|
||||
* array.
|
||||
*
|
||||
* Values can be removed by increasing the `front` variable by 1 (which points
|
||||
* to the first of the array), so it cannot reached any more.
|
||||
*
|
||||
* @author [Pooja](https://github.com/pooja-git11)
|
||||
* @author [Farbod Ahmadian](https://github.com/farbodahm)
|
||||
*/
|
||||
#include <iostream> /// for io operations
|
||||
#include <array> /// for std::array
|
||||
#include <array> /// for std::array
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
constexpr uint16_t max_size{10}; ///< Maximum size of the queue
|
||||
constexpr uint16_t max_size{10}; ///< Maximum size of the queue
|
||||
|
||||
/**
|
||||
* @namespace data_structures
|
||||
@@ -30,29 +31,31 @@ namespace data_structures {
|
||||
/**
|
||||
* @namespace queue_using_array
|
||||
* @brief Functions for [Queue using Array]
|
||||
* (https://www.geeksforgeeks.org/array-implementation-of-queue-simple/) implementation.
|
||||
* (https://www.geeksforgeeks.org/array-implementation-of-queue-simple/)
|
||||
* implementation.
|
||||
*/
|
||||
namespace queue_using_array {
|
||||
|
||||
/**
|
||||
* @brief Queue_Array class containing the main data and also index of head and tail of the array.
|
||||
* @brief Queue_Array class containing the main data and also index of head and
|
||||
* tail of the array.
|
||||
*/
|
||||
class Queue_Array {
|
||||
public:
|
||||
public:
|
||||
void enqueue(const int16_t&); ///< Add element to the first of the queue
|
||||
int dequeue(); ///< Delete element from back of the queue
|
||||
void display() const; ///< Show all saved data
|
||||
private:
|
||||
int8_t front{-1}; ///< Index of head of the array
|
||||
int8_t rear{-1}; ///< Index of tail of the array
|
||||
std::array<int16_t, max_size> arr; ///< All stored data
|
||||
int dequeue(); ///< Delete element from back of the queue
|
||||
void display() const; ///< Show all saved data
|
||||
private:
|
||||
int8_t front{-1}; ///< Index of head of the array
|
||||
int8_t rear{-1}; ///< Index of tail of the array
|
||||
std::array<int16_t, max_size> arr{}; ///< All stored data
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Adds new element to the end of the queue
|
||||
* @param ele to be added to the end of the queue
|
||||
*/
|
||||
void Queue_Array::enqueue(const int16_t& ele ) {
|
||||
void Queue_Array::enqueue(const int16_t& ele) {
|
||||
if (rear == arr.size() - 1) {
|
||||
std::cout << "\nStack is full";
|
||||
} else if (front == -1 && rear == -1) {
|
||||
@@ -98,7 +101,6 @@ void Queue_Array::display() const {
|
||||
} // namespace queue_using_array
|
||||
} // namespace data_structures
|
||||
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @details
|
||||
|
||||
Reference in New Issue
Block a user