Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is written in C. Write a program that swaps the elements of an arra

ID: 3696436 • Letter: T

Question

This program is written in C.

Write a program that swaps the elements of an array pairwise. Start from the left of the array, take 2 elements at a time and swap the two elements. Continue in this fashion until you reach the end of the array. Declare an integer array of size 10 and place random numbers in the range [0 -9]. Print the original array, pairwise swap the elements of the array and print the final array after swap. Consider the following example. After pairwise swap yoy get the following array In the array first two numbers 1 and 7 are swapped, 4 and 0 are swapped, 9 and 4 are swapped, 8 and 8 are swapped and 2 and 4 are swapped. Sample execution of the program is given below Original array 1 7 4 0 9 4 8 8 2 4 After pairwise swap 7 1 0 4 4 9 8 8 4 2

Explanation / Answer

/* C program to pairwise swap elements in a given linked list */

#include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node *next;

};

void swap(int *a, int *b);

/* Function to pairwise swap elements of a linked list */

void pairWiseSwap(struct node *head)

{

    struct node *temp = head;

    /* Traverse further only if there are at-least two nodes left */

    while (temp != NULL && temp->next != NULL)

    {

        /* Swap data of node with its next node's data */

        swap(&temp->data, &temp->next->data);

        /* Move temp by 2 for the next pair */

        temp = temp->next->next;

    }

}

/* Function to swap two integers */

void swap(int *a, int *b)

{

    int temp;

    temp = *a;

    *a = *b;

    *b = temp;

}

/* Function to add a node at the begining of Linked List */

void push(struct node** head_ref, int new_data)

{

    struct node* new_node =

              (struct node*) malloc(sizeof(struct node));

new_node->data = new_data;

    new_node->next = (*head_ref);

(*head_ref)    = new_node;

}

/* Function to print nodes in a given linked list */

void printList(struct node *node)

{

    while (node != NULL)

    {

        printf("%d ", node->data);

       node = node->next;

    }

}

/* Driver program to test above function */

int main()

{

    struct node *start = NULL;

    /* The constructed linked list is:

     1->2->3->4->5 */

    push(&start, 5);

    push(&start, 4);

    push(&start, 3);

    push(&start, 2);

    push(&start, 1);

    printf("Linked list before calling pairWiseSwap() ");

    printList(start);

pairWiseSwap(start);

printf(" Linked list after calling pairWiseSwap() ");

printList(start);

return 0;

}

/* C program to pairwise swap elements in a given linked list */

#include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node *next;

};

void swap(int *a, int *b);

/* Function to pairwise swap elements of a linked list */

void pairWiseSwap(struct node *head)

{

    struct node *temp = head;

    /* Traverse further only if there are at-least two nodes left */

    while (temp != NULL && temp->next != NULL)

    {

        /* Swap data of node with its next node's data */

        swap(&temp->data, &temp->next->data);

        /* Move temp by 2 for the next pair */

        temp = temp->next->next;

    }

}

/* Function to swap two integers */

void swap(int *a, int *b)

{

    int temp;

    temp = *a;

    *a = *b;

    *b = temp;

}

/* Function to add a node at the begining of Linked List */

void push(struct node** head_ref, int new_data)

{

    struct node* new_node =

              (struct node*) malloc(sizeof(struct node));

new_node->data = new_data;

    new_node->next = (*head_ref);

(*head_ref)    = new_node;

}

/* Function to print nodes in a given linked list */

void printList(struct node *node)

{

    while (node != NULL)

    {

        printf("%d ", node->data);

       node = node->next;

    }

}

/* Driver program to test above function */

int main()

{

    struct node *start = NULL;

    /* The constructed linked list is:

     1->2->3->4->5 */

    push(&start, 5);

    push(&start, 4);

    push(&start, 3);

    push(&start, 2);

    push(&start, 1);

    printf("Linked list before calling pairWiseSwap() ");

    printList(start);

pairWiseSwap(start);

printf(" Linked list after calling pairWiseSwap() ");

printList(start);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote