Click here to Skip to main content
11,577,258 members (61,611 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ home
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. . . Frown | :(

// 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 4-Feb-13 3:02am
ctype.h1.8K
Edited 4-Feb-13 5:31am
v2
Comments
CHill60 at 4-Feb-13 9:20am
   
What's the error?
Sandeep Mewara at 4-Feb-13 9:22am
   
What error? What are you talking/trying?
Philippe Mori at 4-Feb-13 21:26pm
   
You already post a very similar question with almost the same code.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Max check is missing
		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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Let me show you the logical step through using just 3 numbers - 1, 3, 2.
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 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:
#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!
  Permalink  
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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;
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 !
  Permalink  
v2

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

  Print Answers RSS
0 DamithSL 155
1 Afzaal Ahmad Zeeshan 125
2 OriginalGriff 115
3 Richard MacCutchan 100
4 Abhinav S 90
0 OriginalGriff 820
1 Sergey Alexandrovich Kryukov 631
2 Abhinav S 528
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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