Question
1. Rewrite the following code to use the conditional operator rather than using an if-else statement. if (a==b) x = a; else x = c;
1. Rewrite the following code to use the conditional operator rather than using an if-else statement. if (a==b) x = a; else x = c; 2. The following C code uses the && (and) logical operator. Using de Morgans law, rewrite the code so that it uses the || (or) and ! (not) logical operators instead of the && operator.
if ((j > 3) && (k > 2)) q++; Switch case 3. Trace the following C program and write the output into the corresponding boxes given below. #include
C function, sort 4. The following C function sorts an array named list of int values into ascending (nondecreasing) order. The function parameter listLength gives the number of elements in the array to be sorted. Observe that the function continues to execute even if the initial array is in sorted order or if the array becomes sorted part way through the functions execution. Re-write the sort function, modifying it so that the function finishes the sorting process as soon as it is discovered that the array is in sorted order. Note: You must modify the sorting algorithm below and NOT use a different sorting algorithm. void sort (int list[], int listLength) { int bottom; for (bottom = 0; bottom < listLength - 1; bottom++) { int smallestLoc = bottom; int i; for (i = bottom + 1; i < listLength; i++) { //element comparison if (list[i] < list[smallestLoc]) smallestLoc = i; } // swap element int temp = list[bottom]; list[bottom] = list[smallestLoc]; list[smallestLoc] = temp; } }
C function, String 5. For data compression and other purposes, we often need to look for runs (or sequences) of repeated characters in data. For example, in the string abbbbccdeeeeef there is a run of 4 bs, a run of 2 cs and a run of 5 es. We also consider that the a, the d, and the f are all runs of length 1. Write a C function named compressString that, given an input character string, prints a possibly compressed representation of the input character string. The new representation is determined by counting the lengths of runs of a single character, as demonstrated below:
Given the string abbbbccdeeeeef, compressString prints 1a4b2c1d5e1f followed by . Given the string a, compressString prints 1a followed by . Given the string zzzzzzzzzzzzzzzzzzzzzzzzzz, compressString prints 26z followed by . Given the string xxbbbxx, compressString prints 2x3b2x followed by . Given the string aaAa, compressString prints 2a1A1a followed by . (Uppercase and lower-case letters are different.). Given the string , compressString only prints .
Write your compressString function and state any assumptions that you make.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started