```
if (min > a[k])
{
min = a[k];
min_index=k;
}
else
```**if (a[k] > max)**
{

And as I mentioned before, do yourself a favor and start using a debugger. You won't get anywhere if you shout help for every tiny problem that you will encounter.

help needed seniors .. !!

Actually if i input values in Thousands, it will not return the expected result,,, That's The P R O B ..

I didn't find a way to fix the error. . . :(

Actually if i input values in Thousands, it will not return the expected result,,, That's The P R O B ..

I didn't find a way to fix the error. . . :(

```
// a r r a y.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdlib.h"
#include <iostream>
using namespace std;
void fill(int a[], int length)
{
for (int i = 0; i < length; i++)
{
cout << "Element #" << i+1 << endl;
cin >> a[i];
//a[i] = i;
}
}
void print(int a[], int length)
{
cout << "A R R A Y Of 10 Elements .." << endl;
for (int j = 0; j < length; j++)
{
cout << a[j] << " | ";
}
}
void min_max(int a[], int length)
{
int min = a[0];
int max = a[0];
int min_index=0, max_index=0;
for (int k = 0; k < length; k++)
{
if (min > a[k])
{
min = a[k];
min_index=k;
}
else
{
max = a[k];
max_index=k;
}
}
cout << '\n';
cout<<"Maximum number is: "<< max <<endl;
cout << "Max_Index: " << max_index << endl;
cout<<"Minimum number is: "<< min <<endl;
cout << "Min_Index: " << min_index << endl;
}
void sum_avg(int a[], int length)
{
int sum = 0;
for (int i = 0; i < length; i++)
{
sum = sum + a[i];
}
cout << "S U M: " << sum << endl;
int avg = sum/length;
cout << "A V G: " << avg << endl;
}
int main()
{
const int length = 10;
int label[length];
fill(label, length);
print(label, length);
sum_avg(label, length);
min_max(label, length);
system("pause");
return 0;
}
```

Comments

What's the error?

What error? What are you talking/trying?

You already post a very similar question with almost the same code.

Max check is missing

And as I mentioned before, do yourself a favor and start using a debugger. You won't get anywhere if you shout help for every tiny problem that you will encounter.

```
if (min > a[k])
{
min = a[k];
min_index=k;
}
else
```**if (a[k] > max)**
{

And as I mentioned before, do yourself a favor and start using a debugger. You won't get anywhere if you shout help for every tiny problem that you will encounter.

Your min is OK but your max is wrong.

should probably be

otherwise you just end up with your last value as max.

There are many other issues with this code but I guess that's the fix you were looking for.

```
...
else
{
max = a[k];
max_index=k;
}
...
```

should probably be

```
...
if( max < a[k] )
{
max = a[k];
max_index=k;
}
...
```

otherwise you just end up with your last value as max.

There are many other issues with this code but I guess that's the fix you were looking for.

Let me show you the logical step through using just 3 numbers - 1, 3, 2.

The offending code is the following:

So, for k = 0 to 2:

k = 0: min = 1, max = 1, a[k] = 1

min == a[k] - run code in "else"

Result: max = 1, max_index = 0

k = 1: min = 1, max = 1, a[k] = 3

min < a[k] - run code in "else"

Result: max = 3, max_index = 1

k = 2: min = 1, max = 3, a[k] = 2

min < a[k] - run code in "else"

Result: max = 2, max_index = 2

There are a few points (the first is a choice):

1a: the for loop should start from 1, not 0 - or

b: "min" initialized to

2: the

If we pick the second option, the code would result as follows:

I haven't looked at the rest of the code, so if there are problems there, you can try and work them out!

The offending code is the following:

```
int min = a[0];
int max = a[0];
...
if (min > a[k])
{
min = a[k];
min_index=k;
}
else
{
max = a[k];
max_index=k;
}
```

So, for k = 0 to 2:

k = 0: min = 1, max = 1, a[k] = 1

min == a[k] - run code in "else"

Result: max = 1, max_index = 0

k = 1: min = 1, max = 1, a[k] = 3

min < a[k] - run code in "else"

Result: max = 3, max_index = 1

k = 2: min = 1, max = 3, a[k] = 2

min < a[k] - run code in "else"

Result: max = 2, max_index = 2

There are a few points (the first is a choice):

1a: the for loop should start from 1, not 0 - or

b: "min" initialized to

`std::numeric_limits<int>::max()`

(`#include <limits>`

) and "max" initialized to 02: the

`else`

should be replaced by a separate `if`

statement that checks against max - the "k = 2" proves why (max would just get overwritten)If we pick the second option, the code would result as follows:

```
#include <limits>
void min_max(int a[], int length)
{
int min = std::numeric_limits<int>::max();
int max = 0;
int min_index=0, max_index=0;
for (int k = 0; k < length; k++)
{
if (min > a[k])
{
min = a[k];
min_index=k;
}
if (max < a[k])
{
max = a[k];
max_index=k;
}
}
cout << '\n';
cout<<"Maximum number is: "<< max <<endl;
cout << "Max_Index: " << max_index << endl;
cout<<"Minimum number is: "<< min <<endl;
cout << "Min_Index: " << min_index << endl;
}
```

I haven't looked at the rest of the code, so if there are problems there, you can try and work them out!

v5

Quote:if (min > a[k])

{

min = a[k];

min_index=k;

}

else

{

max = a[k];

max_index=k;

}

Change to

```
if (min > a[k] )
{
min = a[k];
min_index=k;
}
else if (max < a[k])
{
max = a[k];
max_index=k;
}
```

As this is homework I'm not going to give you the complete solution but I will point you in the right direction ...

When you are checking to see if you have to update the**minimum** you have (correctly) used

You need to do something similar before you update the**maximum**

Or you can just look at all the other solutions !

When you are checking to see if you have to update the

`if (min > a[k])`

You need to do something similar before you update the

Or you can just look at all the other solutions !

v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada
+1 416-849-8900 x 100