Question

In: Computer Science

In C, take the following programming that initializes 2 arrays and swaps them and displays the...

In C, take the following programming that initializes 2 arrays and swaps them and displays the initial input and output and modify it so that it uses different functions for the swapping and printing, so that the main method only has variables and function calls.

#include<stdio.h>

int main()
{
    const int n = 5;

    int firstArray[n], secondArray[n];

    int i, k=0, temp;

    for(i=0, k=0; k<n; i+=2, k++){

        firstArray[k] = i;

        secondArray[k] = i+1;
}
    printf("Before Swap\n");

    for(i=0; i<n; i++)
    {
        printf("firstArray[%d] = %d, secondArray[%d] = %d\n", i, firstArray[i], i, secondArray[i]);
    }
    //swapping

    for(i=0; i<n; i++)
    {
        temp = firstArray[i];

        firstArray[i] = secondArray[i];

        secondArray[i] = temp;
    }
    printf("After Swap\n");

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

        printf("firstArray[%d] = %d, secondArray[%d] = %d\n", i, firstArray[i], i, secondArray[i]);
    }
    return 0;
}

Solutions

Expert Solution

CODE

#include<stdio.h>

void swap(int *firstArray, int *secondArray, int n) {

for(int i=0; i<n; i++)

{

int temp = firstArray[i];

firstArray[i] = secondArray[i];

secondArray[i] = temp;

}

}

void print(int firstArray[], int secondArray[], int n) {

for(int i=0; i<n; i++)

{

printf("firstArray[%d] = %d, secondArray[%d] = %d\n", i, firstArray[i], i, secondArray[i]);

}

}

int main()

{

const int n = 5;

int firstArray[n], secondArray[n];

int i, k=0, temp;

for(i=0, k=0; k<n; i+=2, k++){

firstArray[k] = i;

secondArray[k] = i+1;

}

printf("Before Swap\n");

print(firstArray, secondArray, n);

swap(firstArray, secondArray, n);

printf("After Swap\n");

print(firstArray, secondArray, n);

return 0;

}


Related Solutions

In C programming: Write a program that initializes an array-of-double and then copies the contents of...
In C programming: Write a program that initializes an array-of-double and then copies the contents of the array into another arrays. To make the copy, use a function with array notation. This function takes two arguments the name of the target array and the number of elements to be copied. That is, the function calls would look like this, given the following declarations: double source[5] ={1.1, 2.2, 3.3., 4.4, 5.5}; double target1[5]; double target2[5]; copyarr(source, target1, 5);
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one...
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one dimensional array, Ages, which will hold 4 ages. Each age is an int.        b. Write a for loop and print, in reverse order the 4 values stored in memory assuming that the ages in the previous question have already been entered with a space between each value. Use subscript notation.                                     short cnt; c. Do the same as above, but use pointer...
Java Programming Write a program that displays the following pattern *                         *       &nbsp
Java Programming Write a program that displays the following pattern *                         *          *          * *          *          *          *          *          *          *          *          *          *          *          *             *          *          *          *          *                         *          *          *                                     * Printing Pattern A * ** *** **** ***** ****** ******* Printing Pattern B ******* ****** ***** **** *** ** * Printing Pattern C * ** *** **** ***** ****** *******
C++ Given 2 int arrays that are related, sort them correspondingly. EXAMPLE: int arr1[] = {84,...
C++ Given 2 int arrays that are related, sort them correspondingly. EXAMPLE: int arr1[] = {84, 4, 30, 66, 15}; int arr2[] = {7, 5, 2, 9, 10}; SORTED ANSWER: 4 - 5 15 - 10 30 - 2 66 - 9 84 - 7 IN C++
C++, Take this 3 pseudocode and transform them into C++ then implement them making a quick...
C++, Take this 3 pseudocode and transform them into C++ then implement them making a quick sort program using the function of the pseudocode that you changed to C++, also in the main the user need to put the array that he wants to do quick sort with. plz comment everything you do in details this is for a presentation 1. A first draft of pseudocode for the quick sort algorithm follows: // Sorts theArray[first..last]. quickSort(theArray: ItemArray, first: integer, last:...
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
C# Programming; Create a Windows Form Application that displays a scrollable list of 10 random integers...
C# Programming; Create a Windows Form Application that displays a scrollable list of 10 random integers in the range of 1 to 100. The form should also have (1) an Add button (and input textbox) for the user to add a new number to the list, (2) a Delete button to delete the current selected integer, (3) a Sort button to sort the list, (4) a Reverse button to display the list in reverse order, and (5) Display Multiple (and...
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
C programming 4. Exploring Pointers and Use of Arrays [15 pts] In a shell environment, programs...
C programming 4. Exploring Pointers and Use of Arrays [15 pts] In a shell environment, programs are often executed using command lines arguments. For example: g++ -o cm03_la01 cm03_la01.c. In C such program can be develop using a standard for which the main program has two parameters as follows: int main (int argc, char * argv[ ]) { /*program here */ } 3 Because of the equivalence between pointers and arrays, the above C code can also be written as...
Complete the following exercises using C programming language. Take screenshots of the code and its output...
Complete the following exercises using C programming language. Take screenshots of the code and its output where specified and paste them into in a well-labeled Word document for submission. Scenario Assume you are the CIO of an organization with three different IT department locations with separate costs. You want a program to perform simple IT expenditure calculations. Your IT expenditure target is $35,000 per site. Site expenditures: Site 1 – $35,000. Site 2 – $37,500. Site 3 – $42,500. Exercise...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT