Question

In a series of numbers, a "mini peak" is a set of 3 numbers where the...

In a series of numbers, a "mini peak" is a set of 3 numbers where the middle number is strictly greater than its adjacent left and right neighbors. For example, the array:

[ 4, 6, 2, 1 ]

contains 1 mini peak, with 6 in the middle ([ 4, 6, 2]).

Write full and complete C++ program to open a text file named numbers.txt in the working folder, which contains groups of numbers, one per line, with each group starting with "how many" are in the set:

<how_many_numbers>
<number 1>
<number 2>
...
<number n>

For each group of numbers, output the number of mini peaks in that group.  Do not count boundary numbers (at the beginning and end of the array) since they only have one left/right neighbor.

For example, if the file contains these lines:

5
5
4
1
3
2
3
1
1
2

Your program would first read the number 5, indicating there are 5 numbers in the series. Then it would read the next 5 numbers, count the number of mini peaks, then output the result. Then it would read the number 3, indicating there are 3 numbers in the next set, read 3 numbers, and so on.

Requirements:

  • Continue reading sets of numbers until EOF.
  • Use dynamic memory to allocate enough space for the input. There should be no hard-coded limits on the size of the input.
  • You can assume each series has at least 3 numbers.
  • Free (delete) any memory that you allocate dynamically.
  • Use only the coding structures taught in class.

The output on the above example would be:

Read 5 numbers.  Mini peaks: 1
Read 2 numbers. Mini peaks: 0
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include<iostream>

#include<fstream>

using namespace std;

int main(){

ifstream in;

in.open("numbers.txt");

if(in.fail()){

cout<<"Unable to open file"<<endl;

return 0;

}

int n;

int *arr;

while(!in.eof()){

in>>n;

arr = new int[n];

for(int i=0; i<n; i++)

in>>arr[i];

int peaks = 0;

for(int i=1; i<n-1; i++){

if(arr[i-1]>arr[i] && arr[i]<arr[i+1])

peaks++;

}

delete arr;

cout<<"Read "<<n<<" numbers. Mini peaks: "<<peaks<<endl;

}

in.close();

return 0;

}

Add a comment
Know the answer?
Add Answer to:
In a series of numbers, a "mini peak" is a set of 3 numbers where the...
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
  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • How to I solve this using modulo? Problem 1: Develop a C program that reads a...

    How to I solve this using modulo? Problem 1: Develop a C program that reads a file of integers called “numbers.txt” It then prints the occurrence count of each digit from [0-9]. Your code must utilize the following function: void freqFun (int num, int counters[]) In this prototype, num represents the number read from the file and counters represents the set of counters that hold the occurrence frequency of each digit. Digits that do not occur in the file shall...

  • This is a C++ question we do not use namespace std at the beginning of the...

    This is a C++ question we do not use namespace std at the beginning of the program 9. Arrays: Functions and Reading From File Although this code is a little long, the strategy is straightforward and it will really help you practice functions. I have provided a large amount of detail to help you out. In a nutshell, you're going to read some numbers from a file (into an array) and then ask the user for a number that will...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 IN JAVA PLZ 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4...

  • Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first numbe...

    Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first number in the file will be an integer, and will indicate the amount of decimal numbers to follow. Once you have the read the numbers from the file into an array then compute the following properties on the set of numbers: the sum, the average (mean), the variance, the standard deviation, and the median. Don’t try to do the entire program...

  • A Fibonacci sequence is a series of numbers where the next number in the series is...

    A Fibonacci sequence is a series of numbers where the next number in the series is the sum of the previous two numbers. The sequence starts with the numbers 0 and 1. Here are the first ten numbers of the Fibonacci sequence:             0 1 1 2 3 5 8 13 21 34 Write a Java program to print out the first fifteen numbers of the Fibonacci sequence using a loop. You will need three variables previous, current, and next...

  • Question 3 (2 points) Suppose, you have a file called 'numbers.txt' where each line of the...

    Question 3 (2 points) Suppose, you have a file called 'numbers.txt' where each line of the file contains a number. The number in each line can be a positive or a negative number. Write a program that will find the average of all the numbers between 0 to 1000 (including 1000). Your file may look like the following: 10 25 -5 1001 10 Sample output: The average is 15 o Нt

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
ADVERTISEMENT