Mountain

I have to determine if the string is mountain.
On the first line is N, string size.
On the next line are N natural numbers, string elements.

Restrictions : 3 ≤ N ≤ 500

Input:
7
1 4 6 7 8 5 2 Output: Mountain

Input:
7
1 4 6 10 8 9 2 Output: the string is not a mountain


Solving as much as possible with while and if, no vectors.


#include <iostream>

using namespace std;

int main()
{
int n,x,c,mountain;
cout<<"n=";cin>>n;
x=n;
mountain=1;
while(n>9 && n%10<(n/10)%10)
n=n/10;
if(n==x || n<10) mountain=0;
while(n>9 && n%10>(n/10)%10)
n=n/10;
if(n>9) mountain=0;
if(mountain==1) cout<<"mountain";
else cout<<"the string is not a mountain";
return 0;
}

I determined if a number is mountain, but I must to determine if a string of numbers is mountain.
Well, clearly neither of them are Ben Nevis. What constitutes your definition of a "mountain"? That it has a single peak?
Hello bstroe,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


A couple of questions to start with:

Are you writing the code for the compiler?
When you say "string" do you mean a "std::string" or just a string of numbers?

I have not tested the code yet.

Due to your poor choice of variable it will take me a few minutes to figure out what is what.

Also it is always a good idea to initialize your variables when defined.

As a suggestion you may find this easier to read:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

int main()
{
    int n{}, x{}, c{}, mountain{};

    cout << "n="; 
    cin >> n;

    x = n;
    mountain = 1;

    while (n > 9 && n % 10 < (n / 10) % 10)
        n = n / 10;

    if (n == x || n < 10)
        mountain = 0;

    while (n > 9 && n % 10 > (n / 10) % 10)
        n = n / 10;

    if (n > 9)
        mountain = 0;

    if (mountain == 1)
        cout << "mountain";
    else
        cout << "the string is not a mountain";

    return 0;  // <--- Not required, but makes a good break point.
}


Andy
One way is to check ascending until not and then check descending (assuming only 1 peak!):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

int main()
{
	int i {}, N {}, A[500] {};

	std::cin >> N;

	if (N < 3 || N > 500)
		return (std::cout << "Must be between 3 and 500\n"), 1;

	for (int a = 0; (a < N) && (std::cin >> A[a]); ++a)

	while ((i + 1 < N) && (A[i] < A[i + 1]))
		++i;

	if ((i == 0) || (i == N - 1))
		std::cout << "not a ";
	else {
		while ((i + 1 < N) && (A[i] > A[i + 1]))
			++i;

		if (i != N - 1)
			std::cout << "not a ";
	}

	std::cout << "mountain\n";
}



7
1 4 6 7 8 5 2
mountain

7
1 4 6 10 8 9 2
not a mountain

Well, clearly neither of them are Ben Nevis. What constitutes your definition of a "mountain"? That it has a single peak?

Yes.

- 2 ≤ i ≤ N - 1 where N represents the number of elements in the string
-all elements from 1 to i are in strictly ascending order
-all elements from i to N are in strictly descending order
Last edited on
Hello bstroe,

After I see some things that you could do better and realized that you never get any input from the user for the numbers.

Next question is do you want to store the entered numbers in an array? This could be done without an array.

Some changes to the start you may want to consider:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    bool mountain{ true };
    int howManyNums{}, x{}, c{};
    int input[]{ 1, 4, 6, 7, 8, 5, 2 };

    cout << "\nHow many numbers do you want to enter? ";
    cin >> howManyNums;

    // To do. Enter the numbers.

    x = howManyNums;
    //mountain = 1;  // <--- If you initialize the variable when defined you do not need this line. 

For the variable "mountain" as an "int" it will work, but for the way it is used the "bool" is better.

Work on the "to do" part B4 you worry about the rest of the program. With proper input the rest of the program does not do much.

Andy
When you say "string" do you mean a "std::string" or just a string of numbers?

Just a string of numbers
Input:
N =7 (the number of elements in the string)

1 4 6 7 8 5 2 Output: Mountain
Last edited on
Next question is do you want to store the entered numbers in an array? This could be done without an array.

