Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 int main() { int R=0; do { switch (R) { case 0: printf(%d ,R); case 1: R += 1; break; case 2: printf(2 ); case 3: printf(%d ,R * 2); case 4: R = R + 3; break; default: R = R * 3; printf(%d ,R / 2); } } while(R<=10); return 0;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What roles and responsibilities should people at the meeting have?

Answered: 1 week ago

Question

Compare the different types of employee separation actions.

Answered: 1 week ago

Question

Assess alternative dispute resolution methods.

Answered: 1 week ago

Question

Distinguish between intrinsic and extrinsic rewards.

Answered: 1 week ago