Purchase Solution

Introduction to C - Swapping and Toggling

Not what you're looking for?

Ask Custom Question

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 .

Purchase this Solution

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.

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 ...

Purchase this Solution


Free BrainMass Quizzes
C++ Operators

This quiz tests a student's knowledge about C++ operators.

C# variables and classes

This quiz contains questions about C# classes and variables.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.