Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 21:10pm
ctype.h1.7K
Edited 4-Feb-13 16:23pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

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

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

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

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

Solution 1

fill(label[10],length);
print(label[10],length);
.
.
.
 
use like this
otherwise
 
fill(label[],length);
  Permalink  
Comments
Usman Hunjra at 4-Feb-13 2:56am
   
That is O K .. The Problem is not in there .. !!
Usman Hunjra at 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)

  Print Answers RSS
0 Maciej Los 600
1 OriginalGriff 455
2 Sascha Lefévre 289
3 Richard Deeming 195
4 Abhinav S 190
0 Sergey Alexandrovich Kryukov 9,693
1 OriginalGriff 9,185
2 Peter Leow 5,112
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 2,876


Advertise | Privacy | Mobile
Web02 | 2.8.150327.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