Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# only Create an application that lets you enter a list of positive and zero in

ID: 3737907 • Letter: C

Question

C# only

Create an application that lets you enter a list of positive and zero integers. That is, if you enter a negative number,the application will be terminated. These numbers represent the altitudes. This application will count the number

of “hills” while you are entering a number. A hill is made up of at least three different altitudes, in which these
numbers must go up and then come down. Additionally, a plateau (flat surface) can exist on the top of the hill,
where all the numbers are the same.

Hints: you may need to keep track of your statuses—an uphill, a plateau, and a downhill—in addition to counting
the number of hills. You may also need to keep track of the previous number (see second line of example) in
addition to recording the current number.
For example: 2, 3, 4, 5, 5, 5, 5, 4, 3, 1 is a hill, and 1, 2, 3, 4, 5, 5, 6, 7, 6, 4, 2 is still a hill because even if you are on a
flat at 5 and 5, you can still climb uphill.
Also, 1, 2, 1, 3, 1, 4, 1 counts as 3 hills (1, 2, 1 is one; 1, 3, 1 is another; 1, 4, 1 is the third hill) so you will need to
keep track of the previous number.
1, 2, 3, 4, 5, 6 is not a hill because it is not going downhill, and 4, 3, 2, 1, 3, 5 is not a hill because there are no hills
there.

Explanation / Answer


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace countHills
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter number of numbers you want to enter::");
int len=int.Parse(Console.ReadLine());
int[] ar=new int[len]; //creating array
int prevnum=0;
int sum=0;
int hills=0;
int flat=0;
int index=0; //to store index of flat surface
for(int i=0;i<len;i++){
ar[i]=int.Parse(Console.ReadLine()); //enter number line by line not side by side
if(ar[i]<0)
Environment.Exit(0); //terminating programm if number is <0
if(i==0)
prevnum=ar[0];
else{
if(ar[i]>prevnum)
sum++;
else if(ar[i]==prevnum)
{
sum=sum+0;
flat++;
index=i;
}
else
sum--;
if(sum==0)
hills++;
prevnum=ar[i];
}
}
if(flat==1 && ar[index+1]>ar[index])
hills++;
Console.WriteLine("hills are"+hills);
}
}
}