0

I'm trying to write a program to count and display the heights of students taller than 60 inches. For example: I need it to count and display the number of students taller than 60 inches and display their respective heights. I am unsure of how to store the separate values and display their height. I have gotten the program to count the number of students taller than 60 inches, but I need help displaying their specific height.

#include <iostream>

using namespace std;

int main()
{
    double count60 = 0.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];
    if (height[x] > 60)
    {
        count60 = count60 + 1;
    }       
    }

    cout << "The number of students taller than 60 inches: "<< count60 << endl;
    cout << "The heights of these students are: "

    system("pause"); 
    return 0;
}
1
  • 1
    You should consider writing increments as "++x", rather than "x = x + 1". It's better style. Also, count60 should be an int, not a double. Commented Apr 17, 2013 at 2:40

5 Answers 5

3

Not sure I totally understand where your problem lies.

It's clear from the code you've given you know how to:

  • iterate through an array (your for statement for input);
  • decide if something is greater than 60 (your if statement for updating count); and
  • output a variable (your penultimate cout << statement.

Hence it should be a simple matter to combine those with something like:

for (int x = 0; x < 10; x = x + 1) {
    if (height[x] > 60) {
        cout << height << '\n';
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Just to add, using '\n' instead of endl will mean that the buffer is not flushed after each call.
Yes, and I suspect, like C, if it's going to a terminal (if it can be "determined to refer to an interactive device"), it will get flushed per line. If it's not, it doesn't need to be line-flushed. In any case it's irrelevant here since flushing is not needed (and endl is inefficient in that case).
1

Here you go... Not the best in terms of space utilization but avoids STL.

        #include <iostream>

        using namespace std;

        int main()
        {
            int count60 = 0;
            double height[10];
            double maxheight[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];
            if (height[x] > 60)
            {
                maxheight[count60] = height[x];
                count60 = count60 + 1;
            }       
            }

            cout << "The number of students taller than 60 inches: "<< count60 << endl;

            for (int i = 0; i < count60; i = i + 1)
            {
               cout<<"The heights of these students are: "<< maxheight[i] << endl;
            }

            system("pause"); 
            return 0;
        }

Comments

1

Here is the code:

#include <iostream>

using namespace std;

int main()
{
double count60 = 0.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];
if (height[x] > 60)
{
    count60 = count60 + 1;
}       
}

cout << "The number of students taller than 60 inches: "<< count60 << endl;
cout << "The heights of these students are: ";
for(int i=0;i<10;++i)
    if(height[i]>60)
        cout<<' '<<height[i];
cout<<endl;

return 0;

}

By the way, I think count60 should better be an unsigned int.

Comments

1

Try using a std::vector. They are basically a wrapper around an array and allow you to add values dynamically. In this case you would add the code:

#include <vector> // obviously with the rest of the includes.

std::vector<int> tallPeople;
for (int x = 0; x < 10; x = x + 1)
{
    if (height[x] > 60)
    {
        count60 = count60 + 1;
        tallPeople.push_back(height[x]);
    }   
}

//...

for (int num = 0; num < tallPeople.size(); num++)
{
    cout << tallPeople[num] << endl;
}

Comments

0

if you want to remove duplicates then do this

//bubble sort
for(int i=0;i<9;i++)
{
    for(int j=i+1;j<10;j++)
    {
        if(height[i]>height[j])
        {
            double temp = height[i];
            height[i] = height[j];
            height[j] = temp;
        }       
    }
}   
//print
for(int i=0;i<10;i++)
{
    if(height[i]>60 && (i==0 || height[i]!= height[i-1]))   
    {
        cout << ' ' << height[i];
    }   
}

Comments

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.