Explore BrainMass
Share

# Introduction to C - Swapping and Toggling

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

1 Given two int variables, i and j , which have been declared and initialized, and two other int variables, itemp and jtemp , which have been declared, write some code that swaps the values in i and j by copying their values to itemp and jtemp , respectively, and then copying itemp and jtemp to j and i , respectively.

2. Given three already declared int variables, i , j , and temp , write some code that swaps the values in i and j . Use temp to hold the value of i and then assign j 's value to i . The original value of i , which was saved in temp , can now be assigned to j.

3. Given three already declared int variables, i , j , and temp , write some code that swaps the values in i and j . Use temp to hold the value of i and then assign j 's value to i . The original value of i , which was saved in temp , can now be assigned to j .

4. Write a statement that toggles the value of onOffSwitch . That is, if onOffSwitch is false, its value is changed to true; if onOffSwitch is true, its value is changed to false.

5. Given the integer variables yearsWithCompany and department , write an expression that evaluates to true if yearsWithCompany is less than 5 and department is not equal to 99 .

https://brainmass.com/computer-science/c/introduction-swapping-toggling-457182

#### Solution Preview

<Ol>
<Li><B>Given two int variables, i and j , which have been declared and initialized, and two other int variables, itemp and jtemp , which have been declared, write some code that swaps the values in i and j by copying their values to itemp and jtemp , respectively, and then copying itemp and jtemp to j and i , respectively.</B>

The problem states that all the variables to be used have already been declared (and initialized, where necessary). To copy the values of <I>i</I> and <I>j</I> to <I>itemp</I> and <I>jtemp</I>, use assignment statements. Now, <I>itemp</I> contains the original value of <I>i</I>, and <I>jtemp</I> contains the original value of <I>j</I>. Then, again use assignment statements, but this time, to copy the value <I>itemp</I> to <I>j</I>, and the value of <I>jtemp</I> to <I>i</I>:

<I>
itemp = i;
jtemp = j;
j = itemp;
i = jtemp;
</I>
</Li>
<Li><B>Given three already declared int variables, i , j , and temp , write some code that swaps the values in i and j . Use temp to hold the value of i and then assign j ...

#### Solution Summary

Five elementary C programming problems are solved - three dealing with swapping of values stored in variables, one with toggling value stored in variable, and one with a conditional expression.

\$2.19