In: Computer Science
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
}
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;
}