Question

In: Computer Science

Question #4 DO NOT USE ANY NON-STANDARD LIBRARY. o All the required libraries have already been...

Question #4

DO NOT USE ANY NON-STANDARD LIBRARY. o All the required libraries have already been included. O DO NOT INCLUDE ANY OTHER LIBRARY INTO THE CODE. o DO NOT ALTER THE NAMES OF THE C FILES PROVIDED. o DO NOT ALTER THE NAMES AND PROTOTYPES OF THE FUNCTIONS. DO NOT ALTER THE CODE, ONLY ADD WHATS NEEDED TO MAKE IT WORK.

This is the code:

#include <stdio.h>

/*
* This function counts and returns the number of adjacent Up and Downs in an array of integers.
* In other words, you should count and return the number of changing directions in the sequence.
* This means, for instance, if the sequence is entirely ascending, or entirely descending, it should return 0.
* Examples:
* For the sequence {1,2,3,5} starting from 1, you say "Up, Up, Up", so it returns 0, (no changing direction)
* For the sequence {0,2,1,7} starting from 0, you say "Up, Down, Up", so it returns 2, (two times of changing directions or two adjacent up/downs)
* For the sequence {2,3,1,4,5,2,1} starting from 2, you say "Up, Down, Up, Up, Down, Down", so it returns 3, (three adjacent up/downs)
* For the sequence {4,3,2,1} starting from 4, you say "Down, Down, Down", so it returns 0, (no changing direction)
*/
int adjacentUpDowns(int a[], int size);

int main(void) {

   int a[] = {1,2,3,5};
   int b[] = {0,2,1,7};
   int c[] = {2,3,1,4,5,2,1};
   int d[] = {4,3,2,1};

   printf("%d\n", adjacentUpDowns(a,4));   //Expected output is 0
   printf("%d\n", adjacentUpDowns(b,4));   //Expected output is 2
   printf("%d\n", adjacentUpDowns(c,8));   //Expected output is 3
   printf("%d\n", adjacentUpDowns(d,4));   //Expected output is 0
}

int adjacentUpDowns(int a[], int size) {

   // Complete the code of the function

}

Solutions

Expert Solution

Attched the c code. It has comments for explanation. If you have any doubts, you can talk about it in the comments.

Program Screenshot for Indentation Reference:

Sample Output:

Program code to copy:

#include <stdio.h>

/*
* This function counts and returns the number of adjacent Up and Downs in an array of integers.
* In other words, you should count and return the number of changing directions in the sequence.
* This means, for instance, if the sequence is entirely ascending, or entirely descending, it should return 0.
* Examples:
* For the sequence {1,2,3,5} starting from 1, you say "Up, Up, Up", so it returns 0, (no changing direction)
* For the sequence {0,2,1,7} starting from 0, you say "Up, Down, Up", so it returns 2, (two times of changing directions or two adjacent up/downs)
* For the sequence {2,3,1,4,5,2,1} starting from 2, you say "Up, Down, Up, Up, Down, Down", so it returns 3, (three adjacent up/downs)
* For the sequence {4,3,2,1} starting from 4, you say "Down, Down, Down", so it returns 0, (no changing direction)
*/
int adjacentUpDowns(int a[], int size);

int main(void) {

   int a[] = {1,2,3,5};
   int b[] = {0,2,1,7};
   int c[] = {2,3,1,4,5,2,1}; // it has 7 elements
   int d[] = {4,3,2,1};

   printf("%d\n", adjacentUpDowns(a,4));   //Expected output is 0
   printf("%d\n", adjacentUpDowns(b,4));   //Expected output is 2
   printf("%d\n", adjacentUpDowns(c,7));   //Expected output is 3 // correct the size to 7
   printf("%d\n", adjacentUpDowns(d,4));   //Expected output is 0
}

int adjacentUpDowns(int a[], int size) {

   // is size is <= 2 then return -
   if (size <= 2) {
       return 0;
   }

   int count = 0; // this is the count

   int lastStep = a[0] < a[1]; // last step: 1 for up and 0 for down

   // start at third element
   for (int i = 2; i < size; i++) {
       // check difference
       int compar = a[i - 1] < a[i];
       // is different from lastStep then update count
       if (lastStep != compar) {
           // update lastStep
           lastStep = compar;
           count++;
       }
   }
   // return count
   return count;
}


Related Solutions

