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.
#include <iostream>
usingnamespace 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.
}
#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
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.
#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;
}
elseif (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.
}
#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;
}
elseif (v > prev) // found an inc
inc = true;
}
elseif (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;
}
elseif (v > prev) // found an inc
inc = true;
elsebreak; // plateau
}
elseif (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);
}
}
#include <iostream>
usingnamespace std;
bool isMountain( int N )
{
int a, b;
int ascending = true, descending = false;
cin >> a >> b; if ( b <= a ) returnfalse;
for ( int i = 3; i <= N; i++ )
{
a = b;
cin >> b;
if ( b == a ) returnfalse; // flat is banned!
if ( ascending && b < a )
{
ascending = false;
descending = true;
}
elseif ( descending && b > a )
{
returnfalse;
}
}
return descending;
}
int main()
{
int N;
cin >> N;
cout << ( isMountain( N ) ? "Mountain" : "Not a mountain" ) << '\n';
}