Question

From a sequence of integers, finished with the value 0, and entered by keyboard, we are...

From a sequence of integers, finished with the value 0, and entered by keyboard, we are asked to calculate the number of mountains and valleys it has. (algorithm in C)

Mountain is called a set of 3 consecutive values where the value of the medium is higher than its neighbors (the previous and the later). Valley is called the opposite case, a set of three consecutive values where the value of the medium is lower than that of its neighbors. For example, in the following sequence there are 3 mountains and 3 valleys.

`3 4 7 2 3 1 9 8 5 2 7 0`

** Attention **: The same number can be part of a mountain (4, 7, 2) and a valley (7, 2, 3).

**Note:**
- The empty sequence can be given.
- If there is data, there will always be at least 3.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

int main()

{

int a[40],mountain=0,valley=0,i,len;

len=0;

printf("\nEnter the sequence of number :");

while(1)

{

scanf("%d",&a[len]);

if(a[len]==0)

break;

else

len+=1;

}

for(i=1;i<len-1;i++)

{

if(a[i]>a[i-1]&&a[i]>a[i+1])

{

mountain+=1;

printf("\nMountain :(%d,%d,%d)",a[i-1],a[i],a[i+1]);

}

if(a[i]<a[i-1]&&a[i]<a[i+1])

{

valley+=1;

printf("\nValley:(%d,%d,%d)",a[i-1],a[i],a[i+1]);

}

}

printf("\nThe total # of valleys:%d",valley);

printf("\nThe total # of mountains:%d",mountain);

}

Algorithm

  1. Start
  2. Initialize array a[], valley=0, mountain, i, len
  3. accept value in array a[] untill zero is encountered
  4. Calculate the length of array in len
  5. For i=1 to len-1
  6. if(a[i]>a[i-1]&&a[i]>a[i+1])
  7. ​​​​​​increment mountain by 1
  8. else if(a[i]<a[i-1]&&a[i]<a[i+1])
  9. increment valley by 1
  10. Go to step 6 if I<len-1
  11. Print value of mountain and valley
  12. Stop

​​​​​​I hope the code help you in understanding the problem better.

if you have any questions comment down and please upvote thanks

Add a comment
Know the answer?
Add Answer to:
From a sequence of integers, finished with the value 0, and entered by keyboard, we are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Let's say you are given a sequence of distinct positive numbers. We want to find a...

    Let's say you are given a sequence of distinct positive numbers. We want to find a subsequence with the maximum possible sum, with the restriction that we are not allowed to take three consecutive elements from the original sequence. For example, for input 1, 6, 5, 2, 7, 9, 3, 4, the subsequence with the maximum possible sum is 6, 5, 7, 9, 4 (we have two pairs of consecutive elements 6, 5 and 7, 9 but not three consecutive...

  • (20 points) You are given an array A of distinct integers of size n. The sequence...

    (20 points) You are given an array A of distinct integers of size n. The sequence A[1], A[2], ..., A[n] is unimodal if for some index k between 1 and n the values increase up to position k and then decrease the reminder of the way until position n. (example 1, 4, 5, 7, 9, 10, 13, 14, 8, 6, 4, 3, 2 where the values increase until 14 and then decrease until 1). (a) Propose a recursive algorithm to...

  • In this project, you will work on the algorithm (discussed in Module 1) to determine the...

    In this project, you will work on the algorithm (discussed in Module 1) to determine the length of the longest sub sequence of consecutive integers in an array You will implement the algorithm using Hash tables. You are provided with sample code (in C++ and Java) representing the linked list-based implementation of Hash tables (as an array of Linked Lists). You could go through the code to understand the implementation of a Hash table and the functions that can be...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

  • A sequence (dl, d2, , dn) of nonnegative integers is graphical if it is nonincreasing and there i...

    A sequence (dl, d2, , dn) of nonnegative integers is graphical if it is nonincreasing and there is a graph with vertices vl,... , vn such that for all i with 1 Si< n the degree of vi is di . The following algorithm A computes a graph for a given graphical sequence A- "On input h(d1, ..., dn)i do 1. Let V ← {vi, . . . , vn), E ← ø. For all i with 1 i niet...

  • java estion7 For this question, assume all input comes from the keyboard, and al output goes...

    java estion7 For this question, assume all input comes from the keyboard, and al output goes to the screen. Include method prototypes and comments. The array should have room for 100 integers Write a complete Java program, including at least one comment in the main propram and one in e to do the following: Write a main program which will call the methods described below (a) First the main program will read an integer (this integer is a parameter or...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • (a) Write a C program to print a list of all integers between 1 and 1,000...

    (a) Write a C program to print a list of all integers between 1 and 1,000 (inclusive) which are divisible by 7 but not by 13. The list should be printed to a file called "output.dat". Remember to close the file after use. (35%) (b) Explain what is meant by the C preprocessor. Explain the effect of the following line of code: #define SQUARE (x) (x) * (x) (25%) (c) Explain the concept of an array in C. Write down...

  • Consider the following algorithm: ocedure Algorithm (b: integer, n: positive integer,i datinct integem) proc answer :", 0 nand 6 while (j print(j, z, b, answer) if jSn then answer:-j return a...

    Consider the following algorithm: ocedure Algorithm (b: integer, n: positive integer,i datinct integem) proc answer :", 0 nand 6 while (j print(j, z, b, answer) if jSn then answer:-j return answer (8 points] Assume that this algorithm receives as input the numbers b-17 andn9nd the corresponding sequence or iaie i 2 3 4 516 7 8 corresponding sequence of integers 19 Fill out the table below: i 는, ↓answer (b) [I point] Assume that the algorithm receives the same input...

  • Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n...

    Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n as parameters and returns a NEW list which contains the n largest values in the parameter list. The values in the returned list should be in increasing order. The returned list must always be of length n. If the number of values in the original list is less than n, the value None should be repeated at the end of the returned list to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT