id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

orion.a.smith@gmail.com apcsa-arrays > apcsaArraysSwapFirstElements
prev  |  next  |  chance

This function swaps the FIRST two elements of a given array of int values, and returns the modified array. Note that this function returns a whole array, not just an int. You could just mutate the elements of and return nums, if you want. Other problems might require you to make and alter a copy of an array, but changing the insides of the parameter object is authorized, here. Precondition: nums will contain at least two elements.


SWAPPING ELEMENTS

When swapping two elements (call them element Jamal and element Rosa) of an array, copy the value of one of them (let's say Jamal) to a temporary variable.  Then, overwrite Jamal with the value of element Rosa, and finally overwrite Rosa with the value of Jamal (stored in the temp variable).

apcsaArraysSwapFirstElements([3, 4]) → [4, 3]
apcsaArraysSwapFirstElements([5, 8]) → [8, 5]
apcsaArraysSwapFirstElements([9, 2, 4, 6]) → [2, 9, 4, 6]

...Save, Compile, Run (ctrl-enter)

public int[] apcsaArraysSwapFirstElements(int[] nums) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 210

Copyright Nick Parlante 2017 - privacy