Step 1. Call by Values - Swap Function 1. Declare a function named swap on the top of your main to accept two int variables and returns void. 2. Write your main function as follow: int main() ( int x = 2; int y = 14; printf("Before swap method: x=%d, y=%d.\n", x,y); swap(x,y); printf("After swap method: x=%d, y=%d.\n", x,y); return 0; } 3. Develop swap function to swap two variables. void swap (int a, int b) ( //write a code to swap a and b. printf("In swap function a=%d, b=%d.\n", a,b); return; Output sample: Before swap method: x=14, y=2. In swap function a=14, b=2. After swap method: x=2, y=14. Step 2. Call by Reference. As you saw in step 1, to effectively exchange the values of two variables we can't use a function as call by values. 1. Change the function declaration at the top of the main functon to accept two pointers to int. 2. Change the swap function header to accept two pointers to int as a parameter list. 3. Change the swap function's body to work on two pointers to exchange the values of two variables.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Hello. Please answer the attached C programming question correctly by fulfilling all of the steps on the 2 pics. Please do not use very advanced syntax to solve the problem. 

*If correctly fulfill and do all of the steps correctly, I will give you a thumbs up. Thanks.

### Step 1. Call by Values - Swap Function

1. **Declare a function** named `swap` on the top of your main to accept two `int` variables and return `void`.
   
2. **Write your main function** as follows:

    ```c
    int main()
    {
        int x = 2;
        int y = 14;
        printf("Before swap method: x=%d, y=%d.\n", x, y);
        swap(x, y);
        printf("After swap method: x=%d, y=%d.\n", x, y);
        
        return 0;
    }
    ```

3. **Develop swap function** to swap two variables.

    ```c
    void swap(int a, int b)
    {
        // write a code to swap a and b.
        printf("In swap function a=%d, b=%d.\n", a, b);
        return;
    }
    ```

### Output Sample:

```
Before swap method: x=14, y=2.
In swap function a=14, b=2.
After swap method: x=2, y=14.
```

### Step 2. Call by Reference

As you saw in step 1, to effectively exchange the values of two variables we can’t use a function as call by values.

1. **Change the function declaration** at the top of the main function to accept two pointers to `int`.

2. **Change the swap function header** to accept two pointers to `int` as a parameter list.

3. **Change the swap function’s body** to work on two pointers to exchange the values of two variables.
Transcribed Image Text:### Step 1. Call by Values - Swap Function 1. **Declare a function** named `swap` on the top of your main to accept two `int` variables and return `void`. 2. **Write your main function** as follows: ```c int main() { int x = 2; int y = 14; printf("Before swap method: x=%d, y=%d.\n", x, y); swap(x, y); printf("After swap method: x=%d, y=%d.\n", x, y); return 0; } ``` 3. **Develop swap function** to swap two variables. ```c void swap(int a, int b) { // write a code to swap a and b. printf("In swap function a=%d, b=%d.\n", a, b); return; } ``` ### Output Sample: ``` Before swap method: x=14, y=2. In swap function a=14, b=2. After swap method: x=2, y=14. ``` ### Step 2. Call by Reference As you saw in step 1, to effectively exchange the values of two variables we can’t use a function as call by values. 1. **Change the function declaration** at the top of the main function to accept two pointers to `int`. 2. **Change the swap function header** to accept two pointers to `int` as a parameter list. 3. **Change the swap function’s body** to work on two pointers to exchange the values of two variables.
## Swapping Function in C Programming

This example illustrates how to implement a swapping function in C to interchange the values of two variables using a function.

### Code Explanation:

The code provided aims to define a `swap` function that exchanges the values of two variables and then uses this function within the `main` function.

1. **Function Declaration:**
   ```c
   void swap(-----, -----);
   ```

2. **Main Function:**
   ```c
   int main()
   {
       int x = 2;
       int y = 14;
       printf("Before swap method: x=%d, y=%d.\n", x, y);
       // Call swap function
       printf("After swap method: x=%d, y=%d.\n", x, y);
       return 0;
   }
   ```

3. **Swap Function Definition:**
   ```c
   void swap(-----, -----)
   {
       // Write the code to exchange the values of the parameter values
       printf("In swap function a=%d, b=%d.\n", -----);
       return;
   }
   ```

### Output Sample:
```plain
Before swap method: x=2, y=14.
In swap function a=14, b=2.
After swap method: x=14, y=2.
```

### Steps to Implement:
1. **Function Declaration:**
   - The `swap` function should accept two pointers to integers to enable the swap of variables within the function.
   ```c
   void swap(int *a, int *b);
   ```

2. **Main Function Implementation:**
   - Define two integer variables `x` and `y`.
   - Print their values before the swap.
   - Call the `swap` function with the addresses of `x` and `y` to swap their values.
   - Print the values after the swap.
   ```c
   int main()
   {
       int x = 2;
       int y = 14;
       printf("Before swap method: x=%d, y=%d.\n", x, y);
       // Call swap function
       swap(&x, &y);
       printf("After swap method: x=%d, y=%d.\n", x, y);
       return 0;
   }
   ```

3. **Swap Function Implementation:**
   - Implement the logic
Transcribed Image Text:## Swapping Function in C Programming This example illustrates how to implement a swapping function in C to interchange the values of two variables using a function. ### Code Explanation: The code provided aims to define a `swap` function that exchanges the values of two variables and then uses this function within the `main` function. 1. **Function Declaration:** ```c void swap(-----, -----); ``` 2. **Main Function:** ```c int main() { int x = 2; int y = 14; printf("Before swap method: x=%d, y=%d.\n", x, y); // Call swap function printf("After swap method: x=%d, y=%d.\n", x, y); return 0; } ``` 3. **Swap Function Definition:** ```c void swap(-----, -----) { // Write the code to exchange the values of the parameter values printf("In swap function a=%d, b=%d.\n", -----); return; } ``` ### Output Sample: ```plain Before swap method: x=2, y=14. In swap function a=14, b=2. After swap method: x=14, y=2. ``` ### Steps to Implement: 1. **Function Declaration:** - The `swap` function should accept two pointers to integers to enable the swap of variables within the function. ```c void swap(int *a, int *b); ``` 2. **Main Function Implementation:** - Define two integer variables `x` and `y`. - Print their values before the swap. - Call the `swap` function with the addresses of `x` and `y` to swap their values. - Print the values after the swap. ```c int main() { int x = 2; int y = 14; printf("Before swap method: x=%d, y=%d.\n", x, y); // Call swap function swap(&x, &y); printf("After swap method: x=%d, y=%d.\n", x, y); return 0; } ``` 3. **Swap Function Implementation:** - Implement the logic
Expert Solution
steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Knowledge Booster
Types of Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education