Explore BrainMass

Explore BrainMass

    Introduction to C - Swapping and Toggling

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    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 .

    © BrainMass Inc. brainmass.com September 27, 2022, 4:56 pm ad1c9bdddf
    https://brainmass.com/computer-science/c/introduction-swapping-toggling-457182

    Attachments

    SOLUTION This solution is FREE courtesy of BrainMass!

    <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 's value to i . The original value of i , which was saved in temp , can now be assigned to j.</B>

    Here, we have to use a single temporary variable to do the swapping. This is actually all that is required, because once we assign the value of <I>i</I> to <I>temp</I>, we can safely overwrite the <I>i</I> with the value of <I>j</I> (because that value is already stored in <I>temp</I>). And as <I>i</I> now holds the original value of <I>j</I>, we can copy the original value of <I>i</I> from <I>temp</I> to <I>j</I>:

    <I>
    temp = i;
    i = j;
    j = temp;
    </I>
    </Li>
    <Li><B>Given two int variables, firstPlaceWinner and secondPlaceWinner , write some code that swaps their values. Declare any additional variables as necessary, but do not redeclare firstPlaceWinner and secondPlaceWinner.</B>

    This is basically the same as the previous one, with <I>firstPlaceWinner</I> and <I>secondPlaceWinner</I> in place of <I>i</I> and <I>j</I>. And we can similarly use a temporary variable (which must be declared first) to swap the values:

    <I>
    int temp;

    temp = firstPlaceWinner;
    firstPlaceWinner = secondPlaceWinner;
    secondPlaceWinner = temp;
    </I>
    </Li>
    <Li><B>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.</B>

    There are two ways of doing this in C - one, using the if-else control structure, the other, using the conditional operator <I>? : </I>. I will show both ways:

    Using if-else
    <I>
    if(onOffSwitch == 0)
    onOffSwitch = 1;
    else
    onOffSwitch = 0;
    </I>
    Here, we first check whether onOffSwitch is false, that is, 0. If so, we change its value to true, that is, 1. Otherwise, its value is changed to 0 (false).

    Using conditional operator
    <I>
    onOffSwitch = (onOffSwitch == 0 ? 1 : 0);
    </I>
    The syntax of the conditional operator is <I>condition ? val1 : val2</I>. When <I>condition</I> is true, the value <I>val1</I> is taken, and if <I>condition</I> is false, the value <I>val2</I> is taken. In the above code, the condition is <I>onOffSwitch == 0</I>. If that condition is true (meaning, <I>onOffSwitch</I> has value false), the operator returns the value 1 (true), which is then assigned the <I>onOffSwitch</I>. Otherwise, the value assigned is 0.
    </Li>
    <Li><B>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 .</B>

    Here, no if-else is required, because all that is asked is an <B>expression</B> that evaluates to true or false. No action needs to be taken depending on whether it actually is true or false. So we need an expression involving the logical operators of C - namely <I>&&</I> (and), <I>||</I> (or), and <I>!</I> (not). <I>yearsWithCompany</I> should be less than 5, that is, <I>yearsWithCompany < 5</I>. Also, <I>department</I> should <B>not</B> be 99, that is, <I>department != 99</I>. Combining these using <I>&&</I>, the required expression is:

    <I>yearsWithCompany < 5 && department != 99</I>
    </Ol>

    I hope the explanation is clear enough. Please feel free to ask me for more explanation wherever necessary.

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

    © BrainMass Inc. brainmass.com September 27, 2022, 4:56 pm ad1c9bdddf>
    https://brainmass.com/computer-science/c/introduction-swapping-toggling-457182

    ADVERTISEMENT