Hi,
This is an exercise I solved it, I want to know, is my solve is the best solve or there is notes, if there is a notes, I'm glade to hear it:

The Exercise:

Make a program that calculates the sum, mean, minimum, and maximum of a series of numbers.

Example:
numbers: 10, 12, 10, 14

The sum is 46,

the mean is 11.5,

the minimum is 10,

the maximum is 14.

My solve:

#include <iostream>
using namespace std;

void main()
{
	float sum, mean, minimum, maximum;
	float numbers[4]={10,12,10,14};
	
	sum=numbers[0]+numbers[1]+numbers[2]+numbers[3];
	cout << "Sum = " << sum << endl;

	mean=sum/4;
	cout << "Mean = " << mean << endl;
}

how can I find minimum and maximum from array?

Recommended Answers

All 7 Replies

Make a loop and iterate over the array. While looping, use comparison statements to compare the elements to each other.

Try a bubble sort.Look it up.. it's simple.

Sorry .. here it is

#include <iostream>
using namespace std;

int main ()
  {
    int numbers[8]={4,2,3,1,6,23,41,45};
    int nb=0;
    bool looping=true;

    while (looping==true)
      {
        looping=false;
        for (int x=0;x<7;x++)
          {
            if (numbers[x]>numbers[x+1])
              {
                nb=numbers[x];
                numbers[x]=numbers[x+1];
                numbers[x+1]=nb;
                looping=true;
              }
          }
      }
    cout << "Smallest :" << numbers[0] << endl << "Largest :" << numbers[7];
  }
commented: C'mon, Bubble sort? -2

Try a bubble sort.Look it up.. it's simple.

like this:

#include <iostream>
using namespace std;

void main()
{
	int minimum;
	int numbers[4]={10,12,10,14};
	for (int i=0; i<4; i++)
	{
	if(numbers[i]<numbers[i+1])
	{
	minimum=numbers[i];
	cout << "minimum = " << minimum << endl;
	}
	}
}

No.You will have to test every element of the array against the next one with a conditional operator like >.While looping , if the first will be greater than the second they will swap places thus if 34 was in front of 5 they will pe rearranged so the least will preced the greatest.The loop will run until all the elements are sorted in an ascending order so the first element of the array will be the least while the last element of the array will be the greatest.
ex:
4,343,1,27,54
will look lile
1,4,27,54,343

like this:

#include <iostream>
using namespace std;

void main()
{
	int minimum;
	int numbers[4]={10,12,10,14};
	for (int i=0; i<4; i++)
	{
	if(numbers[i]<numbers[i+1])
	{
	minimum=numbers[i];
	cout << "minimum = " << minimum << endl;
	}
	}
}

This isn't a sort, this is a straight comparison. It is similar to what I suggested you try, but not quite correct.

1. Trace your code by hand, what happens when i == 3? You end up trying to access numbers[4] which does not exist. Use i to determine which iteration of the loop you are on. If you determine that it is the first iteration of the loop, store numbers directly to minimum. On the later iterations, use the comparison statement and compare numbers to the current value of minimum.

begin for loop
  if first iteration of loop detected
    assign first array element to minimum
  else
    if current element less than current minimum
      assign current element to minimum
  end if
end for
display minimum

2. Your scoping braces are not correct. Your display statement for the minimum is inside your if statement which is nested inside your for loop. This statement should be independent and not execute repeatedly or conditionally. It should only execute once and its execution should be guaranteed. Move the statement outside your for loop completely.

If all you want is to select the maximum and minimum, only one iteration is sufficient.

1. First assign the first element of array as min and max.
While iterating,
2. compare if the current value is less than min. If yes, replace the min value
3. compare if the current value is greater than max. If yes, replace the max value
end of iteration

You're done.

commented: Yes. +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.