0

Ok, I also need to calculate and display each height after a 5% increase using a separate 10-element array. Any ideas? Sorry about all this. This is my first time using arrays.

#include <iostream>

using namespace std;

int main()
{
    int MINheight = 0;
    double height[10];
    for (int x = 0; x < 10; x = x + 1)
    {
        height[x] = 0.0;
    }

    cout << "You are asked to enter heights of 10 students. "<< endl;
    for (int x = 0; x < 10; x = x + 1)
    {
        cout << "Enter height of a student: ";
        cin >> height[x];  
    }

    system("pause"); 
    return 0;
}
6
  • 2
    Use existing algorithms. std::min_element is right on your doorstep. Commented Apr 17, 2013 at 3:13
  • I apologize but I don't know how to use std::min_element, could someone explain? I am currently searching for an example Commented Apr 17, 2013 at 3:17
  • It's probably good if you read about iterators first. With that knowledge, and the knowledge that arrays can decay into pointers to their first element, you can use those pointers as random-access iterators for the algorithm, or use something like std::begin and std::end, which works with plain arrays, as well as the better std::array. Commented Apr 17, 2013 at 3:19
  • Ok, I also need to calculate and display each height after a 5% increase using a separate 10-element array. Any ideas? Sorry about all this. This is my first time using arrays. Commented Apr 17, 2013 at 3:34
  • Well, if you have to, you probably have to write these algorithms yourself, but keep in mind for later on that std::transform exists. In standard C++03, the best you're probably going to get is something like std::bind2nd(std::multiplies<double>, 1.05) for the transformation (can't say I've ever had to use that, so I don't know if it's completely right), but C++11 gives us nice lambdas: [](double d){return d * 1.05;}. Again, something to keep in the back of your mind as you learn the language. Commented Apr 17, 2013 at 3:38

2 Answers 2

3

Simply loop like this:

MINheight = height[0];
for (int x = 1; x < 10; x++)
{
   if (height[x] < MINheight)
   {
      MINheight = height[x];
   } 
}
std::cout << "minimum height " << MINheight <<std::endl;

Side Note: you should not name a local variable starting with Capital letter, using x as array index is also kind of strange, though they both work fine, but not good style.

You may also use std::min_element as follows:

std::cout << *std::min_element(height,height+10) << std::endl; 
                               //^^using default comparison

To put elements in separate array with increased heights and display them, do the following:

float increasedHeights[10] = {0.0};
for (int i = 0; i < 10;  ++i)
{
   increasedHeights[i] = height[i] * 1.05;
}

//output increased heights
for (int i = 0; i < 10;  ++i)
{
   std::cout << increasedHeights[i] << std::endl;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I also need to calculate and display each height after a 5% increase using a separate 10-element array. Any ideas? Sorry about all this. This is my first time using arrays.
@user2040308 see updated post? Is this what you mean by putting increased height in separated array?
1

Essentially, you can keep track of the minimum value as it is being entered, so:

cout << "You are asked to enter heights of 10 students. "<< endl;

MINheight = numerical_limits<int>::max
for (int x = 0; x < 10; x = x + 1)
{
    cout << "Enter height of a student: ";
    cin >> height[x];  
    if(height[x] < MINheight)MINheight = height[x];
}
cout << "Minimum value was: " << MINheight << "\n";

What this does is create a variable with its value the maximum possible value, then when ever a new value is entered by the user, check if it less than current minimum, if so store it. Then print out the current minimum at the end.

2 Comments

Ok, I also need to calculate and display each height after a 5% increase using a separate 10-element array. Any ideas? Sorry about all this. This is my first time using arrays.
Essentially, create a new double array, as you have before, run through the array (as above) and set each value to 1.05 * the value at the same index on the height array and print out that calculated value. Simple enough? :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.