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.
#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
I hope the code help you in understanding the problem better.
if you have any questions comment down and please upvote thanks
From a sequence of integers, finished with the value 0, and entered by keyboard, we are...
(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 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 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 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...
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...
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...
(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 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 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...
Write a program “hw4.c” that reads integer (less than or equal 100) from the keyboard and, on the output, writes the sum of the divisors of n (other than itself). For integers less than or equal to 1 it should print 0. For example, the input -3 0 1 4 5 6 12 should generate the output 0 0 0 3 1 6 16 Explanation of output: The input -3 is less than 1, output is 0. The input 0...