<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Maxwell Githinji</title>
    <description>The latest articles on DEV Community by Maxwell Githinji (@max_githinji).</description>
    <link>https://dev.to/max_githinji</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1493164%2F2fa89f20-451a-4fc8-92f8-d250e9df66ff.jpg</url>
      <title>DEV Community: Maxwell Githinji</title>
      <link>https://dev.to/max_githinji</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/max_githinji"/>
    <language>en</language>
    <item>
      <title>C programming: Strings, arrays, and pointers.</title>
      <dc:creator>Maxwell Githinji</dc:creator>
      <pubDate>Mon, 20 May 2024 20:52:02 +0000</pubDate>
      <link>https://dev.to/max_githinji/c-programming-strings-arrays-and-pointers-23h5</link>
      <guid>https://dev.to/max_githinji/c-programming-strings-arrays-and-pointers-23h5</guid>
      <description>&lt;p&gt;In this read, we are going to explore strings, arrays, and pointers. Additionally, we are going to write a block of C code script to help visualize these concepts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strings&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unfortunately, unlike other programming languages like Python, Ruby, JavaScript, etc. C does not offer a direct approach to initializing a string. This would ideally mean that it does not have a data type string, instead, it has a data type character. This means that a string in C is mostly initialized as an array of characters or directly using pointers. To initialize a strings, you can follow the below guide or example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// using Array to initialize an Array

char students[ ] = { "Max", "Brian", "Steve" };

// using Array to initialize a pointer

char *students = { "Max", "Brian", "Steve" };

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Arrays&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In C, an array can simply be defined as a set of elements of the same data type. Examples of how to initialize a C array can be done as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data_type array_name [size] = {value1, value2, value3, ...};

// example, let's initialize an array with the name num and it holds 10 numbers

int num[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// example, let's initialize the same array named num but without determining the array size.

int num[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};


// example, let's initialize an array with float data types

float temperatures[5] = {98.6, 99.5, 100.2, 97.8, 101.3};


// example, let's initialize an array of characters.

char name[3] = "Max";

char nameTwo = "Brian";

// example, let's initialize an array with the name students and it holds names of students as 2D array of characters.

char students[3][10] = {"Brian", "Max", "Steve"}; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To access elements in an array is important as it allows you to decide what you want to do with the data stored in these arrays - this may range from displaying the data to mutating the data or the array as a whole. For this article, we will not delve into using loops to access values or data used in arrays, instead, the example below will offer a basic example that shows how to access an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

int main() {
    // Declare an array of integers with 5 elements
    int myArray[5] = {10, 20, 30, 40, 50};

    // Access and print elements of the array
    printf("Element 0: %d\n", myArray[0]); // Output: Element 0: 10
    printf("Element 1: %d\n", myArray[1]); // Output: Element 1: 20
    printf("Element 2: %d\n", myArray[2]); // Output: Element 2: 30
    printf("Element 3: %d\n", myArray[3]); // Output: Element 3: 40
    printf("Element 4: %d\n", myArray[4]); // Output: Element 4: 50

    return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Pointers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In C, pointers are used to store other variables. These variables can have different data types ranging from integers to floats to char ( characters ). Apart from variables, pointer can also hold arrays, function, and other pointer as well. &lt;br&gt;
To initialize poiters to store various data variables as well as an array, you can follow the examples below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void intPointers(void) {
    // Initialize an integer, float, and char variable

    int num = 10;

    float myFloat = 10.1;

    char character = "A";

    char name[] = "Max";

    int arr[3] = {1, 2, 3};

    // Initialize pointers to store the addresses of the variables

    int *numPointer = &amp;amp;num;         // Pointer to an integer

    float *myFloatPointer = &amp;amp;myFloat; // Pointer to a float

    char *myCharacterPointer = character;     // Pointer to a character

    char *myNamePointer = name;     // Pointer to a char array (string)

    char *myArrayPointer = arr; // Pointer to an int array

    // Initialize an array of strings as a pointer directly

    char *students = { "Max", "Brian", "Steve" };
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you would like to initialize a pointer that holds to call to an already initialized function, you can follow the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void helloWord(void) {
   printf("Hello world");
}

int main(void) {

   void (*func_ptr)() = helloWorld; // Pointer to a function

   func_ptr();  // Calling the function through the pointer

   return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, to store another pointer within another pointer, you can approach it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void intPointers(void) {
    // Initialize an integer, float, and char variable

    int num = 10;


    // Initialize pointer for the variable num

    int *numPointer = &amp;amp;num;         // Pointer to an integer

    // Initialize pointer for the pointer

    int *pointerForMyPointer = &amp;amp;numPointer;
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Bubble Sort Algorithm</title>
      <dc:creator>Maxwell Githinji</dc:creator>
      <pubDate>Mon, 20 May 2024 20:39:25 +0000</pubDate>
      <link>https://dev.to/max_githinji/bubble-sort-algorithm-4j82</link>
      <guid>https://dev.to/max_githinji/bubble-sort-algorithm-4j82</guid>
      <description>&lt;p&gt;Sorting algorithms are important as they help the programmer to arrange or re-arrange elements depending on their desired format. There are a number of sorting algorithms, among them are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Bubble sort.&lt;/li&gt;
&lt;li&gt;Selection sort.&lt;/li&gt;
&lt;li&gt;Insertion sort.&lt;/li&gt;
&lt;li&gt;Merge sort.&lt;/li&gt;
&lt;li&gt;Quick sort.&lt;/li&gt;
&lt;li&gt;Heap sort&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this article, we are going to discuss the bubble sort algorithm. First, let's define and understand how a bubble sort algorithm works. Bubble sort algorithm just like other sorting algorithms, it's mainly concerned with organizing or re-organizing elements. Bubble sort does this by using the ideology of comparison. This would mean that its a step by step approach- for instance, to organize an array with elements "a", "b", and "c", the algorithm would compare element "a" to element "b" and depending on the programmer's desired or defined format, a swap might happen.&lt;br&gt;
Below are examples of code scripts for python, javascript, and C showing how to produce the algorithm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bubble sort-algorithm using Python.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def bubble_sort(arr):
    n = len(arr)
    # Traverse through all array elements
    for i in range(n):
        # Track if any elements were swapped in this iteration
        swapped = False
        # Last i elements are already in place, so inner loop can avoid checking them
        for j in range(0, n-i-1):
            # Traverse the array from 0 to n-i-1
            # Swap if the element found is greater than the next element
            if arr[j] &amp;gt; arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swapped = True
        # If no elements were swapped in the inner loop, then the array is already sorted
        if not swapped:
            break
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Bubble sort-algorithm using JavaScript.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function bubbleSort(arr) {
    let n = arr.length;
    let swapped;

    // Outer loop to iterate over the entire array
    for (let i = 0; i &amp;lt; n; i++) {
        swapped = false;

        // Inner loop to compare adjacent elements
        for (let j = 0; j &amp;lt; n - i - 1; j++) {
            // Swap if the element found is greater than the next element
            if (arr[j] &amp;gt; arr[j + 1]) {
                let temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = true;
            }
        }

        // If no elements were swapped, break out of the loop
        if (!swapped) {
            break;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Bubble sort-algorithm using C.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;stdio.h&amp;gt;

// Function to implement bubble sort
void bubbleSort(int arr[], int n) {
    int i, j, temp;
    int swapped;

    // Outer loop to iterate over the entire array
    for (i = 0; i &amp;lt; n-1; i++) {
        swapped = 0;

        // Inner loop to compare adjacent elements
        for (j = 0; j &amp;lt; n-i-1; j++) {
            // Swap if the element found is greater than the next element
            if (arr[j] &amp;gt; arr[j+1]) {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
                swapped = 1;
            }
        }

        // If no elements were swapped, break out of the loop
        if (swapped == 0)
            break;
    }
}

// Function to print an array
void printArray(int arr[], int size) {
    int i;
    for (i = 0; i &amp;lt; size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

// Main function to test the bubble sort algorithm
int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr)/sizeof(arr[0]);

    printf("Unsorted array: \n");
    printArray(arr, n);

    bubbleSort(arr, n);

    printf("Sorted array: \n");
    printArray(arr, n);

    return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you enjoyed the read, you can follow me on:&lt;br&gt;
Github: &lt;a href="https://github.com/MuthonduG"&gt;https://github.com/MuthonduG&lt;/a&gt;&lt;br&gt;
Linkedin: &lt;a href="https://www.linkedin.com/in/maxwell-githinji-954422258/"&gt;https://www.linkedin.com/in/maxwell-githinji-954422258/&lt;/a&gt; &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
