diff --git a/client_server/udp_server.c b/client_server/udp_server.c index 5342bf6d..2be350a2 100644 --- a/client_server/udp_server.c +++ b/client_server/udp_server.c @@ -30,7 +30,7 @@ int main() memset(&cliaddr, 0, sizeof(cliaddr)); // Filling server information - servaddr.sin_family = AF_INET; // IPv4 + servaddr.sin_family = AF_INET; // IPv4 servaddr.sin_addr.s_addr = INADDR_ANY; servaddr.sin_port = htons(PORT); diff --git a/conversions/binary_to_decimal.c b/conversions/binary_to_decimal.c index f29037ad..32586358 100644 --- a/conversions/binary_to_decimal.c +++ b/conversions/binary_to_decimal.c @@ -7,7 +7,6 @@ int main() { - int remainder, number = 0, decimal_number = 0, temp = 1; printf("/n Enter any binary number= "); scanf("%d", &number); @@ -15,11 +14,10 @@ int main() // Iterate over the number until the end. while (number > 0) { - remainder = number % 10; number = number / 10; decimal_number += remainder * temp; - temp = temp * 2; // used as power of 2 + temp = temp * 2; // used as power of 2 } printf("%d\n", decimal_number); diff --git a/conversions/binary_to_octal.c b/conversions/binary_to_octal.c index d56d82ae..edcb0461 100644 --- a/conversions/binary_to_octal.c +++ b/conversions/binary_to_octal.c @@ -26,7 +26,7 @@ int main(void) while (binary_num > 0) { if (binary_num > - 111) // Checking if binary number is greater than three digits + 111) // Checking if binary number is greater than three digits td = three_digits(binary_num); else @@ -45,7 +45,7 @@ int main(void) base *= 2; } - res += d * ord; // Calculating the octal value + res += d * ord; // Calculating the octal value ord *= 10; } diff --git a/conversions/decimal_to_binary.c b/conversions/decimal_to_binary.c index d7f68f70..27032b2c 100644 --- a/conversions/decimal_to_binary.c +++ b/conversions/decimal_to_binary.c @@ -5,7 +5,6 @@ int main() { - // input of the user int inputNumber; @@ -35,7 +34,6 @@ int main() // actual processing while (inputNumber > 0) { - // computes the remainder by modulo 2 re = inputNumber % 2; diff --git a/conversions/decimal_to_hexa.c b/conversions/decimal_to_hexa.c index 52648841..89de5962 100644 --- a/conversions/decimal_to_hexa.c +++ b/conversions/decimal_to_hexa.c @@ -4,7 +4,6 @@ void decimal2Hexadecimal(long num); int main() { - long decimalnum; printf("Enter decimal number: "); @@ -19,7 +18,6 @@ int main() * number****************/ void decimal2Hexadecimal(long num) { - long decimalnum = num; long quotient, remainder; int i, j = 0; @@ -29,7 +27,6 @@ void decimal2Hexadecimal(long num) while (quotient != 0) { - remainder = quotient % 16; if (remainder < 10) hexadecimalnum[j++] = 48 + remainder; diff --git a/conversions/decimal_to_octal.c b/conversions/decimal_to_octal.c index dfd13c2f..1d720a43 100644 --- a/conversions/decimal_to_octal.c +++ b/conversions/decimal_to_octal.c @@ -4,7 +4,6 @@ void decimal2Octal(long decimalnum); int main() { - long decimalnum; printf("Enter the decimal number: "); @@ -30,9 +29,7 @@ void decimal2Octal(long decimalnum) quotient = quotient / 8; } - for (j = i - 1; j > 0; j--) - - printf("%d", octalNumber[j]); + for (j = i - 1; j > 0; j--) printf("%d", octalNumber[j]); printf("\n"); } diff --git a/conversions/octal_to_decimal.c b/conversions/octal_to_decimal.c index 579cdc42..b363abb3 100644 --- a/conversions/octal_to_decimal.c +++ b/conversions/octal_to_decimal.c @@ -6,12 +6,10 @@ int convertValue(int num, int i) { return num * pow(8, i); } long long toDecimal(int octal_value) { - int decimal_value = 0, i = 0; while (octal_value) { - // Extracts right-most digit and then multiplies by 8^i decimal_value += convertValue(octal_value % 10, i++); @@ -24,7 +22,6 @@ long long toDecimal(int octal_value) int main() { - printf("Enter octal value: "); int octal_value; diff --git a/data_structures/array/carray_tests.c b/data_structures/array/carray_tests.c index 18875c75..add0fa02 100644 --- a/data_structures/array/carray_tests.c +++ b/data_structures/array/carray_tests.c @@ -13,10 +13,10 @@ * */ -#include "CArray.h" #include #include #include +#include "CArray.h" int CArrayTests() { @@ -37,7 +37,7 @@ int CArrayTests() } printf("Entered array is:\n"); displayCArray(array); - printf("\nCode: %d\n", pushValueCArray(array, 11)); // 5 + printf("\nCode: %d\n", pushValueCArray(array, 11)); // 5 for (i = 0; i < array->size; i++) { @@ -46,8 +46,8 @@ int CArrayTests() displayCArray(array); - printf("\nCode: %d", removeValueCArray(array, -1)); // 1 - printf("\nCode: %d\n", insertValueCArray(array, -1, 1)); // 1 + printf("\nCode: %d", removeValueCArray(array, -1)); // 1 + printf("\nCode: %d\n", insertValueCArray(array, -1, 1)); // 1 // Erase for (i = 0; i < array->size; i++) @@ -55,7 +55,7 @@ int CArrayTests() insertValueCArray(array, i, i + 1); } eraseCArray(array); - displayCArray(array); // Should give all 0s + displayCArray(array); // Should give all 0s // Switching CArray *arr = getCArray(13); diff --git a/data_structures/binary_trees/avl.c b/data_structures/binary_trees/avl.c index f3110dae..67d24ae0 100644 --- a/data_structures/binary_trees/avl.c +++ b/data_structures/binary_trees/avl.c @@ -50,8 +50,7 @@ avlNode *minNode(avlNode *node) { avlNode *temp = node; - while (temp->left != NULL) - temp = temp->left; + while (temp->left != NULL) temp = temp->left; return temp; } @@ -64,8 +63,7 @@ void printAVL(avlNode *node, int level) printAVL(node->right, level + 1); printf("\n\n"); - for (i = 0; i < level; i++) - printf("\t"); + for (i = 0; i < level; i++) printf("\t"); printf("%d", node->key); @@ -117,7 +115,6 @@ avlNode *RightLeftRotate(avlNode *z) avlNode *insert(avlNode *node, int key) { - if (node == NULL) return (newNode(key)); @@ -310,7 +307,6 @@ int main() case 1: { - printf("\n\tEnter the Number to insert: "); scanf("%d", &insertNum); diff --git a/data_structures/binary_trees/binary_search_tree.c b/data_structures/binary_trees/binary_search_tree.c index 8981f0ca..c80873b4 100644 --- a/data_structures/binary_trees/binary_search_tree.c +++ b/data_structures/binary_trees/binary_search_tree.c @@ -12,7 +12,6 @@ // Node, the basic data structure in the tree typedef struct node { - // left child struct node *left; @@ -27,7 +26,6 @@ typedef struct node // pointer node *newNode(int data) { - // creates a slug node *tmp = (node *)malloc(sizeof(node)); @@ -110,7 +108,6 @@ node *delete (node *root, int data) // subtree and switch with the root's else { - // finds the biggest node in the left branch. node *tmp = getMax(root->left); @@ -190,7 +187,6 @@ void inOrder(node *root) void main() { - // this reference don't change. // only the tree changes. node *root = NULL; @@ -200,9 +196,10 @@ void main() // event-loop. while (opt != 0) { - printf("\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get " - "current Height\n[5] Print Tree in Crescent Order\n[0] Quit\n"); - scanf("%d", &opt); // reads the choice of the user + printf( + "\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get " + "current Height\n[5] Print Tree in Crescent Order\n[0] Quit\n"); + scanf("%d", &opt); // reads the choice of the user // processes the choice switch (opt) diff --git a/data_structures/binary_trees/recursive_traversals.c b/data_structures/binary_trees/recursive_traversals.c index 175e16d3..d3dbfde3 100644 --- a/data_structures/binary_trees/recursive_traversals.c +++ b/data_structures/binary_trees/recursive_traversals.c @@ -7,7 +7,7 @@ void inOrderTraversal(struct node *node) { - if (node == NULL) // if tree is empty + if (node == NULL) // if tree is empty return; inOrderTraversal(node->leftNode); @@ -17,7 +17,7 @@ void inOrderTraversal(struct node *node) void preOrderTraversal(struct node *node) { - if (node == NULL) // if tree is empty + if (node == NULL) // if tree is empty return; printf("\t%d\t", node->data); @@ -27,7 +27,7 @@ void preOrderTraversal(struct node *node) void postOrderTraversal(struct node *node) { - if (node == NULL) // if tree is empty + if (node == NULL) // if tree is empty return; postOrderTraversal(node->leftNode); diff --git a/data_structures/binary_trees/redblacktree.c b/data_structures/binary_trees/redblacktree.c index e66895b7..68851226 100644 --- a/data_structures/binary_trees/redblacktree.c +++ b/data_structures/binary_trees/redblacktree.c @@ -91,7 +91,6 @@ Node *rightRotate(Node *node) // Check the node after the insertion step void checkNode(Node *node) { - // If the node is the root if (node == NULL || node->par == NULL) { @@ -166,7 +165,7 @@ void checkNode(Node *node) grandParent->color = 1; } else - { // Right Left Case + { // Right Left Case // First step -> Parent Child Rotation parent->left = child->right; if (child->right != NULL) @@ -206,7 +205,7 @@ void checkNode(Node *node) } } else - { // Left Case + { // Left Case // Left Left Case if (parent->left == node) { @@ -238,7 +237,7 @@ void checkNode(Node *node) grandParent->color = 1; } else - { // Left Right Case + { // Left Right Case // First step -> Parent Child Rotation parent->right = child->left; @@ -307,7 +306,6 @@ void insertNode(int val, Node **root) } else { - // Go right if (buffRoot->right != NULL) { @@ -344,7 +342,6 @@ void insertNode(int val, Node **root) void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root) { - if (toDelete == (*root)) { (*root)->color = 0; @@ -374,7 +371,7 @@ void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root) // Get the sibling for further inspection Node *sibling; Node *parent = toDelete->par; - int locateChild = 0; // 0 if toDeleted is left of its parent else 1 + int locateChild = 0; // 0 if toDeleted is left of its parent else 1 if (parent->right == toDelete) { sibling = parent->left; @@ -391,11 +388,9 @@ void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root) { if (sibling->right != NULL && sibling->right->color == 1) { - // Sibling is left and child is right. i.e. LEFT RIGHT ROTATION if (locateChild == 1) { - int parColor = parent->color; // Step 1: Left rotate sibling @@ -427,8 +422,8 @@ void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root) } } else - { // Sibling is right and child is also right. i.e. LEFT LEFT - // ROTATION + { // Sibling is right and child is also right. i.e. LEFT LEFT + // ROTATION int parColor = parent->color; @@ -460,11 +455,9 @@ void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root) } else { - // Sibling is right and child is left. i.e. RIGHT LEFT ROTATION if (locateChild == 0) { - int parColor = parent->color; // Step 1: Right rotate sibling @@ -499,8 +492,8 @@ void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root) } } else - { // Sibling is left and child is also left. i.e. RIGHT RIGHT - // ROTATION + { // Sibling is left and child is also left. i.e. RIGHT RIGHT + // ROTATION int parColor = parent->color; @@ -532,7 +525,7 @@ void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root) } } else if (sibling->color == 0) - { // Make the sibling red and recur for its parent + { // Make the sibling red and recur for its parent // Recolor the sibling sibling->color = 1; @@ -561,9 +554,9 @@ void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root) checkForCase2(parent, 0, locateChild, root); } else - { // Bring the sibling on top and apply 2.1 or 2.2 accordingly + { // Bring the sibling on top and apply 2.1 or 2.2 accordingly if (locateChild) - { // Right Rotate + { // Right Rotate toDelete->par->right = toDelete->left; if (toDelete->left != NULL) @@ -584,7 +577,7 @@ void checkForCase2(Node *toDelete, int delete, int fromDirection, Node **root) checkForCase2(parent->right, 0, 1, root); } else - { // Left Rotate + { // Left Rotate toDelete->par->left = toDelete->right; if (toDelete->right != NULL) @@ -616,7 +609,6 @@ void deleteNode(int val, Node **root) // Search for the element in the tree while (1) { - if (val == buffRoot->val) { // Node Found @@ -684,7 +676,6 @@ void deleteNode(int val, Node **root) (toDelete->left != NULL && toDelete->left->color == 1) || (toDelete->right != NULL && toDelete->right->color == 1)) { - // if it is a leaf if (toDelete->left == NULL && toDelete->right == NULL) { @@ -699,7 +690,7 @@ void deleteNode(int val, Node **root) } } else - { // else its child should be red + { // else its child should be red // Check for the exitstence of left node if (toDelete->left != NULL) @@ -710,7 +701,7 @@ void deleteNode(int val, Node **root) toDelete->left->color = 1; } else - { // else the right node should be red + { // else the right node should be red toDelete->par->left = toDelete->right; toDelete->right->par = toDelete->par; toDelete->right->color = 1; @@ -721,7 +712,7 @@ void deleteNode(int val, Node **root) free(toDelete); } else - { // Case 2 + { // Case 2 checkForCase2(toDelete, 1, ((toDelete->par->right == toDelete)), root); } } @@ -755,8 +746,9 @@ int main() { Node *root = NULL; int scanValue, choice = 1; - printf("1 - Input\n2 - Delete\n3 - Inorder Traversel\n0 - Quit\n\nPlease " - "Enter the Choice - "); + printf( + "1 - Input\n2 - Delete\n3 - Inorder Traversel\n0 - Quit\n\nPlease " + "Enter the Choice - "); scanf("%d", &choice); while (choice) { @@ -795,8 +787,9 @@ int main() printf("Root - %d\n", root->val); } } - printf("1 - Input\n2 - Delete\n3 - Inorder Traversel\n0 - " - "Quit\n\nPlease Enter the Choice - "); + printf( + "1 - Input\n2 - Delete\n3 - Inorder Traversel\n0 - " + "Quit\n\nPlease Enter the Choice - "); scanf("%d", &choice); } } diff --git a/data_structures/binary_trees/threaded_binary_trees.c b/data_structures/binary_trees/threaded_binary_trees.c index 8caf3772..b9ff2560 100644 --- a/data_structures/binary_trees/threaded_binary_trees.c +++ b/data_structures/binary_trees/threaded_binary_trees.c @@ -51,8 +51,8 @@ node *create_node(int data) void insert_bt(node **root, int data) { node *new_node = create_node(data); - node *temp; // to be deleted - node *prev; // keeps track of the parent of the element deleted + node *temp; // to be deleted + node *prev; // keeps track of the parent of the element deleted if (*root == NULL) { *root = new_node; @@ -204,7 +204,7 @@ void delete_bt(node **root, int ele) return; else { - node *replacement; // deleted node's replacement + node *replacement; // deleted node's replacement node *t; if (temp->llink == NULL && temp->rlink == NULL) { @@ -220,15 +220,15 @@ void delete_bt(node **root, int ele) } else { - replacement = temp->rlink; // replaced with inorder successor + replacement = temp->rlink; // replaced with inorder successor t = replacement; while (t->llink != NULL) { t = t->llink; } t->llink = - temp->llink; // leftmost node of the replacement is linked to - // the left child of the deleted node + temp->llink; // leftmost node of the replacement is linked to + // the left child of the deleted node } if (temp == *root) diff --git a/data_structures/dynamic_array/main.c b/data_structures/dynamic_array/main.c index 9948a919..0feb7901 100644 --- a/data_structures/dynamic_array/main.c +++ b/data_structures/dynamic_array/main.c @@ -1,6 +1,6 @@ -#include "dynamic_array.h" #include #include +#include "dynamic_array.h" int main() { diff --git a/data_structures/graphs/bellman_ford.c b/data_structures/graphs/bellman_ford.c index 48afdbf1..9cd4179c 100644 --- a/data_structures/graphs/bellman_ford.c +++ b/data_structures/graphs/bellman_ford.c @@ -74,8 +74,7 @@ void BellmanFord(struct Graph *graph, int src) // Initialize distances array as INF for all except source // Intialize source as zero - for (int i = 0; i < V; i++) - dist[i] = INT_MAX; + for (int i = 0; i < V; i++) dist[i] = INT_MAX; dist[src] = 0; // Calculate shortest path distance from source to all edges @@ -100,8 +99,9 @@ void BellmanFord(struct Graph *graph, int src) if (dist[u] != INT_MAX && dist[u] + w < dist[v]) { - printf("Graph contains negative weight cycle. Hence, shortest " - "distance not guaranteed."); + printf( + "Graph contains negative weight cycle. Hence, shortest " + "distance not guaranteed."); return; } } diff --git a/data_structures/graphs/bfsqueue.c b/data_structures/graphs/bfsqueue.c index 664c6dc5..5612381b 100644 --- a/data_structures/graphs/bfsqueue.c +++ b/data_structures/graphs/bfsqueue.c @@ -1,18 +1,17 @@ -#include "Graph.h" -#include "queue.h" #include #include +#include "Graph.h" +#include "queue.h" #define MAX_NODES 1000 -int visited[MAX_NODES]; // array to store visiting order - // indexed by vertex 0..nV-1 +int visited[MAX_NODES]; // array to store visiting order + // indexed by vertex 0..nV-1 bool findPathBFS(Graph g, int nV, Vertex src, Vertex dest) { Vertex v; - for (v = 0; v < nV; v++) - visited[v] = -1; + for (v = 0; v < nV; v++) visited[v] = -1; visited[src] = src; queue Q = newQueue(); diff --git a/data_structures/graphs/dfs.c b/data_structures/graphs/dfs.c index 9316b8d7..f7110759 100644 --- a/data_structures/graphs/dfs.c +++ b/data_structures/graphs/dfs.c @@ -14,8 +14,8 @@ struct Graph int numVertices; int *visited; struct node * - *adjLists; // we need int** to store a two dimensional array. Similary, - // we need struct node** to store an array of Linked lists + *adjLists; // we need int** to store a two dimensional array. Similary, + // we need struct node** to store an array of Linked lists }; struct Graph *createGraph(int); void addEdge(struct Graph *, int, int); diff --git a/data_structures/graphs/dfsrecursive.c b/data_structures/graphs/dfsrecursive.c index 689fc3d7..d62f0c1d 100644 --- a/data_structures/graphs/dfsrecursive.c +++ b/data_structures/graphs/dfsrecursive.c @@ -1,11 +1,11 @@ -#include "Graph.h" #include #include +#include "Graph.h" #define MAX_NODES 1000 -int visited[MAX_NODES]; // array to store visiting order - // indexed by vertex 0..nV-1 +int visited[MAX_NODES]; // array to store visiting order + // indexed by vertex 0..nV-1 bool dfsPathCheck(Graph g, int nV, Vertex v, Vertex dest) { @@ -25,8 +25,7 @@ bool dfsPathCheck(Graph g, int nV, Vertex v, Vertex dest) bool findPathDFS(Graph g, int nV, Vertex src, Vertex dest) { Vertex v; - for (v = 0; v < nV; v++) - visited[v] = -1; + for (v = 0; v < nV; v++) visited[v] = -1; visited[src] = src; return dfsPathCheck(g, nV, src, dest); } diff --git a/data_structures/graphs/dijkstra.c b/data_structures/graphs/dijkstra.c index 6f2d1378..b1faa472 100644 --- a/data_structures/graphs/dijkstra.c +++ b/data_structures/graphs/dijkstra.c @@ -18,8 +18,7 @@ void createGraph(struct Graph *G, int V) for (int i = 0; i < V; i++) { G->edges[i] = (int *)malloc(V * sizeof(int)); - for (int j = 0; j < V; j++) - G->edges[i][j] = INT_MAX; + for (int j = 0; j < V; j++) G->edges[i][j] = INT_MAX; G->edges[i][i] = 0; } } @@ -63,13 +62,12 @@ void print(int dist[], int V) void Dijkstra(struct Graph *graph, int src) { int V = graph->vertexNum; - int mdist[V]; // Stores updated distances to vertex - int vset[V]; // vset[i] is true if the vertex i included - // in the shortest path tree + int mdist[V]; // Stores updated distances to vertex + int vset[V]; // vset[i] is true if the vertex i included + // in the shortest path tree // Initialise mdist and vset. Set distance of source as zero - for (int i = 0; i < V; i++) - mdist[i] = INT_MAX, vset[i] = 0; + for (int i = 0; i < V; i++) mdist[i] = INT_MAX, vset[i] = 0; mdist[src] = 0; diff --git a/data_structures/graphs/euler.c b/data_structures/graphs/euler.c index 7cceeb39..32cfc2d1 100644 --- a/data_structures/graphs/euler.c +++ b/data_structures/graphs/euler.c @@ -1,6 +1,6 @@ -#include "Graph.h" #include #include +#include "Graph.h" // Return the number of vertices that v is // connected to diff --git a/data_structures/graphs/floyd_warshall.c b/data_structures/graphs/floyd_warshall.c index 69a17ecc..19a86a06 100644 --- a/data_structures/graphs/floyd_warshall.c +++ b/data_structures/graphs/floyd_warshall.c @@ -18,8 +18,7 @@ void createGraph(struct Graph *G, int V) for (int i = 0; i < V; i++) { G->edges[i] = (int *)malloc(V * sizeof(int)); - for (int j = 0; j < V; j++) - G->edges[i][j] = INT_MAX; + for (int j = 0; j < V; j++) G->edges[i][j] = INT_MAX; G->edges[i][i] = 0; } } @@ -38,7 +37,6 @@ void print(int dist[], int V) { for (int j = 0; j < V; j++) { - if (dist[i * V + j] != INT_MAX) printf("%d\t", dist[i * V + j]); else @@ -57,8 +55,7 @@ void FloydWarshall(struct Graph *graph) // Initialise distance array for (int i = 0; i < V; i++) - for (int j = 0; j < V; j++) - dist[i][j] = graph->edges[i][j]; + for (int j = 0; j < V; j++) dist[i][j] = graph->edges[i][j]; // Calculate distances for (int k = 0; k < V; k++) @@ -79,8 +76,7 @@ void FloydWarshall(struct Graph *graph) // Convert 2d array to 1d array for print int dist1d[V * V]; for (int i = 0; i < V; i++) - for (int j = 0; j < V; j++) - dist1d[i * V + j] = dist[i][j]; + for (int j = 0; j < V; j++) dist1d[i * V + j] = dist[i][j]; print(dist1d, V); } diff --git a/data_structures/graphs/graph.c b/data_structures/graphs/graph.c index a7b42cc3..f4c69faa 100644 --- a/data_structures/graphs/graph.c +++ b/data_structures/graphs/graph.c @@ -7,9 +7,9 @@ typedef struct GraphRep { - int **edges; // adjacency matrix - int nV; // #vertices - int nE; // #edges + int **edges; // adjacency matrix + int nV; // #vertices + int nE; // #edges } GraphRep; Graph newGraph(int V) @@ -43,7 +43,7 @@ void insertEdge(Graph g, Edge e) assert(g != NULL && validV(g, e.v) && validV(g, e.w)); if (!g->edges[e.v][e.w]) - { // edge e not in graph + { // edge e not in graph g->edges[e.v][e.w] = 1; g->edges[e.w][e.v] = 1; g->nE++; @@ -55,7 +55,7 @@ void removeEdge(Graph g, Edge e) assert(g != NULL && validV(g, e.v) && validV(g, e.w)); if (g->edges[e.v][e.w]) - { // edge e in graph + { // edge e in graph g->edges[e.v][e.w] = 0; g->edges[e.w][e.v] = 0; g->nE--; @@ -87,8 +87,7 @@ void freeGraph(Graph g) assert(g != NULL); int i; - for (i = 0; i < g->nV; i++) - free(g->edges[i]); + for (i = 0; i < g->nV; i++) free(g->edges[i]); free(g->edges); free(g); } diff --git a/data_structures/graphs/hamiltonian.c b/data_structures/graphs/hamiltonian.c index ea3388cf..00e70ea0 100644 --- a/data_structures/graphs/hamiltonian.c +++ b/data_structures/graphs/hamiltonian.c @@ -1,6 +1,6 @@ -#include "Graph.h" #include #include +#include "Graph.h" #define MAX_NODES 1000 @@ -38,8 +38,7 @@ bool hamiltonR(Graph g, int nV, Vertex v, Vertex dest, int d) bool hasHamiltonianPath(Graph g, int nV, Vertex src, Vertex dest) { Vertex v; - for (v = 0; v < nV; v++) - visited[v] = false; + for (v = 0; v < nV; v++) visited[v] = false; return hamiltonR(g, nV, src, dest, nV - 1); } diff --git a/data_structures/graphs/kruskal.c b/data_structures/graphs/kruskal.c index 63a5163e..49d1c54c 100644 --- a/data_structures/graphs/kruskal.c +++ b/data_structures/graphs/kruskal.c @@ -91,9 +91,9 @@ int myComp(const void *a, const void *b) void KruskalMST(struct Graph *graph) { int V = graph->V; - struct Edge result[V]; // Tnis will store the resultant MST - int e = 0; // An index variable, used for result[] - int i = 0; // An index variable, used for sorted edges + struct Edge result[V]; // Tnis will store the resultant MST + int e = 0; // An index variable, used for result[] + int i = 0; // An index variable, used for sorted edges // Step 1: Sort all the edges in non-decreasing // order of their weight. If we are not allowed to @@ -152,8 +152,8 @@ int main() | \ | 2--------3 4 */ - int V = 4; // Number of vertices in graph - int E = 5; // Number of edges in graph + int V = 4; // Number of vertices in graph + int E = 5; // Number of edges in graph struct Graph *graph = createGraph(V, E); // add edge 0-1 diff --git a/data_structures/graphs/queue.h b/data_structures/graphs/queue.h index a226b5dc..2df425dc 100644 --- a/data_structures/graphs/queue.h +++ b/data_structures/graphs/queue.h @@ -2,11 +2,11 @@ typedef struct QueueRep *queue; -queue newQueue(); // set up empty queue -void dropQueue(queue); // remove unwanted queue -int QueueIsEmpty(queue); // check whether queue is empty -void QueueEnqueue(queue, int); // insert an int at end of queue -int QueueDequeue(queue); // remove int from front of queue +queue newQueue(); // set up empty queue +void dropQueue(queue); // remove unwanted queue +int QueueIsEmpty(queue); // check whether queue is empty +void QueueEnqueue(queue, int); // insert an int at end of queue +int QueueDequeue(queue); // remove int from front of queue // By // .----------------. .----------------. .----------------. diff --git a/data_structures/graphs/strongly_connected_components.c b/data_structures/graphs/strongly_connected_components.c index aed10aa1..652ecf29 100644 --- a/data_structures/graphs/strongly_connected_components.c +++ b/data_structures/graphs/strongly_connected_components.c @@ -1,6 +1,6 @@ #include #include -#define MAX_SIZE 40 // Assume 40 nodes at max in graph +#define MAX_SIZE 40 // Assume 40 nodes at max in graph #define INT_MIN 0 // A vertex of the graph struct node @@ -15,8 +15,8 @@ struct Graph int numVertices; int *visited; struct node * - *adjLists; // we need int** to store a two dimensional array. Similary, - // we need struct node** to store an array of Linked lists + *adjLists; // we need int** to store a two dimensional array. Similary, + // we need struct node** to store an array of Linked lists }; // Structure to create a stack, necessary for topological sorting struct Stack @@ -89,14 +89,14 @@ void fillOrder(int vertex, struct Graph *graph, struct Stack *stack) struct Graph *transpose(struct Graph *g) { struct Graph *graph = - createGraph(g->numVertices); // Number of vertices is same + createGraph(g->numVertices); // Number of vertices is same int i = 0; for (i = 0; i < g->numVertices; i++) { struct node *temp = g->adjLists[i]; while (temp != NULL) { - addEdge(graph, temp->vertex, i); // Reverse all edges + addEdge(graph, temp->vertex, i); // Reverse all edges temp = temp->next; } } @@ -211,7 +211,7 @@ struct Stack *createStack() void push(struct Stack *stack, int element) { stack->arr[++stack->top] = - element; // Increment then add, as we start from -1 + element; // Increment then add, as we start from -1 } // Removes element from stack, or returns INT_MIN if stack empty int pop(struct Stack *stack) diff --git a/data_structures/graphs/topologicalsort.c b/data_structures/graphs/topologicalsort.c index ad008245..a9909cac 100644 --- a/data_structures/graphs/topologicalsort.c +++ b/data_structures/graphs/topologicalsort.c @@ -1,6 +1,6 @@ #include #include -#define MAX_SIZE 40 // Assume 40 nodes at max in graph +#define MAX_SIZE 40 // Assume 40 nodes at max in graph #define INT_MIN 0 // A vertex of the graph struct node @@ -15,8 +15,8 @@ struct Graph int numVertices; int *visited; struct node * - *adjLists; // we need int** to store a two dimensional array. Similary, - // we need struct node** to store an array of Linked lists + *adjLists; // we need int** to store a two dimensional array. Similary, + // we need struct node** to store an array of Linked lists }; // Structure to create a stack, necessary for topological sorting struct Stack @@ -97,8 +97,7 @@ void topologicalSort(struct Graph *graph) topologicalSortHelper(i, graph, stack); } } - while (stack->top != -1) - printf("%d ", pop(stack)); + while (stack->top != -1) printf("%d ", pop(stack)); } // Allocate memory for a node struct node *createNode(int v) @@ -159,7 +158,7 @@ struct Stack *createStack() void push(struct Stack *stack, int element) { stack->arr[++stack->top] = - element; // Increment then add, as we start from -1 + element; // Increment then add, as we start from -1 } // Removes element from stack, or returns INT_MIN if stack empty int pop(struct Stack *stack) diff --git a/data_structures/graphs/transitiveclosure.c b/data_structures/graphs/transitiveclosure.c index 86831512..32ceae03 100644 --- a/data_structures/graphs/transitiveclosure.c +++ b/data_structures/graphs/transitiveclosure.c @@ -11,8 +11,7 @@ void warshall() { int i, s, t; for (s = 0; s < NODES; s++) - for (t = 0; t < NODES; t++) - tc[s][t] = digraph[s][t]; + for (t = 0; t < NODES; t++) tc[s][t] = digraph[s][t]; for (i = 0; i < NODES; i++) for (s = 0; s < NODES; s++) diff --git a/data_structures/linked_list/merge_linked_lists.c b/data_structures/linked_list/merge_linked_lists.c index 2bba2575..c3f5299d 100644 --- a/data_structures/linked_list/merge_linked_lists.c +++ b/data_structures/linked_list/merge_linked_lists.c @@ -105,7 +105,7 @@ int main() merge(); printf("\nMerged Linked List: "); - printlist(head1); // list one has been modified + printlist(head1); // list one has been modified return 0; } diff --git a/data_structures/linked_list/singly_link_list_deletion.c b/data_structures/linked_list/singly_link_list_deletion.c index 83a05107..2df425ce 100644 --- a/data_structures/linked_list/singly_link_list_deletion.c +++ b/data_structures/linked_list/singly_link_list_deletion.c @@ -11,14 +11,14 @@ struct node }; struct node *start = NULL; /////////////////////////////////////////////////////////// -struct node *createnode() // function to create node +struct node *createnode() // function to create node { struct node *t; t = (struct node *)malloc(sizeof(struct node)); return (t); } //////////////////////////////////////////////////////// -void insert() // function to insert at first location +void insert() // function to insert at first location { struct node *p; p = createnode(); @@ -36,7 +36,7 @@ void insert() // function to insert at first location } } /////////////////////////////////////////////////////////// -void deletion() // function to delete from first position +void deletion() // function to delete from first position { struct node *t; if (start == NULL) @@ -52,7 +52,7 @@ void deletion() // function to delete from first position } } /////////////////////////////////////////////////////// -void viewlist() // function to display values +void viewlist() // function to display values { struct node *p; if (start == NULL) diff --git a/data_structures/linked_list/stack_using_linked_lists.c b/data_structures/linked_list/stack_using_linked_lists.c index ef3f7cb0..4bbd7c2d 100644 --- a/data_structures/linked_list/stack_using_linked_lists.c +++ b/data_structures/linked_list/stack_using_linked_lists.c @@ -69,7 +69,6 @@ void pop(struct node *p) void display(struct node *p) { - if (top == NULL) printf("stack is empty\n"); else diff --git a/data_structures/list/list.c b/data_structures/list/list.c index 40764321..9731acf0 100644 --- a/data_structures/list/list.c +++ b/data_structures/list/list.c @@ -29,8 +29,7 @@ L List_push(L list, void *val) int List_length(L list) { int n; - for (n = 0; list; list = list->next) - n++; + for (n = 0; list; list = list->next) n++; return n; } diff --git a/data_structures/list/main.c b/data_structures/list/main.c index 524ed6ee..95c11b66 100644 --- a/data_structures/list/main.c +++ b/data_structures/list/main.c @@ -1,14 +1,13 @@ -#include "list.h" #include #include #include #include +#include "list.h" void print_list(char **array) { int i; - for (i = 0; array[i]; i++) - printf("%s", array[i]); + for (i = 0; array[i]; i++) printf("%s", array[i]); printf("\n"); } diff --git a/data_structures/queue.c b/data_structures/queue.c index e2a7fcb6..231faf7e 100644 --- a/data_structures/queue.c +++ b/data_structures/queue.c @@ -33,7 +33,6 @@ int isEmpty(); int main(int argc, char const *argv[]) { - create(); enque(5); diff --git a/data_structures/stack.c b/data_structures/stack.c index a17e8d0b..026ca525 100644 --- a/data_structures/stack.c +++ b/data_structures/stack.c @@ -38,7 +38,6 @@ int isEmpty(); int main(int argc, char const *argv[]) { - int x, y, z; create(); @@ -54,7 +53,7 @@ int main(int argc, char const *argv[]) y = pop(); // 3, 2. Count: 1. Empty: 0; printf("%d, %d.\t\tCount: %d.\tEmpty: %d.\n", x, y, size(), isEmpty()); - pop(); // Empty the stack. + pop(); // Empty the stack. push(5); push(6); diff --git a/data_structures/stack/main.c b/data_structures/stack/main.c index 2886f1cc..ebdabc46 100644 --- a/data_structures/stack/main.c +++ b/data_structures/stack/main.c @@ -11,7 +11,6 @@ int a[100], top = -1; int main() { - int x; while (1) { diff --git a/data_structures/stack/parenthesis.c b/data_structures/stack/parenthesis.c index 58de28db..126abbc3 100644 --- a/data_structures/stack/parenthesis.c +++ b/data_structures/stack/parenthesis.c @@ -11,16 +11,16 @@ struct node struct node *link; }; -int c = 0; // c used as counter to check if stack is empty or not -struct node *head; // declaring head pointer globally assigned to NULL +int c = 0; // c used as counter to check if stack is empty or not +struct node *head; // declaring head pointer globally assigned to NULL -void push(char x) // function for pushing +void push(char x) // function for pushing { struct node *p = head, *temp; temp = (struct node *)malloc(sizeof(struct node)); temp->data = x; if (head == - NULL) // will be execute only one time i.e, 1st time push is called + NULL) // will be execute only one time i.e, 1st time push is called { head = temp; p = head; @@ -36,7 +36,7 @@ void push(char x) // function for pushing } } -char pop(void) // function for pop +char pop(void) // function for pop { char x; struct node *p = head; @@ -51,16 +51,16 @@ int isBalanced(char *s) { int i = 0; char x; - while (s[i] != '\0') // loop for covering entire string of brackets + while (s[i] != '\0') // loop for covering entire string of brackets { // printf("\t s[i]=%c\n", s[i]); //DEBUG if (s[i] == '{' || s[i] == '(' || - s[i] == '[') // if opening bracket then push + s[i] == '[') // if opening bracket then push push(s[i]); else { - if (c <= 0) // i.e, stack is empty as only opening brackets are - // added to stack + if (c <= 0) // i.e, stack is empty as only opening brackets are + // added to stack return 0; x = pop(); diff --git a/data_structures/stack/stack.c b/data_structures/stack/stack.c index 93bf0e34..31cd4d2b 100644 --- a/data_structures/stack/stack.c +++ b/data_structures/stack/stack.c @@ -33,7 +33,6 @@ int offset = -1; void initStack() { - array = malloc(sizeof(void *) * max); assert(array); /* tests whether pointer is assigned to memory. */ } @@ -46,7 +45,7 @@ void grow() { max += 10; /* increases the capacity */ - int i; // for the loop + int i; // for the loop void **tmp = malloc(sizeof(void *) * max); /* copies the elements from the origin array in the new one. */ @@ -62,12 +61,10 @@ void grow() /* push: pushs the argument onto the stack */ void push(void *object) { - assert(object); /* tests whether pointer isn't null */ if (counter < max) { - offset++; /* increases the element-pointer */ /* @@ -81,7 +78,6 @@ void push(void *object) } else /* stack is full */ { - grow(); /* lets grow stack */ push(object); /* recursive call */ } @@ -92,7 +88,6 @@ void push(void *object) */ void *pop() { - void *top = *(array + offset); /* check pointers */ diff --git a/data_structures/stack/stack_linked_list/main.c b/data_structures/stack/stack_linked_list/main.c index 34d0429e..1e1a6c77 100644 --- a/data_structures/stack/stack_linked_list/main.c +++ b/data_structures/stack/stack_linked_list/main.c @@ -1,7 +1,7 @@ -#include "stack.h" #include #include #include +#include "stack.h" int main() { diff --git a/exercism/acronym/acronym.c b/exercism/acronym/acronym.c index 4c0477f3..2c1c4e64 100644 --- a/exercism/acronym/acronym.c +++ b/exercism/acronym/acronym.c @@ -84,8 +84,7 @@ char *abbreviate(const char *phrase) strcat(acr, words[i]); } - for (i = 0; i < counter; i++) - free(words[i]); + for (i = 0; i < counter; i++) free(words[i]); free(words); return acr; diff --git a/exercism/isogram/isogram.c b/exercism/isogram/isogram.c index d24f0bc7..574b98d9 100644 --- a/exercism/isogram/isogram.c +++ b/exercism/isogram/isogram.c @@ -6,7 +6,6 @@ */ bool is_isogram(const char phrase[]) { - /* use 'unsigned' because of the function strlen(...) */ unsigned int i = 0; unsigned int j = 0; diff --git a/exercism/rna_transcription/rna_transcription.c b/exercism/rna_transcription/rna_transcription.c index 856af351..8d3b1802 100644 --- a/exercism/rna_transcription/rna_transcription.c +++ b/exercism/rna_transcription/rna_transcription.c @@ -4,7 +4,6 @@ char *to_rna(const char s[]) { - /* determines the length of the given string */ int len = strlen(s); diff --git a/exercism/word_count/word_count.c b/exercism/word_count/word_count.c index 669fa88b..206a16dc 100644 --- a/exercism/word_count/word_count.c +++ b/exercism/word_count/word_count.c @@ -41,7 +41,6 @@ int word_count(const char *input_text, word_count_word_t *words) input[index] = '\0'; if (strlen(p_str) <= MAX_WORD_LENGTH) { - if (index_list <= MAX_WORDS) { strcpy(word_list[index_list], p_str); diff --git a/exercism/word_count/word_count.h b/exercism/word_count/word_count.h index c06f19b9..584db736 100644 --- a/exercism/word_count/word_count.h +++ b/exercism/word_count/word_count.h @@ -1,8 +1,8 @@ #ifndef WORD_COUNT_H #define WORD_COUNT_H -#define MAX_WORDS 20 // at most MAX_WORDS can be found in the test input string -#define MAX_WORD_LENGTH 50 // no individual word can exceed this length +#define MAX_WORDS 20 // at most MAX_WORDS can be found in the test input string +#define MAX_WORD_LENGTH 50 // no individual word can exceed this length // results structure typedef struct word_count_word diff --git a/greedy_approach/djikstra.c b/greedy_approach/djikstra.c index fb8c6571..e9d1c63c 100644 --- a/greedy_approach/djikstra.c +++ b/greedy_approach/djikstra.c @@ -63,7 +63,6 @@ void dijkstra(int s) int main(int argc, char const *argv[]) { - printf("Enter the number of vertices: "); scanf(" %d", &V); printf("Enter the adj matrix: "); diff --git a/hash/test_program.c b/hash/test_program.c index 604ad89f..ab93bab9 100644 --- a/hash/test_program.c +++ b/hash/test_program.c @@ -3,8 +3,8 @@ This file contains a simple test program for each hash-function. */ -#include "hash.h" #include +#include "hash.h" int main(void) { diff --git a/leetcode/src/11.c b/leetcode/src/11.c index 05f9d850..5a38ebc5 100644 --- a/leetcode/src/11.c +++ b/leetcode/src/11.c @@ -4,7 +4,6 @@ int min(int a, int b) { return ((a < b) ? a : b); } // Two pointer approach to find maximum container area int maxArea(int *height, int heightSize) { - // Start with maximum container width int start = 0; int end = heightSize - 1; diff --git a/leetcode/src/1184.c b/leetcode/src/1184.c index 56ea3d0a..bbb9f706 100644 --- a/leetcode/src/1184.c +++ b/leetcode/src/1184.c @@ -1,7 +1,6 @@ int distanceBetweenBusStops(int *distance, int distanceSize, int start, int destination) { - int sum1 = 0, sum2 = 0; if (start > destination) { diff --git a/leetcode/src/136.c b/leetcode/src/136.c index bdbab6b9..c18fe933 100644 --- a/leetcode/src/136.c +++ b/leetcode/src/136.c @@ -1,7 +1,6 @@ int singleNumber(int *nums, int numsSize) { int i, result = 0; - for (i = 0; i < numsSize; i++) - result = result ^ nums[i]; + for (i = 0; i < numsSize; i++) result = result ^ nums[i]; return result; } diff --git a/leetcode/src/190.c b/leetcode/src/190.c index 19e15f79..7ab3bbe2 100644 --- a/leetcode/src/190.c +++ b/leetcode/src/190.c @@ -1,25 +1,26 @@ uint32_t reverseBits(uint32_t n) { uint TotalBits = 32; - uint32_t reverse_int = 0; // stored in memory as 32 bits, each bit valued 0 + uint32_t reverse_int = 0; // stored in memory as 32 bits, each bit valued 0 uint i; for (i = 0; i < TotalBits; i++) { if ((n & (UINT32_C(1) - << i))) // if the bit on the ith position of 32 bit input is - // 1, then proceed Further note the use of UINT32_C to - // convert 1 to unsigned 32 bit int, since just 1 is - // treated as int which cannot be shifted left more - // than 30 times + << i))) // if the bit on the ith position of 32 bit input is + // 1, then proceed Further note the use of UINT32_C + // to convert 1 to unsigned 32 bit int, since just 1 + // is treated as int which cannot be shifted left + // more than 30 times reverse_int = reverse_int | (UINT32_C(1) << (TotalBits - 1 - - i)); // Convert the ith bit from the end in reverse_int - // from 0 to 1, if ith bit from beginning in n is 1 - // This is achieved by using bitwise OR on reverse_int - // (where ith bit from end is currently 0) and 1 - // shifted left 31 - i bits (to ith bit from the end) + i)); // Convert the ith bit from the end in reverse_int + // from 0 to 1, if ith bit from beginning in n is 1 + // This is achieved by using bitwise OR on + // reverse_int (where ith bit from end is currently + // 0) and 1 shifted left 31 - i bits (to ith bit from + // the end) } return reverse_int; } \ No newline at end of file diff --git a/leetcode/src/191.c b/leetcode/src/191.c index 169676da..d28854cf 100644 --- a/leetcode/src/191.c +++ b/leetcode/src/191.c @@ -6,10 +6,10 @@ int hammingWeight(uint32_t n) { if (n & (UINT32_C(1) - << i)) // if the bit on the ith position of 32 bit input is 1, - // then proceed Further note the use of UINT32_C to - // convert 1 to unsigned 32 bit int, as just 1 is treated - // as int which cannot be shifted left more than 30 times + << i)) // if the bit on the ith position of 32 bit input is 1, + // then proceed Further note the use of UINT32_C to + // convert 1 to unsigned 32 bit int, as just 1 is treated + // as int which cannot be shifted left more than 30 times weight += 1; } return weight; diff --git a/leetcode/src/231.c b/leetcode/src/231.c index aed27b97..a2d3b1e7 100644 --- a/leetcode/src/231.c +++ b/leetcode/src/231.c @@ -2,7 +2,6 @@ bool isPowerOfTwo(int n) { if (!n) return false; - while (n % 2 == 0) - n /= 2; + while (n % 2 == 0) n /= 2; return n == 1; } \ No newline at end of file diff --git a/leetcode/src/242.c b/leetcode/src/242.c index 5d077750..9b4abbf6 100644 --- a/leetcode/src/242.c +++ b/leetcode/src/242.c @@ -4,14 +4,11 @@ bool isAnagram(char *s, char *t) int m = strlen(t); int cnt_s[1000], cnt_t[1000]; - for (int c = 97; c < 97 + 26; c++) - cnt_s[c] = cnt_t[c] = 0; + for (int c = 97; c < 97 + 26; c++) cnt_s[c] = cnt_t[c] = 0; - for (int i = 0; i < n; i++) - cnt_s[s[i]]++; + for (int i = 0; i < n; i++) cnt_s[s[i]]++; - for (int i = 0; i < m; i++) - cnt_t[t[i]]++; + for (int i = 0; i < m; i++) cnt_t[t[i]]++; for (int c = 97; c < 97 + 26; c++) if (cnt_s[c] != cnt_t[c]) diff --git a/leetcode/src/3.c b/leetcode/src/3.c index 29be37ce..2acd0823 100644 --- a/leetcode/src/3.c +++ b/leetcode/src/3.c @@ -1,29 +1,28 @@ int lengthOfLongestSubstring(char *str) { - int n = strlen(str); if (!n) return 0; - int L_len = 1; // lenght of longest substring - int C_len = 1; // lenght of current substring + int L_len = 1; // lenght of longest substring + int C_len = 1; // lenght of current substring - int P_ind, i; // P_ind for previous index - int visited[256]; // visited will keep track of visiting char for the last - // instance. since there are 256 ASCII char, its size is - // limited to that value. + int P_ind, i; // P_ind for previous index + int visited[256]; // visited will keep track of visiting char for the last + // instance. since there are 256 ASCII char, its size is + // limited to that value. memset(visited, -1, sizeof(int) * 256); visited[str[0]] = - 0; // the index of that char will tell us that when it was visited. + 0; // the index of that char will tell us that when it was visited. for (i = 1; i < n; i++) { P_ind = visited[str[i]]; if (P_ind == -1 || i - C_len > P_ind) - C_len++; // if the current char was not visited earlier, or it is - // not the part of current substring + C_len++; // if the current char was not visited earlier, or it is + // not the part of current substring else - { // otherwise, we need to change the current/longest substring length + { // otherwise, we need to change the current/longest substring length if (C_len > L_len) L_len = C_len; C_len = i - P_ind; @@ -57,8 +56,7 @@ int lengthOfLongestSubstring(char *s) if (cur_max >= max) max = cur_max; cur_max = 0; - while (s[end - 1] != c) - end--; + while (s[end - 1] != c) end--; } } if (cur_max >= max) diff --git a/leetcode/src/38.c b/leetcode/src/38.c index 18a7fcc6..59965546 100644 --- a/leetcode/src/38.c +++ b/leetcode/src/38.c @@ -1,6 +1,5 @@ char *countAndSay(int n) { - // Calculating the length of array double result = 1.0; for (int i = 0; i < n - 1; i++) diff --git a/leetcode/src/387.c b/leetcode/src/387.c index bb856b0f..cbc78b7e 100644 --- a/leetcode/src/387.c +++ b/leetcode/src/387.c @@ -2,8 +2,7 @@ int firstUniqChar(char *s) { int *arr = calloc(256, sizeof(int)); int i; - for (i = 0; i < strlen(s); i++) - arr[s[i]] = arr[s[i]] + 1; + for (i = 0; i < strlen(s); i++) arr[s[i]] = arr[s[i]] + 1; for (i = 0; i < strlen(s); i++) { if (arr[s[i]] == 1) diff --git a/leetcode/src/389.c b/leetcode/src/389.c index 0846934c..3775a55a 100644 --- a/leetcode/src/389.c +++ b/leetcode/src/389.c @@ -2,9 +2,7 @@ char findTheDifference(char *s, char *t) { int sum1 = 0, sum2 = 0; int i; - for (i = 0; i < strlen(s); i++) - sum1 += s[i]; - for (i = 0; i < strlen(t); i++) - sum2 += t[i]; + for (i = 0; i < strlen(s); i++) sum1 += s[i]; + for (i = 0; i < strlen(t); i++) sum2 += t[i]; return (char)(sum2 - sum1); } diff --git a/leetcode/src/442.c b/leetcode/src/442.c index 35d558b4..7bfdfb45 100644 --- a/leetcode/src/442.c +++ b/leetcode/src/442.c @@ -2,7 +2,6 @@ int cmpval(const void *a, const void *b) { return *(int *)a - *(int *)b; } int *findDuplicates(int *nums, int numsSize, int *returnSize) { - int i; qsort(nums, numsSize, sizeof(int), cmpval); int *retArr = malloc(numsSize * sizeof(int)); diff --git a/leetcode/src/461.c b/leetcode/src/461.c index d05b1b62..6453f769 100644 --- a/leetcode/src/461.c +++ b/leetcode/src/461.c @@ -1,19 +1,19 @@ int hammingDistance(int x, int y) { int difference = - x ^ y; // The XOR operator generates the bitwise difference in the - // binary representation of two numbers If bit in ith position of - // both numbers is same, bit in difference is 0, otherwise 1 - int TotalBits = sizeof(difference) * 8; // total number of bits + x ^ y; // The XOR operator generates the bitwise difference in the + // binary representation of two numbers If bit in ith position + // of both numbers is same, bit in difference is 0, otherwise 1 + int TotalBits = sizeof(difference) * 8; // total number of bits int i, distance = 0; for (i = 0; i < TotalBits; i++) { if (difference & (UINT32_C(1) - << i)) // if the bit on the ith position of 32 bit input is 1, then - // proceed Further note the use of UINT32_C to convert 1 to - // unsigned 32 bit int, as just 1 is treated as int which - // cannot be shifted left more than 30 times + << i)) // if the bit on the ith position of 32 bit input is 1, + // then proceed Further note the use of UINT32_C to convert + // 1 to unsigned 32 bit int, as just 1 is treated as int + // which cannot be shifted left more than 30 times distance += 1; } return distance; diff --git a/leetcode/src/476.c b/leetcode/src/476.c index 5a5825f9..27e52e1f 100644 --- a/leetcode/src/476.c +++ b/leetcode/src/476.c @@ -3,21 +3,21 @@ int findComplement(int num) int TotalBits = 0; int temp = num; while (temp) - { // To find position of MSB in given num. Since num is represented as a - // standard size in memory, we cannot rely on size for that information. - TotalBits++; // increment TotalBits till temp becomes 0 - temp >>= 1; // shift temp right by 1 bit every iteration; temp loses 1 - // bit to underflow every iteration till it becomes 0 + { // To find position of MSB in given num. Since num is represented as a + // standard size in memory, we cannot rely on size for that information. + TotalBits++; // increment TotalBits till temp becomes 0 + temp >>= 1; // shift temp right by 1 bit every iteration; temp loses 1 + // bit to underflow every iteration till it becomes 0 } int i, - flipNumber = - 1; // Eg: 1's complement of 101(binary) can be found as 101^111 (XOR - // with 111 flips all bits that are 1 to 0 and flips 0 to 1) + flipNumber = 1; // Eg: 1's complement of 101(binary) can be found as + // 101^111 (XOR with 111 flips all bits that are 1 to 0 + // and flips 0 to 1) for (i = 1; i < TotalBits; i++) { flipNumber += UINT32_C(1) - << i; // Note the use of unsigned int to facilitate left - // shift more than 31 times, if needed + << i; // Note the use of unsigned int to facilitate left + // shift more than 31 times, if needed } num = num ^ flipNumber; return num; diff --git a/leetcode/src/561.c b/leetcode/src/561.c index b090fccc..fde1ad6f 100644 --- a/leetcode/src/561.c +++ b/leetcode/src/561.c @@ -3,7 +3,6 @@ int arrayPairSum(int *nums, int numsSize) { int sum = 0, i; qsort(nums, numsSize, sizeof(int), cmpval); - for (i = 0; i < numsSize; i = i + 2) - sum = sum + nums[i]; + for (i = 0; i < numsSize; i = i + 2) sum = sum + nums[i]; return sum; } diff --git a/leetcode/src/709.c b/leetcode/src/709.c index 032c1ecd..db2fdee5 100644 --- a/leetcode/src/709.c +++ b/leetcode/src/709.c @@ -1,6 +1,5 @@ char *toLowerCase(char *str) { - for (int i = 0; i < strlen(str); i++) - str[i] = tolower(str[i]); + for (int i = 0; i < strlen(str); i++) str[i] = tolower(str[i]); return str; } diff --git a/leetcode/src/771.c b/leetcode/src/771.c index eabc9d4f..a635f106 100644 --- a/leetcode/src/771.c +++ b/leetcode/src/771.c @@ -8,12 +8,10 @@ int numJewelsInStones(char *j, char *s) memset(cnt, 0, sizeof(cnt)); // lookup to know which character occurs in j - for (int i = 0; i < lenj; i++) - cnt[j[i]]++; + for (int i = 0; i < lenj; i++) cnt[j[i]]++; // count the characters in s - for (int i = 0; i < lens; i++) - sol += cnt[s[i]]; + for (int i = 0; i < lens; i++) sol += cnt[s[i]]; return sol; } diff --git a/leetcode/src/977.c b/leetcode/src/977.c index 960bd8f6..66b23607 100644 --- a/leetcode/src/977.c +++ b/leetcode/src/977.c @@ -26,8 +26,7 @@ int cmpval(const void *a, const void *b) { return *(int *)a - *(int *)b; } int *sortedSquares(int *A, int ASize, int *returnSize) { int *res = malloc(ASize * sizeof(int)); - for (int i = 0; i < ASize; i++) - res[i] = A[i] * A[i]; + for (int i = 0; i < ASize; i++) res[i] = A[i] * A[i]; *returnSize = ASize; qsort(res, ASize, sizeof(int), cmpval); return res; diff --git a/machine_learning/adaline_learning.c b/machine_learning/adaline_learning.c index ecef67b1..341d7de2 100644 --- a/machine_learning/adaline_learning.c +++ b/machine_learning/adaline_learning.c @@ -30,18 +30,17 @@ #include #include -#define MAX_ITER 500 // INT_MAX ///< Maximum number of iterations to learn +#define MAX_ITER 500 // INT_MAX ///< Maximum number of iterations to learn /** structure to hold adaline model parameters */ struct adaline { - - double eta; ///< learning rate of the algorithm - double *weights; ///< weights of the neural network - int num_weights; ///< number of weights of the neural network + double eta; ///< learning rate of the algorithm + double *weights; ///< weights of the neural network + int num_weights; ///< number of weights of the neural network }; -#define ACCURACY 1e-5 ///< convergence accuracy \f$=1\times10^{-5}\f$ +#define ACCURACY 1e-5 ///< convergence accuracy \f$=1\times10^{-5}\f$ /** * Default constructor @@ -70,8 +69,7 @@ struct adaline new_adaline(const int num_features, const double eta) } // initialize with random weights in the range [-50, 49] - for (int i = 0; i < num_weights; i++) - ada.weights[i] = 1.f; + for (int i = 0; i < num_weights; i++) ada.weights[i] = 1.f; // ada.weights[i] = (double)(rand() % 100) - 50); return ada; @@ -100,7 +98,7 @@ int activation(double x) { return x > 0 ? 1 : -1; } */ char *get_weights_str(struct adaline *ada) { - static char out[100]; // static so the value is persistent + static char out[100]; // static so the value is persistent sprintf(out, "<"); for (int i = 0; i < ada->num_weights; i++) @@ -124,15 +122,14 @@ char *get_weights_str(struct adaline *ada) */ int predict(struct adaline *ada, const double *x, double *out) { - double y = ada->weights[ada->num_weights - 1]; // assign bias value + double y = ada->weights[ada->num_weights - 1]; // assign bias value - for (int i = 0; i < ada->num_weights - 1; i++) - y += x[i] * ada->weights[i]; + for (int i = 0; i < ada->num_weights - 1; i++) y += x[i] * ada->weights[i]; - if (out) // if out variable is not NULL + if (out) // if out variable is not NULL *out = y; - return activation(y); // quantizer: apply ADALINE threshold function + return activation(y); // quantizer: apply ADALINE threshold function } /** @@ -148,7 +145,7 @@ double fit_sample(struct adaline *ada, const double *x, const int y) { /* output of the model with current weights */ int p = predict(ada, x, NULL); - int prediction_error = y - p; // error in estimation + int prediction_error = y - p; // error in estimation double correction_factor = ada->eta * prediction_error; /* update each weight, the last weight is the bias term */ @@ -156,7 +153,7 @@ double fit_sample(struct adaline *ada, const double *x, const int y) { ada->weights[i] += correction_factor * x[i]; } - ada->weights[ada->num_weights - 1] += correction_factor; // update bias + ada->weights[ada->num_weights - 1] += correction_factor; // update bias return correction_factor; } @@ -207,16 +204,16 @@ void fit(struct adaline *ada, double **X, const int *y, const int N) */ void test1(double eta) { - struct adaline ada = new_adaline(2, eta); // 2 features + struct adaline ada = new_adaline(2, eta); // 2 features - const int N = 10; // number of sample points + const int N = 10; // number of sample points const double saved_X[10][2] = {{0, 1}, {1, -2}, {2, 3}, {3, -1}, {4, 1}, {6, -5}, {-7, -3}, {-8, 5}, {-9, 2}, {-10, -15}}; double **X = (double **)malloc(N * sizeof(double *)); const int Y[10] = {1, -1, 1, -1, -1, - -1, 1, 1, 1, -1}; // corresponding y-values + -1, 1, 1, 1, -1}; // corresponding y-values for (int i = 0; i < N; i++) { X[i] = (double *)saved_X[i]; @@ -255,19 +252,18 @@ void test1(double eta) */ void test2(double eta) { - struct adaline ada = new_adaline(2, eta); // 2 features + struct adaline ada = new_adaline(2, eta); // 2 features - const int N = 50; // number of sample points + const int N = 50; // number of sample points double **X = (double **)malloc(N * sizeof(double *)); - int *Y = (int *)malloc(N * sizeof(int)); // corresponding y-values - for (int i = 0; i < N; i++) - X[i] = (double *)malloc(2 * sizeof(double)); + int *Y = (int *)malloc(N * sizeof(int)); // corresponding y-values + for (int i = 0; i < N; i++) X[i] = (double *)malloc(2 * sizeof(double)); // generate sample points in the interval // [-range2/100 , (range2-1)/100] - int range = 500; // sample points full-range - int range2 = range >> 1; // sample points half-range + int range = 500; // sample points full-range + int range2 = range >> 1; // sample points half-range for (int i = 0; i < N; i++) { double x0 = ((rand() % range) - range2) / 100.f; @@ -300,8 +296,7 @@ void test2(double eta) printf(" ...passed\n"); } - for (int i = 0; i < N; i++) - free(X[i]); + for (int i = 0; i < N; i++) free(X[i]); free(X); free(Y); delete_adaline(&ada); @@ -320,19 +315,18 @@ void test2(double eta) */ void test3(double eta) { - struct adaline ada = new_adaline(6, eta); // 2 features + struct adaline ada = new_adaline(6, eta); // 2 features - const int N = 50; // number of sample points + const int N = 50; // number of sample points double **X = (double **)malloc(N * sizeof(double *)); - int *Y = (int *)malloc(N * sizeof(int)); // corresponding y-values - for (int i = 0; i < N; i++) - X[i] = (double *)malloc(6 * sizeof(double)); + int *Y = (int *)malloc(N * sizeof(int)); // corresponding y-values + for (int i = 0; i < N; i++) X[i] = (double *)malloc(6 * sizeof(double)); // generate sample points in the interval // [-range2/100 , (range2-1)/100] - int range = 200; // sample points full-range - int range2 = range >> 1; // sample points half-range + int range = 200; // sample points full-range + int range2 = range >> 1; // sample points half-range for (int i = 0; i < N; i++) { double x0 = ((rand() % range) - range2) / 100.f; @@ -374,8 +368,7 @@ void test3(double eta) printf(" ...passed\n"); } - for (int i = 0; i < N; i++) - free(X[i]); + for (int i = 0; i < N; i++) free(X[i]); free(X); free(Y); delete_adaline(&ada); @@ -384,10 +377,10 @@ void test3(double eta) /** Main function */ int main(int argc, char **argv) { - srand(time(NULL)); // initialize random number generator + srand(time(NULL)); // initialize random number generator - double eta = 0.1; // default value of eta - if (argc == 2) // read eta value from commandline argument if present + double eta = 0.1; // default value of eta + if (argc == 2) // read eta value from commandline argument if present eta = strtof(argv[1], NULL); test1(eta); diff --git a/misc/cantor_set.c b/misc/cantor_set.c index 2df3196e..a5d3e6b7 100644 --- a/misc/cantor_set.c +++ b/misc/cantor_set.c @@ -47,7 +47,6 @@ void propagate(Contour *head) void print(Contour *head) { - Contour *temp = head; while (temp != NULL) { @@ -62,7 +61,6 @@ void print(Contour *head) int main(int argc, char const *argv[]) { - head = NULL; int start_num, end_num, levels; diff --git a/misc/cartesian_to_polar.c b/misc/cartesian_to_polar.c index 8aa4b426..f7c4f3e2 100644 --- a/misc/cartesian_to_polar.c +++ b/misc/cartesian_to_polar.c @@ -19,25 +19,25 @@ int main() { theta = atan(y / x); if ((x > 0 && y > 0) || (x == -y)) - { // Q1 + { // Q1 thetaFinal = theta; } else if (x < 0 && y > 0) - { // Q2 + { // Q2 thetaFinal = theta + pi; } else if (x < 0 && y < 0) - { // Q3 + { // Q3 thetaFinal = theta - pi; } else if (x > 0 && y < 0) - { // Q4 + { // Q4 thetaFinal = 2 * pi - theta; } } } if (x == 0) - { // exceptions when no actual angle is present + { // exceptions when no actual angle is present if (y > 0) { thetaFinal = pi / 2; diff --git a/misc/catalan.c b/misc/catalan.c index f985736a..2116f5cb 100644 --- a/misc/catalan.c +++ b/misc/catalan.c @@ -2,27 +2,27 @@ code for computing nth catalan number */ #include -long int factorial(int x) // long int for more than 10 factorial +long int factorial(int x) // long int for more than 10 factorial { int i; - long int fac; // fac stores x factorial + long int fac; // fac stores x factorial fac = x; - for (i = 1; i < x; i++) // loop to calculate x factorial + for (i = 1; i < x; i++) // loop to calculate x factorial { fac = fac * (x - i); } - return fac; // returning x factorial + return fac; // returning x factorial } int main() { - long int f1, f2, f3; // long int for more than 10 factorial + long int f1, f2, f3; // long int for more than 10 factorial int n; - float C; // C is catalan number for n; + float C; // C is catalan number for n; scanf("%d", &n); f1 = factorial(2 * n); f2 = factorial(n + 1); f3 = factorial(n); - C = f1 / (f2 * f3); // formula for catalan number for n + C = f1 / (f2 * f3); // formula for catalan number for n printf("%0.2f", C); return 0; } diff --git a/misc/collatz.c b/misc/collatz.c index 0ba7a0da..3725929b 100644 --- a/misc/collatz.c +++ b/misc/collatz.c @@ -21,18 +21,18 @@ int main(int argc, char *argv[]) else { printf("Enter starting number: "); - scanf("%lu", &n); // input number + scanf("%lu", &n); // input number } - curr_no = n; // curr_no stores input number n - while (curr_no != 1) // loop till series reaches 1 + curr_no = n; // curr_no stores input number n + while (curr_no != 1) // loop till series reaches 1 { num_steps++; printf("%llu->", curr_no); - if (curr_no % 2 == 0) // condition for even number + if (curr_no % 2 == 0) // condition for even number curr_no = curr_no / 2; else - curr_no = (curr_no * 3) + 1; // condition for odd number + curr_no = (curr_no * 3) + 1; // condition for odd number } printf("1\nNumber of steps: %llu\n", num_steps); return 0; diff --git a/misc/factorial.c b/misc/factorial.c index 1c5ae566..3435b2f9 100644 --- a/misc/factorial.c +++ b/misc/factorial.c @@ -25,8 +25,7 @@ int main() temp = temp / 10; } } - for (i = counter; i >= 0; i--) - printf("%d", a[i]); + for (i = counter; i >= 0; i--) printf("%d", a[i]); } return 0; } diff --git a/misc/factorial_large_number.c b/misc/factorial_large_number.c index 9720d709..09634c3b 100644 --- a/misc/factorial_large_number.c +++ b/misc/factorial_large_number.c @@ -107,8 +107,7 @@ int main(int argc, char *argv[]) large_num *result = new_number(); clock_t start_time = clock(); - for (i = 2; i <= number; i++) - /* Multiply every number from 2 thru N */ + for (i = 2; i <= number; i++) /* Multiply every number from 2 thru N */ multiply(result, i); double time_taken = (clock() - start_time) * (double)1e3 / CLOCKS_PER_SEC; // time_taken = (clock() - start_time) / (double) CLOCKS_PER_SEC; diff --git a/misc/factorial_trailing_zeroes.c b/misc/factorial_trailing_zeroes.c index 5d92e1c3..b578c1e6 100644 --- a/misc/factorial_trailing_zeroes.c +++ b/misc/factorial_trailing_zeroes.c @@ -2,7 +2,7 @@ programme for computing number of zeroes at the end of factorial of a given number n */ -#include //including math.h header file to use pow function +#include //including math.h header file to use pow function #include int main() { @@ -16,14 +16,14 @@ int main() test = n / pow(5, - i); // division of n by ith power of 5(storing in integer form) + i); // division of n by ith power of 5(storing in integer form) if (test != - 0) // condition for zeroes at end corresponding individual ith case + 0) // condition for zeroes at end corresponding individual ith case { count = count + test; } else - break; // break the loop for if test=0 + break; // break the loop for if test=0 } printf("%d\n", count); return 0; diff --git a/misc/fibonacci_dp.c b/misc/fibonacci_dp.c index 7bc6af55..fff4532a 100644 --- a/misc/fibonacci_dp.c +++ b/misc/fibonacci_dp.c @@ -17,7 +17,7 @@ int fib(int n) } // declaring array to store fibonacci numbers -- memoization int *f = (int *)malloc( - (n + 2) * sizeof(int)); // one extra to handle edge case, n = 0 + (n + 2) * sizeof(int)); // one extra to handle edge case, n = 0 int i; /* let 0th and 1st number of the series be 0 and 1*/ diff --git a/misc/fibonacci_fast.c b/misc/fibonacci_fast.c index e12a3b98..b356ca4c 100644 --- a/misc/fibonacci_fast.c +++ b/misc/fibonacci_fast.c @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) { unsigned long number, result; - setlocale(LC_NUMERIC, ""); // format the printf output + setlocale(LC_NUMERIC, ""); // format the printf output // Asks for the number/position of term in Fibonnacci sequence if (argc == 2) diff --git a/misc/large_factorials.c b/misc/large_factorials.c index b0477dd6..9670b7ca 100644 --- a/misc/large_factorials.c +++ b/misc/large_factorials.c @@ -2,7 +2,6 @@ int main() { - int a[16500], T; long long int i, j; diff --git a/misc/lerp.c b/misc/lerp.c index 0aa1ca88..7acc3024 100644 --- a/misc/lerp.c +++ b/misc/lerp.c @@ -14,8 +14,9 @@ int main() printf("Input a number, this is the bigger bound of the lerp:\n"); scanf("%f", &finish); - printf("Input a number, this is in how many steps you want to divide the " - "lerp:\n"); + printf( + "Input a number, this is in how many steps you want to divide the " + "lerp:\n"); scanf("%f", &steps); for (int i = 0; i < steps + 1; i++) diff --git a/misc/lexicographic_permutations.c b/misc/lexicographic_permutations.c index e89760ff..20a417d8 100644 --- a/misc/lexicographic_permutations.c +++ b/misc/lexicographic_permutations.c @@ -50,7 +50,7 @@ void PrintSortedPermutations(char *str) int main() { - int n; // size of string + int n; // size of string scanf("%d\n", &n); char *str = (char *)malloc(n * sizeof(char)); scanf("%s", str); diff --git a/misc/longest_subsequence.c b/misc/longest_subsequence.c index 23d506f8..0beeda9d 100644 --- a/misc/longest_subsequence.c +++ b/misc/longest_subsequence.c @@ -2,7 +2,7 @@ #include void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH) -{ // RESULT and RESULT_LENGTH will be modified by their pointers +{ // RESULT and RESULT_LENGTH will be modified by their pointers if (ARRAY_LENGTH <= 1) { @@ -20,7 +20,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH) { if (ARRAY[i] < PIVOT) { - TEMPORARY_ARRAY_LENGTH = 0; TEMPORARY_ARRAY = NULL; @@ -28,7 +27,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH) { if (ARRAY[j] >= ARRAY[i]) { - TEMPORARY_ARRAY_LENGTH++; TEMPORARY_ARRAY = (int *)realloc( TEMPORARY_ARRAY, @@ -41,7 +39,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH) &TEMPORARY_ARRAY, &TEMPORARY_ARRAY_LENGTH); if (LONGEST_SUB_LENGTH < TEMPORARY_ARRAY_LENGTH + 1) { - LONGEST_SUB_LENGTH = TEMPORARY_ARRAY_LENGTH + 1; LONGEST_SUB = (int *)realloc( LONGEST_SUB, LONGEST_SUB_LENGTH * sizeof(int)); @@ -57,7 +54,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH) TEMPORARY_ARRAY_LENGTH = 0; for (i = 1; i < ARRAY_LENGTH; i++) { - if (ARRAY[i] >= PIVOT) { TEMPORARY_ARRAY_LENGTH++; @@ -71,7 +67,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH) &TEMPORARY_ARRAY_LENGTH); if (TEMPORARY_ARRAY_LENGTH + 1 > LONGEST_SUB_LENGTH) { - LONGEST_SUB_LENGTH = TEMPORARY_ARRAY_LENGTH + 1; LONGEST_SUB = (int *)realloc(LONGEST_SUB, LONGEST_SUB_LENGTH * sizeof(int)); @@ -86,7 +81,6 @@ void longestSub(int *ARRAY, int ARRAY_LENGTH, int **RESULT, int *RESULT_LENGTH) int main() { - int EXAMPLE_LENGTH = 8; int EXAMPLE[] = {18, 2, 15, 4, 30, 0, 11, 12}; @@ -96,8 +90,7 @@ int main() longestSub(EXAMPLE, EXAMPLE_LENGTH, &RESULT, &RESULT_LENGTH); printf("Longest Sub Sequence length: %d and it's:\n", RESULT_LENGTH); - for (i = 0; i < RESULT_LENGTH; i++) - printf("%d ", RESULT[i]); + for (i = 0; i < RESULT_LENGTH; i++) printf("%d ", RESULT[i]); printf("\n"); return 0; diff --git a/misc/mirror.c b/misc/mirror.c index 4fbaa744..56ffc88a 100644 --- a/misc/mirror.c +++ b/misc/mirror.c @@ -1,8 +1,8 @@ #include -#include // we include the library string.h to the use of string +#include // we include the library string.h to the use of string void saisie( - char *cpointeur); // Prototypes of the three functions used in the program + char *cpointeur); // Prototypes of the three functions used in the program int compte(char *s); char *miroir(char *s); diff --git a/misc/pid.c b/misc/pid.c index 44a9b361..95c743ca 100644 --- a/misc/pid.c +++ b/misc/pid.c @@ -62,8 +62,9 @@ int main() struct pid controller = {.lastError = 0, .integral = 0}; // Take the controller gains from the user - printf("Please enter controller gains in format kP, kI, KD. For example, " - "\"1.2 2.1 3.2\"\n> "); + printf( + "Please enter controller gains in format kP, kI, KD. For example, " + "\"1.2 2.1 3.2\"\n> "); scanf("%f %f %f", &controller.kP, &controller.kI, &controller.kD); printf("Using kP: %f, kI: %f, kD: %f\n", controller.kP, controller.kI, controller.kD); diff --git a/misc/prime_factoriziation.c b/misc/prime_factoriziation.c index 8c18ef0f..7e466923 100644 --- a/misc/prime_factoriziation.c +++ b/misc/prime_factoriziation.c @@ -45,7 +45,6 @@ void destroy(Range); */ int main() { - int n = 0; /* for user input */ printf("\t\tPrim factoriziation\n\n"); diff --git a/misc/sudoku_solver.c b/misc/sudoku_solver.c index 41787c6e..5199afd1 100644 --- a/misc/sudoku_solver.c +++ b/misc/sudoku_solver.c @@ -83,8 +83,7 @@ int main() scanf("%d%d%d", &N, &R, &C); int a[M], i, j; for (i = 0; i < N; i++) - for (j = 0; j < N; j++) - scanf("%d", &a[i * N + j]); + for (j = 0; j < N; j++) scanf("%d", &a[i * N + j]); if (solve(a)) print(a); diff --git a/numerical_methods/gauss_elimination.c b/numerical_methods/gauss_elimination.c index 87daa410..18405ce6 100644 --- a/numerical_methods/gauss_elimination.c +++ b/numerical_methods/gauss_elimination.c @@ -65,7 +65,6 @@ int main(void) printf("\n"); for (i = 0; i < n; i++) { - printf("Enter Co-efficient Of Equations %d & Total --->>>\n", i + 1); for (j = 0; j <= n; j++) { diff --git a/numerical_methods/lagrange_theorem.c b/numerical_methods/lagrange_theorem.c index b7569fd0..6ad4530d 100644 --- a/numerical_methods/lagrange_theorem.c +++ b/numerical_methods/lagrange_theorem.c @@ -32,7 +32,6 @@ int main() p = 1.0; for (j = 0; j < n; j++) { - if (i != j) { p = p * (a - x[j]) / (x[i] - x[j]); diff --git a/numerical_methods/lu_decompose.c b/numerical_methods/lu_decompose.c index 3ede1220..22dd8f47 100644 --- a/numerical_methods/lu_decompose.c +++ b/numerical_methods/lu_decompose.c @@ -32,8 +32,7 @@ int lu_decomposition(double **A, double **L, double **U, int mat_size) { // Summation of L[i,j] * U[j,k] double lu_sum = 0.; - for (j = 0; j < row; j++) - lu_sum += L[row][j] * U[j][col]; + for (j = 0; j < row; j++) lu_sum += L[row][j] * U[j][col]; // Evaluate U[i,k] U[row][col] = A[row][col] - lu_sum; @@ -53,8 +52,7 @@ int lu_decomposition(double **A, double **L, double **U, int mat_size) // Summation of L[i,j] * U[j,k] double lu_sum = 0.; - for (j = 0; j < row; j++) - lu_sum += L[col][j] * U[j][row]; + for (j = 0; j < row; j++) lu_sum += L[col][j] * U[j][row]; // Evaluate U[i,k] L[col][row] = (A[col][row] - lu_sum) / U[row][row]; @@ -80,19 +78,19 @@ void display(double **A, int N) /** Main function */ int main(int argc, char **argv) { - int mat_size = 3; // default matrix size + int mat_size = 3; // default matrix size const int range = 10; const int range2 = range >> 1; if (argc == 2) mat_size = atoi(argv[1]); - srand(time(NULL)); // random number initializer + srand(time(NULL)); // random number initializer /* Create a square matrix with random values */ double **A = (double **)malloc(mat_size * sizeof(double *)); - double **L = (double **)malloc(mat_size * sizeof(double *)); // output - double **U = (double **)malloc(mat_size * sizeof(double *)); // output + double **L = (double **)malloc(mat_size * sizeof(double *)); // output + double **U = (double **)malloc(mat_size * sizeof(double *)); // output for (int i = 0; i < mat_size; i++) { // calloc so that all valeus are '0' by default diff --git a/numerical_methods/mean.c b/numerical_methods/mean.c index 63ae4b8c..c37dbbc0 100644 --- a/numerical_methods/mean.c +++ b/numerical_methods/mean.c @@ -29,8 +29,7 @@ int main(int argc, char **argv) } putchar('\n'); - for (i = 0; i < n; i++) - sum = sum + a[i]; + for (i = 0; i < n; i++) sum = sum + a[i]; mean = sum / (float)n; printf("\nMean :"); diff --git a/numerical_methods/qr_decompose.h b/numerical_methods/qr_decompose.h index 73513a39..55349f44 100644 --- a/numerical_methods/qr_decompose.h +++ b/numerical_methods/qr_decompose.h @@ -25,8 +25,7 @@ void print_matrix(double **A, /**< matrix to print */ { for (int row = 0; row < M; row++) { - for (int col = 0; col < N; col++) - printf("% 9.3g\t", A[row][col]); + for (int col = 0; col < N; col++) printf("% 9.3g\t", A[row][col]); putchar('\n'); } putchar('\n'); @@ -49,8 +48,7 @@ double vector_dot(double *a, double *b, int L) // parallelize on threads #pragma omp parallel for reduction(+ : mag) #endif - for (i = 0; i < L; i++) - mag += a[i] * b[i]; + for (i = 0; i < L; i++) mag += a[i] * b[i]; return mag; } @@ -88,8 +86,7 @@ double *vector_proj(double *a, double *b, double *out, int L) // parallelize on threads #pragma omp for #endif - for (i = 0; i < L; i++) - out[i] = scalar * b[i]; + for (i = 0; i < L; i++) out[i] = scalar * b[i]; return out; } @@ -112,8 +109,7 @@ double *vector_sub(double *a, /**< minuend */ // parallelize on threads #pragma omp for #endif - for (i = 0; i < L; i++) - out[i] = a[i] - b[i]; + for (i = 0; i < L; i++) out[i] = a[i] - b[i]; return out; } @@ -176,8 +172,7 @@ void qr_decompose(double **A, /**< input matrix to decompose */ } for (j = 0; j < i; j++) { - for (int k = 0; k < M; k++) - col_vector2[k] = Q[k][j]; + for (int k = 0; k < M; k++) col_vector2[k] = Q[k][j]; vector_proj(col_vector, col_vector2, col_vector2, M); vector_sub(tmp_vector, col_vector2, tmp_vector, M); } @@ -187,16 +182,13 @@ void qr_decompose(double **A, /**< input matrix to decompose */ // parallelize on threads #pragma omp for #endif - for (j = 0; j < M; j++) - Q[j][i] = tmp_vector[j] / mag; + for (j = 0; j < M; j++) Q[j][i] = tmp_vector[j] / mag; /* compute upper triangular values of R */ - for (int kk = 0; kk < M; kk++) - col_vector[kk] = Q[kk][i]; + for (int kk = 0; kk < M; kk++) col_vector[kk] = Q[kk][i]; for (int k = i; k < N; k++) { - for (int kk = 0; kk < M; kk++) - col_vector2[kk] = A[kk][k]; + for (int kk = 0; kk < M; kk++) col_vector2[kk] = A[kk][k]; R[i][k] = vector_dot(col_vector, col_vector2, M); } } @@ -206,4 +198,4 @@ void qr_decompose(double **A, /**< input matrix to decompose */ free(tmp_vector); } -#endif // QR_DECOMPOSE_H +#endif // QR_DECOMPOSE_H diff --git a/numerical_methods/qr_decomposition.c b/numerical_methods/qr_decomposition.c index f953b96b..3efeddc9 100644 --- a/numerical_methods/qr_decomposition.c +++ b/numerical_methods/qr_decomposition.c @@ -6,11 +6,11 @@ * \author [Krishna Vedala](https://github.com/kvedala) */ -#include "qr_decompose.h" #include #include #include #include +#include "qr_decompose.h" /** * main function @@ -24,8 +24,9 @@ int main(void) scanf("%u %u", &ROWS, &COLUMNS); if (ROWS < COLUMNS) { - fprintf(stderr, "Number of rows must be greater than or equal to " - "number of columns.\n"); + fprintf(stderr, + "Number of rows must be greater than or equal to " + "number of columns.\n"); return -1; } @@ -36,8 +37,7 @@ int main(void) A[i] = (double *)malloc(COLUMNS * sizeof(double)); for (int i = 0; i < ROWS; i++) - for (int j = 0; j < COLUMNS; j++) - scanf("%lf", &A[i][j]); + for (int j = 0; j < COLUMNS; j++) scanf("%lf", &A[i][j]); print_matrix(A, ROWS, COLUMNS); diff --git a/numerical_methods/qr_eigen_values.c b/numerical_methods/qr_eigen_values.c index 504cab0e..0b0d1bf0 100644 --- a/numerical_methods/qr_eigen_values.c +++ b/numerical_methods/qr_eigen_values.c @@ -5,12 +5,12 @@ * method. * \author [Krishna Vedala](https://github.com/kvedala) */ -#include "qr_decompose.h" #include #include #include #include #include +#include "qr_decompose.h" #ifdef _OPENMP #include #endif @@ -72,8 +72,7 @@ double **mat_mul(double **A, double **B, double **OUT, int R1, int C1, int R2, for (int j = 0; j < C2; j++) { OUT[i][j] = 0.f; - for (int k = 0; k < C1; k++) - OUT[i][j] += A[i][k] * B[k][j]; + for (int k = 0; k < C1; k++) OUT[i][j] += A[i][k] * B[k][j]; } return OUT; } @@ -144,8 +143,7 @@ double eigen_values(double **A, double *eigen_vals, int mat_size, while (fabs(A[num_eigs][num_eigs - 1]) > EPSILON) { last_eig = A[num_eigs][num_eigs]; - for (int i = 0; i < rows; i++) - A[i][i] -= last_eig; /* A - cI */ + for (int i = 0; i < rows; i++) A[i][i] -= last_eig; /* A - cI */ qr_decompose(A, Q, R, rows, columns); if (debug_print) @@ -158,8 +156,7 @@ double eigen_values(double **A, double *eigen_vals, int mat_size, } mat_mul(R, Q, A, columns, columns, rows, columns); - for (int i = 0; i < rows; i++) - A[i][i] += last_eig; /* A + cI */ + for (int i = 0; i < rows; i++) A[i][i] += last_eig; /* A + cI */ } /* store the converged eigen value */ @@ -209,13 +206,12 @@ void test1() { int mat_size = 2; double X[][2] = {{5, 7}, {7, 11}}; - double y[] = {15.56158, 0.384227}; // corresponding y-values + double y[] = {15.56158, 0.384227}; // corresponding y-values double eig_vals[2]; // The following steps are to convert a "double[][]" to "double **" double **A = (double **)malloc(mat_size * sizeof(double *)); - for (int i = 0; i < mat_size; i++) - A[i] = X[i]; + for (int i = 0; i < mat_size; i++) A[i] = X[i]; printf("------- Test 1 -------\n"); @@ -262,13 +258,12 @@ void test2() {0, -3, 3, -1, -3}, {-3, -1, -3, -3, 0}}; double y[] = {9.27648, -9.26948, 2.0181, -1.03516, - -5.98994}; // corresponding y-values + -5.98994}; // corresponding y-values double eig_vals[5]; // The following steps are to convert a "double[][]" to "double **" double **A = (double **)malloc(mat_size * sizeof(double *)); - for (int i = 0; i < mat_size; i++) - A[i] = X[i]; + for (int i = 0; i < mat_size; i++) A[i] = X[i]; printf("------- Test 2 -------\n"); @@ -306,7 +301,7 @@ int main(int argc, char **argv) if (argc == 2) mat_size = atoi(argv[1]); else - { // if invalid input argument is given run tests + { // if invalid input argument is given run tests test1(); test2(); printf("Usage: ./qr_eigen_values [mat_size]\n"); @@ -341,12 +336,10 @@ int main(int argc, char **argv) double dtime = eigen_values(A, eigen_vals, mat_size, 0); printf("Eigen vals: "); - for (i = 0; i < mat_size; i++) - printf("% 9.4g\t", eigen_vals[i]); + for (i = 0; i < mat_size; i++) printf("% 9.4g\t", eigen_vals[i]); printf("\nTime taken to compute: % .4g sec\n", dtime); - for (int i = 0; i < mat_size; i++) - free(A[i]); + for (int i = 0; i < mat_size; i++) free(A[i]); free(A); free(eigen_vals); return 0; diff --git a/numerical_methods/simpsons_1_3rd_rule.c b/numerical_methods/simpsons_1_3rd_rule.c index 7314231b..b73b4684 100644 --- a/numerical_methods/simpsons_1_3rd_rule.c +++ b/numerical_methods/simpsons_1_3rd_rule.c @@ -4,7 +4,7 @@ float f(float x) { return 1.0 + - x * x * x; // This is the expresion of the function to integrate? + x * x * x; // This is the expresion of the function to integrate? } int main() diff --git a/numerical_methods/variance.c b/numerical_methods/variance.c index 6bb8e84a..6db3bfbf 100644 --- a/numerical_methods/variance.c +++ b/numerical_methods/variance.c @@ -4,7 +4,6 @@ int main() { - int *ARRAY = NULL, ARRAY_LENGTH, i, TEMPORARY_ELEMENT, isSorted = 0; float MEAN = 0, VARIANCE = 0, STAND; @@ -12,26 +11,25 @@ int main() scanf("%d", &ARRAY_LENGTH); ARRAY = (int *)realloc( ARRAY, - ARRAY_LENGTH * (sizeof(int))); // We allocate the dedicated memory - for (i = 0; i < ARRAY_LENGTH; i++) // We generate the random numbers + ARRAY_LENGTH * (sizeof(int))); // We allocate the dedicated memory + for (i = 0; i < ARRAY_LENGTH; i++) // We generate the random numbers ARRAY[i] = rand() % 100; - printf("Random Numbers Generated are :\n"); // We display them - for (i = 0; i < ARRAY_LENGTH; i++) - printf("%d ", ARRAY[i]); + printf("Random Numbers Generated are :\n"); // We display them + for (i = 0; i < ARRAY_LENGTH; i++) printf("%d ", ARRAY[i]); - printf("\nSorted Data: "); // Then we sort it using Bubble Sort.. + printf("\nSorted Data: "); // Then we sort it using Bubble Sort.. while (!isSorted) - { // While our array's not sorted - isSorted = 1; // we suppose that it's sorted + { // While our array's not sorted + isSorted = 1; // we suppose that it's sorted for (i = 0; i < ARRAY_LENGTH - 1; i++) - { // then for each element of the array + { // then for each element of the array if (ARRAY[i] > ARRAY[i + 1]) - { // if the two elements aren't sorted - isSorted = 0; // it means that the array is not sorted - TEMPORARY_ELEMENT = ARRAY[i]; // and we switch these elements - // using TEMPORARY_ELEMENT + { // if the two elements aren't sorted + isSorted = 0; // it means that the array is not sorted + TEMPORARY_ELEMENT = ARRAY[i]; // and we switch these elements + // using TEMPORARY_ELEMENT ARRAY[i] = ARRAY[i + 1]; ARRAY[i + 1] = TEMPORARY_ELEMENT; } diff --git a/project_euler/problem_1/sol1.c b/project_euler/problem_1/sol1.c index c91a824a..3ffc03fa 100644 --- a/project_euler/problem_1/sol1.c +++ b/project_euler/problem_1/sol1.c @@ -14,12 +14,12 @@ int main() int t; printf("Enter number of times you want to try"); scanf("%d", &t); - while (t--) // while t > 0, decrement 't' before every iteration + while (t--) // while t > 0, decrement 't' before every iteration { unsigned long long N, p = 0, sum = 0; printf("Enter the value of N "); - scanf("%lld", &N); // Take input of N from user + scanf("%lld", &N); // Take input of N from user p = (N - 1) / 3; sum = ((3 * p * (p + 1)) / 2); @@ -28,8 +28,8 @@ int main() p = (N - 1) / 15; sum = sum - ((15 * p * (p + 1)) / 2); - printf("%lld\n", sum); // print the sum of all numbers that are - // multiples of 3 & 5 below N + printf("%lld\n", sum); // print the sum of all numbers that are + // multiples of 3 & 5 below N } return 0; } diff --git a/project_euler/problem_1/sol2.c b/project_euler/problem_1/sol2.c index d78c22d5..72ca678b 100644 --- a/project_euler/problem_1/sol2.c +++ b/project_euler/problem_1/sol2.c @@ -19,7 +19,7 @@ int main() scanf("%d", &n); int terms = (n - 1) / 3; - sum += ((terms) * (6 + (terms - 1) * 3)) / 2; // sum of an A.P. + sum += ((terms) * (6 + (terms - 1) * 3)) / 2; // sum of an A.P. terms = (n - 1) / 5; sum += ((terms) * (10 + (terms - 1) * 5)) / 2; terms = (n - 1) / 15; diff --git a/project_euler/problem_1/sol4.c b/project_euler/problem_1/sol4.c index 3477412c..27758016 100644 --- a/project_euler/problem_1/sol4.c +++ b/project_euler/problem_1/sol4.c @@ -19,7 +19,7 @@ int main() unsigned long long N, p = 0, sum = 0; printf("Enter the value of N "); - scanf("%lld", &N); // Take input of N from user + scanf("%lld", &N); // Take input of N from user for (int i = 0; i < N; i++) { if (i % 3 == 0 || i % 5 == 0) @@ -27,8 +27,8 @@ int main() sum = sum + i; } } - printf("%lld\n", sum); // print the sum of all numbers that are - // multiples of 3 & 5 below N + printf("%lld\n", sum); // print the sum of all numbers that are + // multiples of 3 & 5 below N } return 0; } diff --git a/project_euler/problem_13/sol1.c b/project_euler/problem_13/sol1.c index c1d05327..e5c1fd90 100644 --- a/project_euler/problem_13/sol1.c +++ b/project_euler/problem_13/sol1.c @@ -85,8 +85,7 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print) uint8_t end_pos; /* skip all initial zeros */ - while (number[start_pos] == 0) - start_pos--; + while (number[start_pos] == 0) start_pos--; /* if end_pos < 0, print all digits */ if (num_digits_to_print < 0) @@ -99,8 +98,7 @@ int print_number(uint8_t *number, uint8_t N, int8_t num_digits_to_print) return -1; } - for (int i = start_pos; i >= end_pos; i--) - putchar(number[i] + 0x30); + for (int i = start_pos; i >= end_pos; i--) putchar(number[i] + 0x30); putchar('\n'); diff --git a/project_euler/problem_16/sol1.c b/project_euler/problem_16/sol1.c index 68a12cac..1a93a546 100644 --- a/project_euler/problem_16/sol1.c +++ b/project_euler/problem_16/sol1.c @@ -54,8 +54,7 @@ int main(int argc, char **argv) } printf("2^%d = ", N); - for (int i = MAX_NUM_DIGITS - 1; i >= 0; i--) - putchar(digits[i] + 0x30); + for (int i = MAX_NUM_DIGITS - 1; i >= 0; i--) putchar(digits[i] + 0x30); printf("\n\t Sum: %d\t Num. digits: %lu\n", sum, MAX_NUM_DIGITS); free(digits); diff --git a/project_euler/problem_19/sol1.c b/project_euler/problem_19/sol1.c index 4e91ee25..90ea0f04 100644 --- a/project_euler/problem_19/sol1.c +++ b/project_euler/problem_19/sol1.c @@ -122,9 +122,10 @@ int main(int argc, char **argv) } } - printf("Total number of Sundays that happened on the 1st of a month in the " - "last century: %d\n", - count_sundays); + printf( + "Total number of Sundays that happened on the 1st of a month in the " + "last century: %d\n", + count_sundays); return 0; } diff --git a/project_euler/problem_2/so1.c b/project_euler/problem_2/so1.c index 2b73325a..de238ebb 100644 --- a/project_euler/problem_2/so1.c +++ b/project_euler/problem_2/so1.c @@ -25,7 +25,7 @@ int main() while (j <= n) { - if ((j & 1) == 0) // can also use(j%2 == 0) + if ((j & 1) == 0) // can also use(j%2 == 0) sum += j; temp = i; i = j; diff --git a/project_euler/problem_22/sol1.c b/project_euler/problem_22/sol1.c index bd8aee8d..dbb44d56 100644 --- a/project_euler/problem_22/sol1.c +++ b/project_euler/problem_22/sol1.c @@ -38,8 +38,7 @@ void shell_sort(char data[][MAX_NAME_LEN], int LEN) } } #ifdef DEBUG - for (i = 0; i < LEN; i++) - printf("%s\t", data[i]); + for (i = 0; i < LEN; i++) printf("%s\t", data[i]); #endif } @@ -63,8 +62,7 @@ void lazy_sort(char data[][MAX_NAME_LEN], int LEN) } } #ifdef DEBUG - for (i = 0; i < LEN; i++) - printf("%s\t", data[i]); + for (i = 0; i < LEN; i++) printf("%s\t", data[i]); #endif } diff --git a/project_euler/problem_23/sol1.c b/project_euler/problem_23/sol1.c index 308b0362..c2f3d1bc 100644 --- a/project_euler/problem_23/sol1.c +++ b/project_euler/problem_23/sol1.c @@ -117,9 +117,10 @@ int main(int argc, char **argv) } printf("Time taken: %.4g s\n", total_duration); - printf("Sum of numbers that cannot be represented as sum of two abundant " - "numbers : %lu\n", - sum); + printf( + "Sum of numbers that cannot be represented as sum of two abundant " + "numbers : %lu\n", + sum); return 0; } diff --git a/project_euler/problem_23/sol2.c b/project_euler/problem_23/sol2.c index 079fc9ab..a149010d 100644 --- a/project_euler/problem_23/sol2.c +++ b/project_euler/problem_23/sol2.c @@ -182,9 +182,10 @@ int main(int argc, char **argv) printf("Time taken for final sum: %.4g ms\nTotal Time taken: %.4g ms\n", t22, t1 + t22); printf("Memory used: %lu bytes\n", MAX_N >> 3); - printf("Sum of numbers that cannot be represented as sum of two abundant " - "numbers : %lu\n", - sum); + printf( + "Sum of numbers that cannot be represented as sum of two abundant " + "numbers : %lu\n", + sum); free(abundant_flags); diff --git a/project_euler/problem_25/sol1.c b/project_euler/problem_25/sol1.c index e5bef0bc..30bd697b 100644 --- a/project_euler/problem_25/sol1.c +++ b/project_euler/problem_25/sol1.c @@ -60,11 +60,9 @@ int print_number(unsigned char *number, int N) int start_pos = N - 1; /* skip all initial zeros */ - while (number[start_pos] == 0) - start_pos--; + while (number[start_pos] == 0) start_pos--; - for (int i = start_pos; i >= 0; i--) - putchar(number[i] + 0x30); + for (int i = start_pos; i >= 0; i--) putchar(number[i] + 0x30); return 0; } @@ -73,8 +71,7 @@ int print_number(unsigned char *number, int N) unsigned int get_digits(unsigned char *number) { unsigned int digits = MAX_DIGITS; - while (number[digits] == 0) - digits--; + while (number[digits] == 0) digits--; return digits; } diff --git a/project_euler/problem_26/sol1.c b/project_euler/problem_26/sol1.c index b5359ed2..66decd32 100644 --- a/project_euler/problem_26/sol1.c +++ b/project_euler/problem_26/sol1.c @@ -12,7 +12,7 @@ #endif #define MAX_DENO 2000 /**< limit of unit fractions */ -#define MAX_LEN \ +#define MAX_LEN \ (MAX_DENO + 10) /**< length of resulting recurring fraction number */ /** comparison function for use with internal `qsort` algorithm */ diff --git a/project_euler/problem_401/sol1.c b/project_euler/problem_401/sol1.c index ed260b32..7576f7db 100644 --- a/project_euler/problem_401/sol1.c +++ b/project_euler/problem_401/sol1.c @@ -55,25 +55,25 @@ uint64_t get_divisors(uint64_t N, uint64_t *D) // because after this, the pair of divisors will repeat themselves for (i = 1; i * i <= N + 1; i++) { - r = N % i; // get reminder + r = N % i; // get reminder // reminder = 0 if 'i' is a divisor of 'N' if (r == 0) { q = N / i; - if (!is_in(i, D, num)) // if divisor was already stored + if (!is_in(i, D, num)) // if divisor was already stored { D[num] = i; num++; } - if (!is_in(q, D, num)) // if divisor was already stored + if (!is_in(q, D, num)) // if divisor was already stored { D[num] = q; num++; } } - if (num == MAX_L) // limit of array reached, allocate more space + if (num == MAX_L) // limit of array reached, allocate more space D = (uint64_t *)realloc(D, MAX_L * sizeof(uint64_t) << 1); } return num; diff --git a/searching/fibonacci_search.c b/searching/fibonacci_search.c index 9bffb7d4..cc6b3554 100644 --- a/searching/fibonacci_search.c +++ b/searching/fibonacci_search.c @@ -4,9 +4,9 @@ int fibMonaccianSearch(int arr[], int x, int n) { /* Initialize fibonacci numbers */ - int fibMMm2 = 0; // (m-2)'th Fibonacci No. - int fibMMm1 = 1; // (m-1)'th Fibonacci No. - int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci + int fibMMm2 = 0; // (m-2)'th Fibonacci No. + int fibMMm1 = 1; // (m-1)'th Fibonacci No. + int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci /* fibM is going to store the smallest Fibonacci Number greater than or equal to n */ diff --git a/searching/interpolation_search.c b/searching/interpolation_search.c index 473eaac7..595f3d87 100644 --- a/searching/interpolation_search.c +++ b/searching/interpolation_search.c @@ -40,8 +40,7 @@ int main() int n = sizeof(arr) / sizeof(arr[0]); printf("Array: "); - for (int i = 0; i < n; i++) - printf("%d ", arr[i]); + for (int i = 0; i < n; i++) printf("%d ", arr[i]); printf("\nEnter the number to be searched: "); scanf("%d", &x); /* Element to be searched */ diff --git a/searching/linear_search.c b/searching/linear_search.c index aa9b27c8..6982512c 100644 --- a/searching/linear_search.c +++ b/searching/linear_search.c @@ -16,16 +16,16 @@ int main() { int n, i, v; printf("Enter the size of the array:\n"); - scanf("%d", &n); // Taking input for the size of Array + scanf("%d", &n); // Taking input for the size of Array int *a = (int *)malloc(n * sizeof(int)); printf("Enter the contents for an array of size %d:\n", n); for (i = 0; i < n; i++) - scanf("%d", &a[i]); // accepts the values of array elements until the - // loop terminates// + scanf("%d", &a[i]); // accepts the values of array elements until the + // loop terminates// printf("Enter the value to be searched:\n"); - scanf("%d", &v); // Taking input the value to be searched + scanf("%d", &v); // Taking input the value to be searched if (linearsearch(a, n, v)) printf("Value %d is in the array.\n", v); else diff --git a/searching/modified_binary_search.c b/searching/modified_binary_search.c index 3ddb834a..7a4d0039 100644 --- a/searching/modified_binary_search.c +++ b/searching/modified_binary_search.c @@ -1,7 +1,7 @@ #include #include -int n, m; // size of the matrix +int n, m; // size of the matrix // This function does Binary search for x in i-th row from j_low to j_high. void binarySearch(int **mat, int i, int j_low, int j_high, int x) @@ -28,7 +28,7 @@ void binarySearch(int **mat, int i, int j_low, int j_high, int x) // Function to perform binary search on the mid values of row to get the desired // pair of rows where the element can be found void modifiedBinarySearch(int **mat, int n, int m, int x) -{ // If Single row matrix +{ // If Single row matrix if (n == 1) { binarySearch(mat, 0, 0, m - 1, x); @@ -77,11 +77,10 @@ void modifiedBinarySearch(int **mat, int n, int m, int x) int main() { - int x; // element to be searched + int x; // element to be searched scanf("%d %d %d\n", &n, &m, &x); int **mat = (int **)malloc(n * sizeof(int *)); - for (x = 0; x < n; x++) - mat[x] = (int *)malloc(m * sizeof(int)); + for (x = 0; x < n; x++) mat[x] = (int *)malloc(m * sizeof(int)); for (int i = 0; i < n; i++) { @@ -92,8 +91,7 @@ int main() } modifiedBinarySearch(mat, n, m, x); - for (x = 0; x < n; x++) - free(mat[x]); + for (x = 0; x < n; x++) free(mat[x]); free(mat); return 0; } diff --git a/searching/pattern_search/boyer_moore_search.c b/searching/pattern_search/boyer_moore_search.c index 87ed5879..a803e032 100644 --- a/searching/pattern_search/boyer_moore_search.c +++ b/searching/pattern_search/boyer_moore_search.c @@ -9,11 +9,9 @@ void computeArray(char *pattern, int size, int arr[NUM_OF_CHARS]) { int i; - for (i = 0; i < NUM_OF_CHARS; i++) - arr[i] = -1; + for (i = 0; i < NUM_OF_CHARS; i++) arr[i] = -1; /* Fill the actual value of last occurrence of a character */ - for (i = 0; i < size; i++) - arr[(int)pattern[i]] = i; + for (i = 0; i < size; i++) arr[(int)pattern[i]] = i; } /* Boyer Moore Search algorithm */ void boyer_moore_search(char *str, char *pattern) @@ -27,8 +25,7 @@ void boyer_moore_search(char *str, char *pattern) while (shift <= (n - m)) { int j = m - 1; - while (j >= 0 && pattern[j] == str[shift + j]) - j--; + while (j >= 0 && pattern[j] == str[shift + j]) j--; if (j < 0) { printf("--Pattern is found at: %d\n", shift); diff --git a/searching/pattern_search/rabin_karp_search.c b/searching/pattern_search/rabin_karp_search.c index ea4d4cd2..c86a988b 100644 --- a/searching/pattern_search/rabin_karp_search.c +++ b/searching/pattern_search/rabin_karp_search.c @@ -13,8 +13,7 @@ void rabin_karp_search(char *str, char *pattern, int d, int q) int hash_p = 0; /* hash value for pattern */ /* h = pow(d, len_pat - 1) % q */ - for (i = 0; i < len_pat - 1; i++) - h = d * h % q; + for (i = 0; i < len_pat - 1; i++) h = d * h % q; /* Calculating hashing of pattern and the 1st window of text */ for (i = 0; i < len_pat; i++) { diff --git a/searching/ternary_search.c b/searching/ternary_search.c index 0447a4cb..bce2ebe0 100644 --- a/searching/ternary_search.c +++ b/searching/ternary_search.c @@ -6,7 +6,6 @@ int ternarySearch(int l, int r, int key, int ar[]) { if (r >= l) { - // Find the mid1 and mid2 int mid1 = l + (r - l) / 3; int mid2 = r - (r - l) / 3; @@ -28,19 +27,16 @@ int ternarySearch(int l, int r, int key, int ar[]) if (key < ar[mid1]) { - // The key lies in between l and mid1 return ternarySearch(l, mid1 - 1, key, ar); } else if (key > ar[mid2]) { - // The key lies in between mid2 and r return ternarySearch(mid2 + 1, r, key, ar); } else { - // The key lies in between mid1 and mid2 return ternarySearch(mid1 + 1, mid2 - 1, key, ar); } diff --git a/sorting/bead_sort.c b/sorting/bead_sort.c index a9d9510c..979bac68 100644 --- a/sorting/bead_sort.c +++ b/sorting/bead_sort.c @@ -5,7 +5,6 @@ /*Displays the array, passed to this method*/ void display(int *arr, int n) { - int i; for (i = 0; i < n; i++) { @@ -33,8 +32,7 @@ void bead_sort(int *a, int len) /* mark the beads */ for (i = 0; i < len; i++) - for (j = 0; j < a[i]; j++) - BEAD(i, j) = 1; + for (j = 0; j < a[i]; j++) BEAD(i, j) = 1; for (j = 0; j < max; j++) { @@ -45,8 +43,7 @@ void bead_sort(int *a, int len) BEAD(i, j) = 0; } /* mark bottom sum beads */ - for (i = len - sum; i < len; i++) - BEAD(i, j) = 1; + for (i = len - sum; i < len; i++) BEAD(i, j) = 1; } for (i = 0; i < len; i++) @@ -62,7 +59,7 @@ int main(int argc, const char *argv[]) { int n; printf("Enter size of array:\n"); - scanf("%d", &n); // E.g. 8 1 2 3 + scanf("%d", &n); // E.g. 8 1 2 3 printf("Enter the elements of the array\n"); int i; diff --git a/sorting/binary_insertion_sort.c b/sorting/binary_insertion_sort.c index 075bbe2b..a111e284 100644 --- a/sorting/binary_insertion_sort.c +++ b/sorting/binary_insertion_sort.c @@ -56,7 +56,7 @@ int main(int argc, const char *argv[]) { int n; printf("Enter size of array:\n"); - scanf("%d", &n); // E.g. 8 + scanf("%d", &n); // E.g. 8 printf("Enter the elements of the array\n"); int i; diff --git a/sorting/bogo_sort.c b/sorting/bogo_sort.c index 26f8fa35..c9e06e49 100644 --- a/sorting/bogo_sort.c +++ b/sorting/bogo_sort.c @@ -26,8 +26,7 @@ void shuffle(int *a, int n) void sort(int *a, int n) { - while (!check_sorted(a, n)) - shuffle(a, n); + while (!check_sorted(a, n)) shuffle(a, n); } int main() @@ -40,7 +39,6 @@ int main() scanf("%d", &numbers[i]); } sort(numbers, 6); - for (i = 0; i < 6; i++) - printf("%d ", numbers[i]); + for (i = 0; i < 6; i++) printf("%d ", numbers[i]); printf("\n"); } diff --git a/sorting/bubble_sort.c b/sorting/bubble_sort.c index 2a042985..de203329 100644 --- a/sorting/bubble_sort.c +++ b/sorting/bubble_sort.c @@ -5,7 +5,6 @@ /*Displays the array, passed to this method*/ void display(int *arr, int n) { - int i; for (i = 0; i < n; i++) { @@ -18,7 +17,6 @@ void display(int *arr, int n) /*Swap function to swap two values*/ void swap(int *first, int *second) { - int temp = *first; *first = *second; *second = temp; @@ -30,7 +28,6 @@ void swap(int *first, int *second) */ void bubbleSort(int *arr, int size) { - for (int i = 0; i < size - 1; i++) { for (int j = 0; j < size - 1 - i; j++) @@ -47,7 +44,7 @@ int main(int argc, const char *argv[]) { int n; printf("Enter size of array:\n"); - scanf("%d", &n); // E.g. 8 + scanf("%d", &n); // E.g. 8 printf("Enter the elements of the array\n"); int i; @@ -58,12 +55,12 @@ int main(int argc, const char *argv[]) } printf("Original array: "); - display(arr, n); // Original array : 10 11 9 8 4 7 3 8 + display(arr, n); // Original array : 10 11 9 8 4 7 3 8 bubbleSort(arr, n); printf("Sorted array: "); - display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 + display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 free(arr); return 0; diff --git a/sorting/bubble_sort_2.c b/sorting/bubble_sort_2.c index c141ba91..9e3c8000 100644 --- a/sorting/bubble_sort_2.c +++ b/sorting/bubble_sort_2.c @@ -6,7 +6,6 @@ int main() { - int i, arraySort[MAX] = {0}, isSort = FALSE, changePlace; /* For example diff --git a/sorting/bucket_sort.c b/sorting/bucket_sort.c index 9d65430c..8417a981 100644 --- a/sorting/bucket_sort.c +++ b/sorting/bucket_sort.c @@ -79,7 +79,6 @@ void BucketSort(int arr[]) node = buckets[i]; while (node) { - // precondition for avoiding out of bounds by the array assert(j < NARRAY); arr[j++] = node->data; @@ -125,7 +124,7 @@ struct Node *InsertionSort(struct Node *list) { struct Node *tmp; tmp = k; - k = k->next; // important for the while + k = k->next; // important for the while tmp->next = nodeList; nodeList = tmp; continue; @@ -144,7 +143,7 @@ struct Node *InsertionSort(struct Node *list) { struct Node *tmp; tmp = k; - k = k->next; // important for the while + k = k->next; // important for the while tmp->next = ptr->next; ptr->next = tmp; continue; @@ -152,7 +151,7 @@ struct Node *InsertionSort(struct Node *list) else { ptr->next = k; - k = k->next; // important for the while + k = k->next; // important for the while ptr->next->next = NULL; continue; } diff --git a/sorting/comb_sort.c b/sorting/comb_sort.c index 85d9a5b1..7d2234c5 100644 --- a/sorting/comb_sort.c +++ b/sorting/comb_sort.c @@ -1,16 +1,16 @@ #include #include -#define SHRINK 1.3 // suggested shrink factor value +#define SHRINK 1.3 // suggested shrink factor value void sort(int *numbers, int size) { int gap = size; - while (gap > 1) // gap = 1 means that the array is sorted + while (gap > 1) // gap = 1 means that the array is sorted { gap = gap / SHRINK; int i = 0; while ((i + gap) < size) - { // similiar to the Shell Sort + { // similiar to the Shell Sort if (numbers[i] > numbers[i + gap]) { int tmp = numbers[i]; @@ -25,8 +25,7 @@ void sort(int *numbers, int size) void display(int *array, int n) { int i; - for (i = 0; i < n; ++i) - printf("%d ", array[i]); + for (i = 0; i < n; ++i) printf("%d ", array[i]); printf("\n"); } @@ -36,8 +35,7 @@ int main() int *numbers = malloc(size * sizeof(int)); printf("Insert %d unsorted numbers: \n", size); int i; - for (i = 0; i < size; ++i) - scanf("%d", &numbers[i]); + for (i = 0; i < size; ++i) scanf("%d", &numbers[i]); printf("Initial array: "); display(numbers, size); sort(numbers, size); diff --git a/sorting/counting_sort.c b/sorting/counting_sort.c index a2927ef5..fff5aa26 100644 --- a/sorting/counting_sort.c +++ b/sorting/counting_sort.c @@ -28,14 +28,13 @@ int main() int *b = (int *)malloc((l + 1) * sizeof(int)); memset(b, 0, (l + 1) * sizeof(b[0])); - for (i = 0; i < n; i++) - b[a[i]]++; // hashing number to array index + for (i = 0; i < n; i++) b[a[i]]++; // hashing number to array index - for (i = 0; i < (l + 1); i++) // unstable , stabilized by prefix sum array + for (i = 0; i < (l + 1); i++) // unstable , stabilized by prefix sum array { if (b[i] > 0) { - while (b[i] != 0) // for case when number exists more than once + while (b[i] != 0) // for case when number exists more than once { printf("%d ", i); b[i]--; diff --git a/sorting/cycle_sort.c b/sorting/cycle_sort.c index 77cfcaa3..16fdf7df 100644 --- a/sorting/cycle_sort.c +++ b/sorting/cycle_sort.c @@ -5,7 +5,6 @@ // Displays the array, passed to this method void display(int *arr, int n) { - int i; for (i = 0; i < n; i++) { @@ -18,7 +17,6 @@ void display(int *arr, int n) // Swap function to swap two values void swap(int *first, int *second) { - int temp = *first; *first = *second; *second = temp; @@ -49,8 +47,7 @@ void cycleSort(int *arr, int n) continue; // ignore all duplicate elements - while (item == arr[pos]) - pos += 1; + while (item == arr[pos]) pos += 1; // put the item to it's right position if (pos != cycle_start) @@ -70,8 +67,7 @@ void cycleSort(int *arr, int n) pos += 1; // ignore all duplicate elements - while (item == arr[pos]) - pos += 1; + while (item == arr[pos]) pos += 1; // put the item to it's right position if (item != arr[pos]) @@ -86,10 +82,10 @@ void cycleSort(int *arr, int n) // Driver program to test above function int main() { - int n; // Size of array elements + int n; // Size of array elements printf("Enter size of array:\n"); - scanf("%d", &n); // E.g. 8 + scanf("%d", &n); // E.g. 8 printf("Enter the elements of the array\n"); int i; diff --git a/sorting/gnome_sort.c b/sorting/gnome_sort.c index 0e92f60e..973d2ff8 100644 --- a/sorting/gnome_sort.c +++ b/sorting/gnome_sort.c @@ -23,8 +23,7 @@ void sort(int *numbers, int size) void display(int *array, int n) { int i; - for (i = 0; i < n; ++i) - printf("%d ", array[i]); + for (i = 0; i < n; ++i) printf("%d ", array[i]); printf("\n"); } @@ -34,8 +33,7 @@ int main() int i; int *numbers = malloc(size * sizeof(int)); printf("Insert %d unsorted numbers: \n", size); - for (i = 0; i < size; ++i) - scanf("%d", &numbers[i]); + for (i = 0; i < size; ++i) scanf("%d", &numbers[i]); printf("Initial array: "); display(numbers, size); sort(numbers, size); diff --git a/sorting/insertion_sort.c b/sorting/insertion_sort.c index f9f87b8e..572ca3cd 100644 --- a/sorting/insertion_sort.c +++ b/sorting/insertion_sort.c @@ -39,7 +39,7 @@ int main(int argc, const char *argv[]) { int n; printf("Enter size of array:\n"); - scanf("%d", &n); // E.g. 8 + scanf("%d", &n); // E.g. 8 printf("Enter the elements of the array\n"); int i; diff --git a/sorting/merge_sort.c b/sorting/merge_sort.c index bbc00144..e2262b24 100644 --- a/sorting/merge_sort.c +++ b/sorting/merge_sort.c @@ -1,7 +1,7 @@ #include #include -void swap(int *a, int *b) // To swap the variables// +void swap(int *a, int *b) // To swap the variables// { int t; t = *a; @@ -9,7 +9,7 @@ void swap(int *a, int *b) // To swap the variables// *b = t; } -void merge(int a[], int l, int r, int n) // To merge // +void merge(int a[], int l, int r, int n) // To merge // { int *b = (int *)malloc(n * sizeof(int)); int c = l; @@ -47,8 +47,7 @@ void merge(int a[], int l, int r, int n) // To merge // }; } - for (c = l; c < r - l + 1; c++) - a[c] = b[c]; + for (c = l; c < r - l + 1; c++) a[c] = b[c]; } void merge_sort(int *a, int n, int l, int r) @@ -69,20 +68,18 @@ void merge_sort(int *a, int n, int l, int r) } } int main(void) -{ // main function// +{ // main function// int *a, n, i; scanf("%d", &n); a = (int *)malloc(n * sizeof(int)); for (i = 0; i < n; i++) { - scanf("%d", &a[i]); } merge_sort(a, n, 0, n - 1); for (i = 0; i < n; i++) { - printf(" %d", a[i]); } diff --git a/sorting/merge_sort_nr.c b/sorting/merge_sort_nr.c index 219822c4..fdbce7cd 100644 --- a/sorting/merge_sort_nr.c +++ b/sorting/merge_sort_nr.c @@ -44,17 +44,14 @@ void mergesort(int x[], int n) else temp[k++] = x[j++]; - while (i <= ub1) - temp[k++] = x[i++]; + while (i <= ub1) temp[k++] = x[i++]; - while (j <= ub2) - temp[k++] = x[j++]; + while (j <= ub2) temp[k++] = x[j++]; lb1 = ub2 + 1; } - for (i = 0; i <= ub2; i++) - x[i] = temp[i]; + for (i = 0; i <= ub2; i++) x[i] = temp[i]; size = size * 2; @@ -66,26 +63,23 @@ void mergesort(int x[], int n) void show(int x[], int n) { int i; - for (i = 0; i < n; i++) - printf("%d ", x[i]); + for (i = 0; i < n; i++) printf("%d ", x[i]); printf("\n\n"); } -int main() // main function +int main() // main function { int i, n, x[20]; printf("Enter the number of elements: "); scanf("%d", &n); printf("Enter the elements:\n"); - for (i = 0; i < n; i++) - scanf("%d", &x[i]); + for (i = 0; i < n; i++) scanf("%d", &x[i]); mergesort(x, n); printf("Sorted array is as shown:\n"); - for (i = 0; i < n; i++) - printf("%d ", x[i]); + for (i = 0; i < n; i++) printf("%d ", x[i]); return 0; } diff --git a/sorting/multikey_quick_sort.c b/sorting/multikey_quick_sort.c index 671b12ea..8d773286 100644 --- a/sorting/multikey_quick_sort.c +++ b/sorting/multikey_quick_sort.c @@ -18,11 +18,11 @@ #define min(a, b) ((a) <= (b) ? (a) : (b)) #endif -#define swap(a, b) \ - { \ - char *t = x[a]; \ - x[a] = x[b]; \ - x[b] = t; \ +#define swap(a, b) \ + { \ + char *t = x[a]; \ + x[a] = x[b]; \ + x[b] = t; \ } #define i2c(i) x[i][depth] @@ -98,11 +98,11 @@ void vecswap2(char **a, char **b, int n) } } -#define swap2(a, b) \ - { \ - t = *(a); \ - *(a) = *(b); \ - *(b) = t; \ +#define swap2(a, b) \ + { \ + t = *(a); \ + *(a) = *(b); \ + *(b) = t; \ } #define ptr2char(i) (*(*(i) + depth)) @@ -146,7 +146,7 @@ void ssort2(char **a, int n, int depth) pm = a + (n / 2); pn = a + (n - 1); if (n > 30) - { // On big arrays, pseudomedian of 9 + { // On big arrays, pseudomedian of 9 d = (n / 8); pl = med3(pl, pl + d, pl + 2 * d); pm = med3(pm - d, pm, pm + d); @@ -294,8 +294,7 @@ void insert2(char *s) void cleanup2() { int i; - for (i = 0; i < freen; i++) - free(freearr[i]); + for (i = 0; i < freen; i++) free(freearr[i]); } // Search Algorithms @@ -387,7 +386,6 @@ void nearsearch(Tptr p, char *s, int d) int main(int argc, char *argv[]) { - char *arr[NUMBER_OF_STRING] = {"apple", "cat", "boy"}; ssort1main(arr, NUMBER_OF_STRING); diff --git a/sorting/partition_sort.c b/sorting/partition_sort.c index b6da9be2..fc7a3430 100644 --- a/sorting/partition_sort.c +++ b/sorting/partition_sort.c @@ -48,8 +48,7 @@ void partitionSort(int arr[], int low, int high) void printArray(int arr[], int n) { int i; - for (i = 0; i < n; i++) - printf("%d ", arr[i]); + for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); } diff --git a/sorting/quick_sort.c b/sorting/quick_sort.c index dfd1ed9a..6a0c8768 100644 --- a/sorting/quick_sort.c +++ b/sorting/quick_sort.c @@ -4,7 +4,6 @@ /*Displays the array, passed to this method*/ void display(int arr[], int n) { - int i; for (i = 0; i < n; i++) { @@ -17,7 +16,6 @@ void display(int arr[], int n) /*Swap function to swap two values*/ void swap(int *first, int *second) { - int temp = *first; *first = *second; *second = temp; @@ -32,25 +30,23 @@ void swap(int *first, int *second) */ int partition(int arr[], int lower, int upper) { - int i = (lower - 1); - int pivot = arr[upper]; // Selects last element as the pivot value + int pivot = arr[upper]; // Selects last element as the pivot value int j; for (j = lower; j < upper; j++) { - if (arr[j] <= pivot) - { // if current element is smaller than the pivot + { // if current element is smaller than the pivot - i++; // increment the index of smaller element + i++; // increment the index of smaller element swap(&arr[i], &arr[j]); } } - swap(&arr[i + 1], &arr[upper]); // places the last element i.e, the pivot to - // its correct position + swap(&arr[i + 1], &arr[upper]); // places the last element i.e, the pivot + // to its correct position return (i + 1); } @@ -62,10 +58,8 @@ int partition(int arr[], int lower, int upper) */ void quickSort(int arr[], int lower, int upper) { - if (upper > lower) { - // partitioning index is returned by the partition method , partition // element is at its correct poition @@ -79,10 +73,9 @@ void quickSort(int arr[], int lower, int upper) int main() { - int n; printf("Enter size of array:\n"); - scanf("%d", &n); // E.g. 8 + scanf("%d", &n); // E.g. 8 printf("Enter the elements of the array\n"); int i; @@ -93,12 +86,12 @@ int main() } printf("Original array: "); - display(arr, n); // Original array : 10 11 9 8 4 7 3 8 + display(arr, n); // Original array : 10 11 9 8 4 7 3 8 quickSort(arr, 0, n - 1); printf("Sorted array: "); - display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 + display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 getchar(); return 0; } diff --git a/sorting/radix_sort.c b/sorting/radix_sort.c index 97bf43c9..364a3917 100644 --- a/sorting/radix_sort.c +++ b/sorting/radix_sort.c @@ -48,8 +48,7 @@ void RadixSort(int a[], int n) } divisor *= 10; - for (i = 0; i < n; i++) - printf("%d ", a[i]); + for (i = 0; i < n; i++) printf("%d ", a[i]); printf("\n"); } } @@ -66,8 +65,7 @@ int main() } RadixSort(a, n); printf("The sorted elements are :: "); - for (i = 0; i < n; i++) - printf("%d ", a[i]); + for (i = 0; i < n; i++) printf("%d ", a[i]); printf("\n"); return 0; } diff --git a/sorting/radix_sort_2.c b/sorting/radix_sort_2.c index d1902e2d..9d6cabb2 100644 --- a/sorting/radix_sort_2.c +++ b/sorting/radix_sort_2.c @@ -2,7 +2,7 @@ #include #include -#define range 10 // Range for integers is 10 as digits range from 0-9 +#define range 10 // Range for integers is 10 as digits range from 0-9 // Utility function to get the maximum value in ar[] int MAX(int *ar, int size) @@ -23,13 +23,11 @@ void countSort(int *arr, int n, int place) int *output = (int *)malloc(n * sizeof(int)); // Store count of occurences in freq[] - for (i = 0; i < n; i++) - freq[(arr[i] / place) % range]++; + for (i = 0; i < n; i++) freq[(arr[i] / place) % range]++; // Change freq[i] so that it contains the actual position of the digit in // output[] - for (i = 1; i < range; i++) - freq[i] += freq[i - 1]; + for (i = 1; i < range; i++) freq[i] += freq[i - 1]; // Build the output array for (i = n - 1; i >= 0; i--) @@ -40,8 +38,7 @@ void countSort(int *arr, int n, int place) // Copy the output array to arr[], so it contains numbers according to the // current digit - for (i = 0; i < n; i++) - arr[i] = output[i]; + for (i = 0; i < n; i++) arr[i] = output[i]; free(output); } @@ -51,7 +48,7 @@ void countSort(int *arr, int n, int place) max --- Maximum element in Array */ void radixsort2(int *arr, int n, - int max) // max is the maximum element in the array + int max) // max is the maximum element in the array { int mul = 1; while (max) @@ -64,8 +61,7 @@ void radixsort2(int *arr, int n, void display(int *arr, int N) { - for (int i = 0; i < N; i++) - printf("%d, ", arr[i]); + for (int i = 0; i < N; i++) printf("%d, ", arr[i]); putchar('\n'); } @@ -73,7 +69,7 @@ int main(int argc, const char *argv[]) { int n; printf("Enter size of array:\n"); - scanf("%d", &n); // E.g. 8 + scanf("%d", &n); // E.g. 8 printf("Enter the elements of the array\n"); int i; @@ -84,7 +80,7 @@ int main(int argc, const char *argv[]) } printf("Original array: "); - display(arr, n); // Original array : 10 11 9 8 4 7 3 8 + display(arr, n); // Original array : 10 11 9 8 4 7 3 8 int max; max = MAX(arr, n); @@ -92,7 +88,7 @@ int main(int argc, const char *argv[]) radixsort2(arr, n, max); printf("Sorted array: "); - display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 + display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 free(arr); return 0; diff --git a/sorting/selection_sort.c b/sorting/selection_sort.c index 52dcfb39..dd8982be 100644 --- a/sorting/selection_sort.c +++ b/sorting/selection_sort.c @@ -5,7 +5,6 @@ /*Displays the array, passed to this method*/ void display(int *arr, int n) { - int i; for (i = 0; i < n; i++) { @@ -18,7 +17,6 @@ void display(int *arr, int n) /*Swap function to swap two values*/ void swap(int *first, int *second) { - int temp = *first; *first = *second; *second = temp; @@ -30,7 +28,6 @@ void swap(int *first, int *second) */ void selectionSort(int *arr, int size) { - for (int i = 0; i < size; i++) { int min_index = i; @@ -49,7 +46,7 @@ int main(int argc, const char *argv[]) { int n; printf("Enter size of array:\n"); - scanf("%d", &n); // E.g. 8 + scanf("%d", &n); // E.g. 8 printf("Enter the elements of the array\n"); int i; @@ -60,12 +57,12 @@ int main(int argc, const char *argv[]) } printf("Original array: "); - display(arr, n); // Original array : 10 11 9 8 4 7 3 8 + display(arr, n); // Original array : 10 11 9 8 4 7 3 8 selectionSort(arr, n); printf("Sorted array: "); - display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 + display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 free(arr); return 0; diff --git a/sorting/shaker_sort.c b/sorting/shaker_sort.c index ea8d9c9c..9f9a0b6d 100644 --- a/sorting/shaker_sort.c +++ b/sorting/shaker_sort.c @@ -31,11 +31,9 @@ int main() scanf("%d", &n); int *arr = (int *)malloc(n * sizeof(int)); int i; - for (i = 0; i < n; i++) - scanf("%d ", &arr[i]); + for (i = 0; i < n; i++) scanf("%d ", &arr[i]); shakersort(arr, n); - for (i = 0; i < n; i++) - printf("%d ", arr[i]); + for (i = 0; i < n; i++) printf("%d ", arr[i]); free(arr); return 0; } diff --git a/sorting/shell_sort.c b/sorting/shell_sort.c index d9e7a304..4c22c3d4 100644 --- a/sorting/shell_sort.c +++ b/sorting/shell_sort.c @@ -4,7 +4,8 @@ #define ELEMENT_NR 20000 #define ARRAY_LEN(x) (sizeof(x) / sizeof((x)[0])) -const char *notation = "Shell Sort Big O Notation:\ +const char *notation = + "Shell Sort Big O Notation:\ \n--> Best Case: O(n log(n)) \ \n--> Average Case: depends on gap sequence \ \n--> Worst Case: O(n)"; @@ -13,8 +14,7 @@ void show_data(int arr[], int len) { int i; - for (i = 0; i < len; i++) - printf("%3d ", arr[i]); + for (i = 0; i < len; i++) printf("%3d ", arr[i]); printf("\n"); } @@ -47,8 +47,7 @@ int main(int argc, char *argv[]) double time_spent; srand(time(NULL)); - for (i = 0; i < ELEMENT_NR; i++) - array[i] = rand() % range + 1; + for (i = 0; i < ELEMENT_NR; i++) array[i] = rand() % range + 1; size = ARRAY_LEN(array); diff --git a/sorting/shell_sort2.c b/sorting/shell_sort2.c index c6ae4851..bf471592 100644 --- a/sorting/shell_sort2.c +++ b/sorting/shell_sort2.c @@ -13,8 +13,7 @@ /** Helper function to print array values */ void show_data(int *arr, long len) { - for (long i = 0; i < len; i++) - printf("%3d ", arr[i]); + for (long i = 0; i < len; i++) printf("%3d ", arr[i]); printf("\n"); } @@ -38,10 +37,10 @@ void shell_sort(int *array, long LEN) long i, j, g; for (g = 0; g < gap_len; g++) - { // for each gap + { // for each gap int gap = gaps[g]; for (i = gap; i < LEN; i++) - { // from gap position to the end + { // from gap position to the end int tmp = array[i]; for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap) @@ -50,8 +49,7 @@ void shell_sort(int *array, long LEN) } } #ifdef DEBUG - for (i = 0; i < LEN; i++) - printf("%s\t", data[i]); + for (i = 0; i < LEN; i++) printf("%s\t", data[i]); #endif } @@ -66,21 +64,21 @@ int main(int argc, char *argv[]) fprintf(stderr, "Usage: ./shell_sort [number of values]\n"); int *array = (int *)malloc(size * sizeof(int)); - int range = 500; // range of array values + int range = 500; // range of array values double time_spent; - srand(time(NULL)); // initialize random number generator + srand(time(NULL)); // initialize random number generator for (i = 0; i < size; i++) // fill array with random integers array[i] = rand() % range + 1; - show_data(array, size); // show array before sorting - clock_t t1 = clock(); // start timer - shell_sort(array, size); // sort the array - clock_t t2 = clock(); // end timer + show_data(array, size); // show array before sorting + clock_t t1 = clock(); // start timer + shell_sort(array, size); // sort the array + clock_t t2 = clock(); // end timer printf("Data Sorted\n"); - show_data(array, size); // display array after sorting + show_data(array, size); // display array after sorting printf("Time spent sorting: %.4g s\n", (t2 - t1) / CLOCKS_PER_SEC); diff --git a/sorting/stooge_sort.c b/sorting/stooge_sort.c index f7ef862f..0072adb7 100644 --- a/sorting/stooge_sort.c +++ b/sorting/stooge_sort.c @@ -7,8 +7,7 @@ int main() printf("How many elements do you want to sort: "); scanf("%d", &n); - for (i = 0; i < n; i++) - scanf(" %d", &arr[i]); + for (i = 0; i < n; i++) scanf(" %d", &arr[i]); stoogesort(arr, 0, n - 1); printf("Sorted array : \n"); for (i = 0; i < n; i++)