Without an array.

I don't want to use vectors and arrays.
Last edited on
Hello bstroe,

This is the first thought I had. Not saying it is the best way, but a start to consider:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

//using namespace std;

int main()
{
    bool mountain{ true };
    int howManyNums{}, /*x{}, c{},*/ peak{}, decending{}, num{};

    std::cout << "\n How many numbers do you want to enter? ";
    std::cin >> howManyNums;

    for (int lc = 0; lc < howManyNums; lc++)
    {
        std::cout << " Enter a number: ";
        std::cin >> num;

        if (num > peak && decending == peak)
        {
            peak = decending = num;

            continue;
        }

        if (num > decending)
        {
            mountain = false;

            break;
        }
        else if (num < decending)
        {
            decending = num;
        }
    }

    std::cout << '\n'; // <--- Used for testing. Remove when finished. Needed when entering the numbers as (1 4 6 7 8 5 2) at the 1st prompt.

    if (mountain)
        std::cout << "\n The series is a mountain.\n";
    else
        std::cout << "\nThe series is not a mountain.\n";



    return 0;  // <--- Not required, but makes a good break point.
}


Andy
I don't know if you allow "plateaus" or not.
For example, this sequence has plateaus and would be a "mountain" if plateaus are allowed:

12
1 2 2 3 4 5 5 4 3 2 1 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <sstream>
#include <string>

#if 1

// Allow plateaus.
void is_mountain(std::istream& in)
{
    bool inc = false, dec = false;
    int n, prev;
    in >> n >> prev;
    for (int i = 1; i < n; ++i)
    {
        int v;
        in >> v;
        if (!dec)
        {
            if (v < prev)        // found a dec
            {
                if (!inc) break; // no inc before dec
                dec = true;
            }
            else if (v > prev)   // found an inc
                inc = true;
        }
        else if (v > prev)       // inc after dec
        {
            dec = false;
            break;
        }
        prev = v;
    }
    if (inc && dec) std::cout << "Mountain.\n";
    else            std::cout << "Not a mountain.\n";
}

#else

// Disallow plateaus.
void is_mountain(std::istream& in)
{
    bool inc = false, dec = false;
    int n, prev;
    in >> n >> prev;
    for (int i = 1; i < n; ++i)
    {
        int v;
        in >> v;
        if (!dec)
        {
            if (v < prev)        // found a dec
            {
                if (!inc) break; // no inc before dec
                dec = true;
            }
            else if (v > prev)   // found an inc
                inc = true;
            else
                break;           // plateau
        }
        else if (v >= prev)      // inc (or plateau) after dec
        {
            dec = false;
            break;
        }
        prev = v;
    }
    if (inc && dec) std::cout << "Mountain.\n";
    else            std::cout << "Not a mountain.\n";
}

#endif

int main()
{
    std::istringstream ss
    {
        "5 1 2 3 4 5\n"
        "5 5 4 3 2 1\n"
        "5 1 1 1 1 1\n"
        "5 1 2 3 2 3\n"
        "5 1 2 3 2 1\n"
        "12 1 2 2 3 4 5 5 4 3 2 1 1\n"
    };
    std::string line;
    while (std::getline(ss, line))
    {
        std::cout << line << '\n';
        std::istringstream s(line);
        is_mountain(s);
    }
}

Based on:
-all elements from 1 to i are in strictly ascending order
-all elements from i to N are in strictly descending order


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

bool isMountain( int N )
{
   int a, b;
   int ascending = true, descending = false;
   cin >> a >> b;   if ( b <= a ) return false;
   for ( int i = 3; i <= N; i++ )
   {
      a = b;
      cin >> b;
      if ( b == a ) return false;      // flat is banned!
      if ( ascending && b < a )
      {
         ascending = false;
         descending = true;
      }
      else if ( descending && b > a )
      {
         return false;
      }
   }
   return descending;
}

int main()
{
   int N;
   cin >> N;
   cout << ( isMountain( N ) ? "Mountain" : "Not a mountain" ) << '\n';
}
Last edited on
Topic archived. No new replies allowed.