12,292,585 members (67,063 online)
Rate this:
See more:
Hi, I've write a code that will use functions to compute maximum, minimun, index, sum & average. !!
P R O B L E M: While i'm filling the array, it will not stop at 10 & The Result is Unpredictable..
Any suggestions will be appreciated..??
```// 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 >> 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 < 10; k++)
{
if (min > a[k])
{
min = a[k];
min_index ++;
}
else
{
max = a[k];
max_index ++;
}
}
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 << "Mean: " << sum << endl;
int avg = sum/length;
cout << "Median: " << 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 3-Feb-13 20:10pm
ctype.h1.8K
Edited 4-Feb-13 15:23pm
v2

Rate this:

## Solution 3

```void fill(int a[], int length)
{
for (int i = 0; i < length; i++)
{
cout << "Element #" << i+1 << endl;
cin >> i;
a[i] = i;
}

}```

Value i using in for loop and you use same i value in cin(input). Try:

```int input;
cin >> input;
a[i] = input;```
Rate this:

## Solution 4

Missing ;
`sum_avg(label, length)`

Calculation can not work correct:
```int avg = sum/length;
cout << "Median: " << avg << endl;```
Result and calculation should bei float or double.

Variable i used for different things, change to:
```cin >> num;
a[i] = num;```
v2
Rate this:

## Solution 5

In addition to all errors mentionned in other solutions, the computation of the maximum is also incorrect.

Inside the else part, a condition must be added to ensure that the value is actually the maximum. If a value is not the minimum at a given point it might not be the maximum.

```//...
else if (max < a[k]) { /* code corrected according to other solution here */ }
```

Usually it is somewhat easier to read if the condition is specified in the natural way.

`if (a[k] > max) { }`

As a side note, it is a very bad idea to name your variable either min or max as these names where popular macro name a few years ago...
Rate this:

## Solution 2

Inconsistent loops:
```for (int k = 0; k < 10; k++)
```
should be `length `instead of 10.

There are a couple of other obvious problems, like:
```if (min > a[k])
{
min = a[k];
min_index ++;
}
```
should be
```min_index = k;
```

And your outputs are confusing. Mean: shows the sum and Median: shows the average.
```cout << "Mean: " << sum << endl;
int avg = sum/length;
cout << "Median: " << avg << endl;
```
An average is not a median value.

My advice: Get used to running your programs in a debugger and single-step them in your first run. ALWAYS!

Then get your statistics terms straight. Use Google to lookup the definition of the median value.
Rate this:

## Solution 1

fill(label[10],length);
print(label[10],length);
.
.
.

use like this
otherwise

fill(label[],length);
Usman Hunjra 4-Feb-13 2:56am

That is O K .. The Problem is not in there .. !!
Usman Hunjra 4-Feb-13 3:26am

HELP NEEDED .. .. .. .

Top Experts
Last 24hrsThis month
 Dave Kreskowiak 451 OriginalGriff 410 KARTHIK Bangalore 395 ppolymorphe 300 F-ES Sitecore 300
 OriginalGriff 8,405 ppolymorphe 5,047 CHill60 4,960 KARTHIK Bangalore 4,437 Sergey Alexandrovich Kryukov 4,352