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

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...
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 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)...
Jeremy and Alyssa Johnson have been married for five years and do not have any children....
Jeremy and Alyssa Johnson have been married for five years and do not have any children. Jeremy was married previously and has one child from the prior marriage. He is self-employed and operates his own computer repair store. For the first two months of the year, Alyssa worked for Office Depot as an employee. In March, Alyssa accepted a new job with Super Toys Inc. (ST), where she worked for the remainder of the year. This year, the Johnsons received...
Jeremy and Alyssa Johnson have been married for five years and do not have any children....
Jeremy and Alyssa Johnson have been married for five years and do not have any children. Jeremy was married previously and has one child from the prior marriage. He is self-employed and operates his own computer repair store. For the first two months of the year, Alyssa worked for Office Depot as an employee. In March, Alyssa accepted a new job with Super Toys Inc. (ST), where she worked for the remainder of the year. This year, the Johnsons received...
Jeremy and Alyssa Johnson have been married for five years and do not have any children....
Jeremy and Alyssa Johnson have been married for five years and do not have any children. Jeremy was married previously and has one child from the prior marriage. He is self-employed and operates his own computer repair store. For the first two months of the year, Alyssa worked for Office Depot as an employee. In March, Alyssa accepted a new job with Super Toys Inc. (ST), where she worked for the remainder of the year. This year, the Johnsons received...
Jeremy and Alyssa Johnson have been married for five years and do not have any children....
Jeremy and Alyssa Johnson have been married for five years and do not have any children. Jeremy was married previously and has one child from the prior marriage. He is self-employed and operates his own computer repair store. For the first two months of the year, Alyssa worked for Office Depot as an employee. In March, Alyssa accepted a new job with Super Toys Inc. (ST), where she worked for the remainder of the year. This year, the Johnsons received...
Jeremy and Alyssa Johnson have been married for five years and do not have any children....
Jeremy and Alyssa Johnson have been married for five years and do not have any children. Jeremy was married previously and has one child from the prior marriage. He is self-employed and operates his own computer repair store. For the first two months of this year, Alyssa worked for Office Depot as an employee. In March, Alyssa accepted a new job with Super Toys, Inc. (ST) where she worked for the remainder of the year. This year the Johnsons received...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT