Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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. . . :(

C++
// 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;
}
Posted
Updated 4-Feb-13 5:31am
v2
Comments
CHill60 4-Feb-13 9:20am    
What's the error?
Sandeep Mewara 4-Feb-13 9:22am    
What error? What are you talking/trying?
Philippe Mori 4-Feb-13 21:26pm    
You already post a very similar question with almost the same code.

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


Change to

C++
if (min > a[k] )
        {
            min = a[k];
            min_index=k;
        }
        else if (max < a[k])
        {
            max = a[k];
            max_index=k;
        }
 
Share this answer
 
Let me show you the logical step through using just 3 numbers - 1, 3, 2.
The offending code is the following:
C#
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 0
2: 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:
C++
#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!
 
Share this answer
 
v5
Your min is OK but your max is wrong.
...
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.
 
Share this answer
 
Max check is missing
C++
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.
 
Share this answer
 
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
if (min > a[k])

You need to do something similar before you update the maximum

Or you can just look at all the other solutions !
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900