Question

C++ Write a function SwapArrayEnds() that swaps the first and last elements of the function's array...

C++

Write a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The array's size may differ from 4.

#include <iostream>
using namespace std;

/* Your solution goes here */

int main() {
   const int SORT_ARR_SIZE = 4;
   int sortArray[SORT_ARR_SIZE];
   int i = 0;

   sortArray[0] = 10;
   sortArray[1] = 20;
   sortArray[2] = 30;
   sortArray[3] = 40;

   SwapArrayEnds(sortArray, SORT_ARR_SIZE);

   for (i = 0; i < SORT_ARR_SIZE; ++i) {
      cout << sortArray[i] << " ";
   }
   cout << endl;

   return 0;
}

.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot of the code:

Output:

40 20 30 10

Code to copy:

//Include this header file while using visual studio.

#include "stdafx.h"

#include <iostream>

using namespace std;

//Define the function to swap the first and last

//element of the array.

void SwapArrayEnds(int sortArray[], int SORT_ARR_SIZE)

{

       //Define the temporary variable.

       int t;

       //Swap the first and last element of the array.

       t = sortArray[0];

       sortArray[0] = sortArray[SORT_ARR_SIZE - 1];

       sortArray[SORT_ARR_SIZE - 1] = t;

}

int main() {

       const int SORT_ARR_SIZE = 4;

       int sortArray[SORT_ARR_SIZE];

       int i = 0;

       sortArray[0] = 10;

       sortArray[1] = 20;

       sortArray[2] = 30;

       sortArray[3] = 40;

       SwapArrayEnds(sortArray, SORT_ARR_SIZE);

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

              cout << sortArray[i] << " ";

       }

       cout << endl;

       system("pause");

       return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++ Write a function SwapArrayEnds() that swaps the first and last elements of the function's array...
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 method swapArrayEnds0 that swaps the first and last elements of its array parameter. Ex:...

    Write a method swapArrayEnds0 that swaps the first and last elements of its array parameter. Ex: sortArray-10, 20, 30, 40 becomes (40, 20, 30, 10). The array's size may differ from 4. 1 import java.util.Scanner; 3 public class ModifyArray 4 5 Display array values 6 public void displayValues(intarrayVals) 7 int i; for (i-o; i < arrayvals.length; ++?) { 10 System.out.printCarrayValsCi] +); System.out.println("); 12 13 14 15/ Your solution goes here*/ 16

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • C++ Write a loop that subtracts 1 from each element in lowerScores. If the element was...

    C++ Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. #include <iostream> using namespace std; int main() { const int SCORES_SIZE = 4; int lowerScores[SCORES_SIZE]; int i; for (i = 0; i < SCORES_SIZE; ++i) { cin >> lowerScores[i]; } /* Your solution goes here */ for (i = 0; i < SCORES_SIZE;...

  • in java please fix code between 14 and 20 to make the code compile Write a...

    in java please fix code between 14 and 20 to make the code compile Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10). The array's size may differ from 4. Display ari ay Values 1 pas [ pa: 6 public void displayValues(int [] arrayVals) { 7 int i; 8 9 for (i = 0; i < arrayVals.length; ++i) { 19 System.out.print(arrayVals[i] +...

  • Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array...

    Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...

  • using C++ only. The findFirstZero function is supposed to find the first element in an array...

    using C++ only. The findFirstZero function is supposed to find the first element in an array whose value is zero, and sets the parameter  p to point to that element, so the caller can know the location of that element holding the value zero. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only; you must not change the  main routine below in any way. As a result of your fixing...

  • Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements....

    Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 2, 1, 2} and matchValue is 2 , then numMatches should be 3. Your code will be tested with the following values: * matchValue: 2, userValues: {2, 2, 1, 2} (as in the example program above) * matchValue: 0, userValues: {0, 0, 0, 0} * matchValue: 50, userValues: {10, 20, 30, 40} (Notes) #include <iostream> #include <vector> using namespace...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

  • Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an...

    Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....

  • PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw...

    PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...

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
ADVERTISEMENT