Question

2 write a void function that accepts two integer pointers and swaps the two integers pointed to by the pointers write the complete function including the function header (15 pts)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Swap function

/*Function Declaration*/

void swap(int* , int*);

/*Function Definition*/

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

   int temp;
   temp = *a;
   *a = *b;
   *b = temp;
}

Explanation:

In the above code snippet,

The return type is void and the swap function accepts two integer pointers, *a and *b.

Temperory variable 'temp' is used to swap the integers pointed by pointers.

Program.c

#include<stdio.h>
void swap(int* , int*);
void swap(int *a, int *b){
  
   int temp;
   temp = *a;
   *a = *b;
   *b = temp;
}

void main(){
  
   int value = 2, list = 8;
   int i;
   printf("\nBefore swap\n\n%d : %d\n",value,list);
  
   swap(&value,&list);
  
   printf("\nAfter swap\n\n%d : %d\n",value,list);
}

//Output

Add a comment
Know the answer?
Add Answer to:
Write a void function that accepts two integer pointers and swaps the two integers pointed to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program to add and subtract any two given integer numbers using pointers

    1- Write a C program to add and subtract any two given integer numbers using pointers.  2- Write a C program to find the factorial using a function and pointers.  3- Write a C program to find the square of an Integer number using a function and pointers.  4- Write a C program to find the area and perimeter of a rectangle using a function and pointers.  5- Write a C program to find the larger of two integers numbers using...

  • Given the following C function prototype that accepts two integer arguments. Complete the function and return...

    Given the following C function prototype that accepts two integer arguments. Complete the function and return 1 if a > b , return -1 if a < b and return 0 if a is equal to b. C function to complete int compare(int a, int b) Write a full c program that has the header required and will accept the user entry.

  • C++ Programming 1. Write a function (header and body) that accepts an integer as an argument...

    C++ Programming 1. Write a function (header and body) that accepts an integer as an argument and returns that number squared. 2. Write the code that will fill an integer array named multiples with 10 integers from 5 to 50 that are multiples of five. (This should NOT be in the form of an initialization statement.) 3. Write the code that will display the contents of the integer array named multiples. Recall: the size of the array is 10. 4....

  • Requirements: You must use pointers to process all arrays. Code a C function that accepts pointers...

    Requirements: You must use pointers to process all arrays. Code a C function that accepts pointers to two arrays of integers and their respective sizes. This function should check and count how many matchings entries in both arrays and return the count back to main for display. (important! Matchings entries do not have to be located in the same position and you are required to assume that the two arrays are of different sizes…also assume no duplicate within either arrays)

  • In C write a function called find_unique (). This function accepts a pointe integers, which consists...

    In C write a function called find_unique (). This function accepts a pointe integers, which consists of numbers that are greater than or equal to 0. The function searches the array for the unique integer. Not all arrays will have a unique integer, but if they do, then there will only be one unique number. A unique number is defined as the integer in the array that does not have at least one other matching value. If a unique number...

  • Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts...

    Function name: triangular Parameters: integer Returns: a list Description: Write a function called triangular that accepts one integer parameter that returns a list with the same number of terms as the value of the parameter. If the parameter is zero or a negative number, you should return an empty list A triangular number is the sum of a positive integer and all the integers before it. The series is as follows: 1, 3, 6, 10, 15, 21,

  • Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e....

    Assignment: Write a C function that accepts the pointer to a matrix of integer numbers (i.e. a two-dimensional array), number of rows and number of columns as input and prints the matrix after rotating it 90 degrees clockwise. Example: void rotate-matrix (int* matrix, int row, int column) Inputs: row 4, column-6 and the pointer to the matrix below: 2 3 6 5 8 0 Output:

  • In C++ cout<<"" << NO std:cout Write a function that accepts two integer arrays of the...

    In C++ cout<<"" << NO std:cout Write a function that accepts two integer arrays of the same size. The first array will have numbers and the second array will be filled out inside the function. The function should find all numbers in the array that are greater than or equal to the average and fill out the second array with these numbers. You need to design the function.Comments as well please because i am super confused. Thank you!

  • c++ write a boolean function that accepts two integer arrays of size 10 as input parameters,...

    c++ write a boolean function that accepts two integer arrays of size 10 as input parameters, the function should return true if there are any similar values between the two arrays and shall return false if not, if there are similar values, print out on screen

  • Function with Pointer (C++) 1. The function should be named "myFunction" which takes two pointers to...

    Function with Pointer (C++) 1. The function should be named "myFunction" which takes two pointers to chars and returns void (nothing). 2. The value pointed in the first argument should be copied into the value of the second pointer (and the second to the first). 3. The pointer itself is not copied, just the char it points to. Thanks in advance!

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT