Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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..??
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 >> 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
Updated 4-Feb-13 15:23pm
v2

C++
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:

C++
int input;
cin >> input;
a[i] = input;
 
Share this answer
 
Missing ;
sum_avg(label, length)


Calculation can not work correct:
CSS
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;
 
Share this answer
 
v2
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.

C++
//...
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...
 
Share this answer
 
Inconsistent loops:
C++
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.
 
Share this answer
 
fill(label[10],length);
print(label[10],length);
.
.
.

use like this
otherwise

fill(label[],length);
 
Share this answer
 
Comments
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 .. .. .. .

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