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

Must be written in C Write a function swap that takes as arguments two pointers

ID: 3704288 • Letter: M

Question

Must be written in C

Write a function swap that takes as arguments two pointers to integers. This function should swap the values pointed to by the pointers passed to the function. So, if there were two pointers, a and b, which point to integers 5 and 7, respectively, then after the function should run, then a should point to an integer with value 7 and b should point to an integer with a value of 5 Write a program that prompts a user to enter 15 integers. The program should store those integers in arn array. Then use the function you wrote to swap the 0th element with the 7th, the 8th with the 3rd, and the 14th with the 0th (in that order). The program should then print the values stored by the array after the swaps. The output of the program should look like this: Enter 15 integers: 0 1 2 3 4 5 678 9 10 11 12 13 14 Swapped array: 14 12 845 60 3 9 10 11 12 13 7 Algorithm Design Process: 1. Let's write the function first: a. What should the function prototype look like? b. What variables are needed inside the function? c. How can we test the function with a simple program'?

Explanation / Answer

#include <stdio.h>

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

int t = *a;

*a = *b;

*b=t;

}

int main()

{

int a[15],i;

printf("Enter 15 integers: ");

for(i=0;i<15;i++) {

scanf("%d", &a[i]);

}

swap(&a[0],&a[7]);

swap(&a[8],&a[3]);

swap(&a[14],&a[0]);

printf("Swapped array: ");

for(i=0;i<15;i++) {

printf("%d "' a[i]);

}

printf(" ");

return 0;

}

Output:

$gcc - 0 main *.c

$main

Enter 15 integers:

Swappeed array:

14 1 2 8 4 5 6 0 3 9 10 11 12 13 7