From 34c4f2eafa98a6ce6c091ef95015b96f446316eb Mon Sep 17 00:00:00 2001 From: Hizenberg Date: Wed, 7 Feb 2024 22:39:51 +0530 Subject: [PATCH] Assignment2 --- Assignment2/array_iterator.c | 47 ++++++++++++ Assignment2/exercise2q.c | 141 +++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 Assignment2/array_iterator.c create mode 100644 Assignment2/exercise2q.c diff --git a/Assignment2/array_iterator.c b/Assignment2/array_iterator.c new file mode 100644 index 0000000..4263ea0 --- /dev/null +++ b/Assignment2/array_iterator.c @@ -0,0 +1,47 @@ +/* + * ===================================================================================== + * + * Filename: array_iterator.c + * + * Description: + * + * Version: 1.0 + * Created: 07/02/24 07:51:54 PM IST + * Revision: none + * Compiler: gcc + * + * Author: YOUR NAME (), + * Organization: + * + * ===================================================================================== + */ + +#include + +#include + + + +#define ITERATE_ARRAY_BEGIN(arrptr, array_size, start_index, scansize, out_index) { \ + unsigned int cnt; \ + for( cnt = 0, out_index = start_index ; cnt < scansize ; cnt++ , out_index = (out_index+1)%array_size ){ +#define ITERATE_ARRAY_END }} + + +int + +main(int argc, char **argv){ + + unsigned int arr[10] = {1,2,3,4,5,6,7,8,9,10}; + + unsigned int i; + + ITERATE_ARRAY_BEGIN(arr, 10, 5, 10, i) { + + printf("arr[%u] = %u\n", i, arr[i]); + + } ITERATE_ARRAY_END; + + return 0; + +} diff --git a/Assignment2/exercise2q.c b/Assignment2/exercise2q.c new file mode 100644 index 0000000..6134357 --- /dev/null +++ b/Assignment2/exercise2q.c @@ -0,0 +1,141 @@ +#include +#include +#include +#include + +#define MAX_NBRS 5 +#define NAME_SIZE 32 + +typedef struct node_{ + + char node_name[NAME_SIZE]; + struct node_ *nbrs[MAX_NBRS]; +} node_t ; + +void +add_nbr(node_t *node1, node_t *node2){ + + int i = 0; + for(; i < MAX_NBRS; i++){ + if(node1->nbrs[i]) continue; + node1->nbrs[i] = node2; + return; + } + /*Do not try to add more than MAX_NBRS*/ + assert(0); +} + +node_t * +create_new_node(char *name){ + + node_t *node = calloc(1, sizeof(node_t)); + strncpy(node->node_name, name, NAME_SIZE); + node->node_name[NAME_SIZE] = '\0'; + /*rest of the members is already NULL as + * we have used calloc*/ + return node; +} + +#define ITERATE_NBRS_BEGIN(node, nbrnode) \ +{ \ + for( unsigned int i = 0 ; i < 5 && node->nbrs[i] != NULL ; i++ ){ \ + nbrnode = node->nbrs[i]; + /**/ + +#define ITERATE_NBRS_END }} + /**/ + +#define ITERATE_ONE_HOP_NBRS_BEGIN(node, nbrnode) \ +{ \ + for( unsigned int j = 0 ; j < 5 && node->nbrs[j] != NULL ; j++ ){ \ + for( unsigned int i = 0 ; i < 5 && node->nbrs[j]->nbrs[i] != NULL ; i++ ){ \ + if( node->nbrs[j]->nbrs[i] == node ) continue; \ + nbrnode = node->nbrs[j]->nbrs[i]; + + /**/ + +#define ITERATE_ONE_HOP_NBRS_END }}} + /**/ + +int +main(int argc, char **argv){ + +/*Problem 2 : In thie Question, I have created a graph as shown in the diagram below.*/ + +/*Let us create the following graph now*/ +/* + A-------------B------------C---------H + | | | + | | | + | | | + | | | + | | | + E-------------D------------F---------G +*/ + + /*First I have created the nodes of the graph*/ + node_t *A = create_new_node("A"); + node_t *B = create_new_node("B"); + node_t *C = create_new_node("C"); + node_t *D = create_new_node("D"); + node_t *E = create_new_node("E"); + node_t *F = create_new_node("F"); + node_t *G = create_new_node("G"); + node_t *H = create_new_node("H"); + + + /*Now i am setting up the edges connecting two nodes of the graph using function add_nbr(). + * Result is - Graph will be created exactly same as drawn above*/ + add_nbr(A,B); + add_nbr(A,E); + + add_nbr(B,A); + add_nbr(B,D); + add_nbr(B,C); + + add_nbr(C,B); + add_nbr(C,F); + add_nbr(C,H); + + add_nbr(D,B); + add_nbr(D,E); + add_nbr(D,F); + + add_nbr(E,A); + add_nbr(E,D); + + add_nbr(F,C); + add_nbr(F,D); + add_nbr(F,G); + + add_nbr(G,F); + + add_nbr(H,C); + + node_t *nbr = NULL; + printf("Nbrs of A : \n"); + ITERATE_NBRS_BEGIN(A, nbr){ + + printf("Nbr name = %s\n", nbr->node_name); + } ITERATE_NBRS_END; + + printf("Nbrs of H : \n"); + ITERATE_NBRS_BEGIN(H, nbr){ + + printf("Nbr name = %s\n", nbr->node_name); + } ITERATE_NBRS_END; + + printf("Nbrs of D : \n"); + ITERATE_NBRS_BEGIN(D, nbr){ + + printf("Nbr name = %s\n", nbr->node_name); + } ITERATE_NBRS_END; + + printf("One hop Nbrs of A : \n"); + ITERATE_ONE_HOP_NBRS_BEGIN(A, nbr){ + + printf("Nbr name = %s\n", nbr->node_name); + } ITERATE_ONE_HOP_NBRS_END; + + return 0; +}