### Arrays

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.

This code is the code from program 1:
 ``123456789101112131415161718192021222324252627282930313233`` ``````#include using namespace std; int main() { double height[10]; int tall=0; for(int x=0;x<10;x++) { height[x] = 0.0; } cout<<"Enter the height of 10 students."<>height[x]; if(height[x]>60) { tall++; } } cout<<"Number of students taller than 60 inches: "<

Input:
62 61 59 64 60
54 57 53 60 55

Output has to look like this:
 ``` Heights of students taller than 60 inches: 62 61 64 Number of students taller than 60 inches: 3 ```
use a for loop to compare the heights with 60
I'm sorry I don't understand how to construct a loop to store the numbers for recall.
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:

int a=0;

for (int i=0; i<10; i++){

if (height[i] >60){
array[a] = height[i];
a++;
}

}

Note! Not tested!
I kinda understand what you are saying Hekri but for some reason when I build it, it gets stuck in the loop. Any suggestions?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include using namespace 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."<>height[x]; if(height[x]>60) { tall++; } for(int x=0; x<10; x++) { if(height[x]>60) { tallest[a]=height[x]; a++; } } } cout<<"Number of students taller than 60 inches: "<

Try to refrain using a namespace.
Here's a bit of code I made up to get you going in the right direction.

 ``12345678910111213141516171819202122`` ``````#include int main() { unsigned short student_heights[20]= {0}; unsigned short height_index[20]= {0}; unsigned short height_index_size= 0; std::cout << "configuring heights" << std::endl; for(unsigned i=0; i<20; ++i) { student_heights[i]= i*4; if(i*4 > 60) height_index[height_index_size++]= i; } for(unsigned i=0; i 1-20 << " is " << student_heights[height_index[i]] << "inches"<< std::endl; } }``````
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.
Could someone please explain to me why my instructor uses this for loop:
 ``1234`` ``````for(int x=0;x<10;x++) { height[x] = 0.0; }``````

in the very beginning of program 1? I think this part is really tripping me up.
Last edited on
He/She is initializing all the array elements to 0 because if you don't initialize they will contain garbage values.

Your code looks fine to me. You just need a for loop to print out all the elements of the array "tallest".
Here:

for (int i=0; i<3; i++){

cout<<tallest[i]<<" ";
}

like it is your code seems to print only the last member of the array "tallest".
Last edited on
Ok gave your idea a try hekri but i'm not getting the correct output. Do i have it in the wrong place?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include using namespace 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."<>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<

 ``` 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. ```

plus: the program crashes after completion.
Last edited on
If you really intend learning C++ you should find some good C++ books!

Here you have it:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` `````` #include using namespace std; int main() { double height[10]; int tall=0; double tallest[10]; 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."<>height[x]; } int a=0; for(int x=0; x<10; x++) { if(height[x]>60) { tallest[a]=height[x]; a++; } } cout<<"Number of students taller than 60 inches: "<
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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````// Lab1301.cpp – read and display heights of students taller than 60 inches // Created by Taft Sanders on 11/9/12 #include using namespace 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."<>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: "<60) { cout<<"Heights of students taller than 60 inches: "<

 ``` 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. ```
Lol. Just copy and paste the code I wrote and try it.
Last edited on
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?
NM, just add a simple if statement. Fixed!

Thank you everyone for their help. Props to hekri! I have a better understanding of arrays thanks you.
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.
Topic archived. No new replies allowed.