Computer Science Question 1: DO NOT USE ANY NON-STANDARD LIBRARY. o All the required libraries have...
Computer Science Question 1: DO NOT USE ANY NON-STANDARD LIBRARY. o All the required libraries have already been included. O DO NOT INCLUDE ANY OTHER LIBRARY INTO THE CODE. o DO NOT ALTER THE NAMES OF THE C FILES PROVIDED. o DO NOT ALTER THE NAMES AND PROTOTYPES OF THE FUNCTIONS.DO NOT ALTER ANY OF THE CODE! JUST ADD WHAT NEEDS TO BE ADDED FOR IT TO WORK! Please copy and paste the whole solution. Please read the code below...
(please use zelle's Python Graphics library, I've already asked this question and have had to post...
(please use zelle's Python Graphics library, I've already asked this question and have had to post this multiple times. I am working with the Python Graphics library and nothing else. thank you! note that I will downvote anything other than python graphics. this is the 4th time I've had to post this same question) im trying to create a function that has a circle bouncing left to right on a window and when the circle is clicked on, it stops...
(please use zelle's Python Graphics library, I've already asked this question and have had to post...
(please use zelle's Python Graphics library, I've already asked this question and have had to post this multiple times. I am working with the Python Graphics library and nothing else. thank you! note that I will downvote anything other than python graphics. this is the 4th time I've had to post this same question, just a simple code with 1 ball is needed ) im trying to create a function that has a circle bouncing left to right on a...
Question 2 of 4 Use decimals if needed, not fractions. Do not round at all (without...
Question 2 of 4 Use decimals if needed, not fractions. Do not round at all (without rounding, truncate final answer after three decimal places if needed). Suppose the demand for union representation is given by RD=240-4p and the supply of union representation is LS=2p. A. Graph the supply and demand for union representation. If union membership is free, the maximum number of workers who would want a union is _______ . If union fees are above $ _______ , zero...
Important notes: Use Console I/O (Scanner and System.out) for all user I/O in this lab. Do...
Important notes: Use Console I/O (Scanner and System.out) for all user I/O in this lab. Do not hard code data file paths or filenames; always ask the user for them. Complete the Monster Attack project by doing the following: In place of the driver used in homework 2, add a menu with a loop and switch (this is already done, ignore this). The user must be able to add attacks, get a report on existing attacks, save a file, retrieve...
**New code needed! Please do not reference code that has already been answered for this question...
**New code needed! Please do not reference code that has already been answered for this question as that code contains errors*** Write a C++ program to simulate a service desk. This service desk should be able to service customers that can have one of three different priorities (high, medium, and low). The duration for any customer is a random number (between 5 minutes and 8 minutes). You need to write a program that will do the following: Generate random 100...
Please read the Specifications carefully. Also do not use any C library. Program Specifications Assuming that...
Please read the Specifications carefully. Also do not use any C library. Program Specifications Assuming that all input strings are non-empty (having length of at least 1) and shorter than MAX_LENGTH, implement the following string functions: • strgLen( s ): return the length of string s. • strgCopy( s, d ): copy the content of string s (source) to d (destination). • strgChangeCase( s ): for each character in the string, if it is an alphabet, reverse the case of...
Show ALL work if it is required for the question and BASES 4. Convert the following...
Show ALL work if it is required for the question and BASES 4. Convert the following from Signed Binary to Decimal: (signed) a. 10011010 b. 01001101 5. 10000001 - 01100110 6. 80D3 + 74C2
PLEASE I HAVE TO RETURN IN 20 MINUTES, I HAVE ALREADY DO THE QUESTION 1, 2,...
PLEASE I HAVE TO RETURN IN 20 MINUTES, I HAVE ALREADY DO THE QUESTION 1, 2, 3, 4 BUT THE QUESTION 5 I CAN'T In python : 1)Write a sum function expecting an array of integers and returning its sum. 2) Write a multiply function excepting two arrays of integers and returning the product array term by term. For example mutliply([1,2,3], [5,4,3]) will give [5,8,9] 3) Write a nb1 function expecting an array containing only 0 and 1 and returning...
PLEASE DO ONLY THE "JOURNAL" SECTION. I ALREADY HAVE ANSWERS TO ALL THE OTHER ONES!!! Beacon...
PLEASE DO ONLY THE "JOURNAL" SECTION. I ALREADY HAVE ANSWERS TO ALL THE OTHER ONES!!! Beacon Signals Company maintains and repairs warning lights, such as those found on radio towers and lighthouses. Beacon Signals Company prepared the following end-ofperiod spreadsheet at December 31, 2019, fiscal year: Beacon Signals Company End-of-Period Spreadsheet For the Year Ended December 31, 2019 Unadjusted Trial Balance Adjustments Adjusted Trial Balance Account Title Dr. Cr. Dr. Cr. Dr. Cr. Cash 13,000.00 13,000.00 Accounts Receivable 40,500.00 (a)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT