So I have this problem with a program involving arrays. The problem is:
Modify your program in Program 1. In addition to count and show the number of students taller than 60 inches, also display the heights of these students (i.e. students who are taller than 60 inches). For example, suppose 3 students in this class are taller than 60” and their heights are 62”, 61” and 64”. Display the count (i.e. 3) and the heights 62, 61 and 64.
How do I go about using an array to store my numbers of the students that are taller than 60" to be able to recall them later.
Create another array like this for example: array[3] and then use a for loop to assign the members of the array you created (array[3]) with the members of the "height" array. Write something like this:
Uhm.. thanks for the help Nexius but your code is very confusing to me. This is my first semester in C++ and I'm at a loss to even know where to begin with your example.
I'm not just looking for an answer to my problem, but I'm looking for how to understand it so that this won't be a problem again.
#include <iostream>
usingnamespace std;
int main()
{
double height[10];
int tall=0;
double tallest[10];
int a=0;
for(int x=0;x<10;x++)
{
height[x] = 0.0;
}
for(int a=0;a<10;a++)
{
tallest[a]=0.0;
}
cout<<"Enter the height of 10 students."<<endl;
for(int x=0; x<10; x++)
{
cout<< "Enter the height of the student: ";
cin>>height[x];
if(height[x]>60)
{
tall++;
}
for(int x=0; x<10; x++)
{
if(height[x]>60)
{
tallest[a]=height[x];
a++;
}
}
for (int a=0; a<3; a++)
{
cout<<tallest[a]<<" ";
}
}
cout<<"Number of students taller than 60 inches: "<<tall<<endl;
cout<<"Heights of students taller than 60 inches: "<<tallest[a];
}
Enter the height of 10 students.
Enter the height of the student: 62
62 0 0 Enter the height of the student: 61
62 62 61 Enter the height of the student: 59
62 62 61 Enter the height of the student: 64
62 62 61 Enter the height of the student: 60
62 62 61 Enter the height of the student: 54
62 62 61 Enter the height of the student: 57
62 62 61 Enter the height of the student: 53
62 62 61 Enter the height of the student: 60
62 62 61 Enter the height of the student: 55
Process returned -1073741819 (0xC0000005) execution time : 23.183 s
Press any key to continue.
OK I'm slowly making progress. I got the code to work like I want it to, somewhat... but the only thing is that it is printing out the last height over 60, 10 times. Instead I need the 3 heights that are over 60 printed in one sentence.
// Lab1301.cpp – read and display heights of students taller than 60 inches
// Created by Taft Sanders on 11/9/12
#include <iostream>
usingnamespace std;
int main()
{
double height[10];
int tall=0;
double tallest[10];
int a=0;
for(int x=0;x<10;x++)
{
height[x] = 0.0;
}
for(int a=0;a<10;a++)
{
tallest[a] = 0.0;
}
cout<<"Enter the height of 10 students."<<endl;
for(int x=0; x<10; x++)
{
cout<< "Enter the height of the student: ";
cin>>height[x];
if(height[x]>60)
{
tall++;
}
for(int x=0; x<10; x++)
{
if(height[x]>60)
{
for(int a=0;a<10;a++)
{
tallest[a]=height[x];
}
}
}
}
cout<<"Number of students taller than 60 inches: "<<tall<<endl;
for(int a=0;a<10;a++)
{
if(tallest[a]>60)
{
cout<<"Heights of students taller than 60 inches: "<<tallest[a]<<" ";
a++;
}
}
}
Enter the height of 10 students.
Enter the height of the student: 61
Enter the height of the student: 62
Enter the height of the student: 63
Enter the height of the student: 0
Enter the height of the student: 0
Enter the height of the student: 0
Enter the height of the student: 0
Enter the height of the student: 0
Enter the height of the student: 0
Enter the height of the student: 0
Number of students taller than 60 inches: 3
Heights of students taller than 60 inches: 63 Heights of students taller than 60
inches: 63 Heights of students taller than 60 inches: 63 Heights of students ta
ller than 60 inches: 63 Heights of students taller than 60 inches: 63
Process returned 0 (0x0) execution time : 7.621 s
Press any key to continue.
Wow I totally skipped your code you wrote hekri. After posting up here so much I thought it was code of my own. But the good news is that I have almost what I need. I was looking over your code and saw you set a<3. This program has to be adaptive to change, not just set to 3. So it would have to be 10.
But when I do change it to 10 it gives me all 10 values of tallest[a]. How do I get only the largest values?
I didn't have much time to write my code, so forgive me for not answering to your queue earlier. The example I posted was intended to show you proper handling of arrays. Allow me to explain my code, starting at the main function.
The first two lines declare an an array of 20 elements of unsigned short, a 2 byte memory type, so it's 40 bytes of memory each, and initialize each element to zero. An array, as you may have guessed, is just a block of reserved memory on the stack. The third line only declares and initializes the variable.
Each of those have a specific purpose.
student_heights is holds the heights of all the clients. height_index holds the indexes of the heights (within studen_heights) that are above 60. height_index_size only serves to tell how many students are >60 inches.
I use a loop to set the heights of the students and record who is higher than 60 accordingly, student_height_index grows, the index in student_heights put into the array to note THAT element is greater than 60.
In the last loop I simply read from that array accordingly. It should make sense if you follow through it, with all this in mind.