954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ excercise and my solve ..!

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?

iammfa
Light Poster
47 posts since Apr 2009
Reputation Points: 3
Solved Threads: 0
 

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

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

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

caut_baia
Posting Whiz
387 posts since Apr 2010
Reputation Points: 25
Solved Threads: 49
 

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];
  }
caut_baia
Posting Whiz
387 posts since Apr 2010
Reputation Points: 25
Solved Threads: 49
 
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;
	}
	}
}
iammfa
Light Poster
47 posts since Apr 2009
Reputation Points: 3
Solved Threads: 0
 

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

caut_baia
Posting Whiz
387 posts since Apr 2010
Reputation Points: 25
Solved Threads: 49
 

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. Usei to determine which iteration of the loop you are on. If you determine that it is the first iteration of the loop, store numbers[i] directly to minimum. On the later iterations, use the comparison statement and compare numbers[i] 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.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

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.

chiwawa10
Junior Poster
156 posts since Jul 2005
Reputation Points: 88
Solved Threads: 27
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You