solve this please....

write a code that first gets n.then gets n numbers(solider's height),(numbers will separate with space)and prints out the number of soldiers that are between the smallest and the tallest soldiers.note that n<10000 and if the tallest one is in front of the smallest should print 0.
input 1:
5
100,102,120,300,500
output 1:
3
input 2:
7
100 90 40 80 10 30 20
output 2:
0
closed account (48T7M4Gy)
Sorry, this is not a homework site. Attempts by you are the key to unlocking the advice treasure trove.
#include <iostream>
using namespace std;
main()
{
int n;
cin>>n;
int a[n],num,maxim=0,minim=0;
string s;
for(int i=0;i<n;i++)
{
cin>>s;
for(int j=0;j<n;j++)
{
num+=(s[i]-48);
num*=10;
}
a[i]=num;
s="";
}
for(int k=0;k<n;k++)
{
if(a[k]>a[maxim])
maxim=k;
if(a[k]<a[minim])
minim=k;
}
if(maxim>minim)
cout<<maxim-minim;

}
Actually it is not a homework.
I found it on the net.but I can't solve.
please write the correct code
Regards
closed account (48T7M4Gy)
Show us where you found it please.
it is a persian site
closed account (48T7M4Gy)
Where/what is the problem with the program?
The problem states that the input heights are separated by spaces, but your input shows them separated by a comma. Make sure that you use spaces when creating the input.
1
2
    cin >> n;
    int a[n], num, maxim = 0, minim = 0;
The underlined part is non-standard C++. You could create a vector instead. Better yet, figure out how to do the problem without storing all the numbers at all.
1
2
3
4
5
6
7
        cin >> s;
        for (int j = 0; j < n; j++) {
            num += (s[i] - 48);
            num *= 10;
        }
        a[i] = num;
        s = "";
You can replace all of this with cin >> a[i] Once that's done, s and num are no longer used so they can be removed.

The output isn't quite right. I suggest you temporarily add cout << "minim=" << minim << ", Maxim=" << maxim << '\n'; just before generating the output.


Topic archived. No new replies allowed.