Array question

Hello. I'm just a beginner. Here is my problem. I have a data file with an unknown number of integers. I initialized an array of 100. I then input the file into the array. I know for a fact the elements of the file are less than 100. Now, I need to sort the array using bubble_sort but how can I avoid the zero values within the array? I will need to find the median of the sorted array. The only thing I need guidance on is how to modify the array with 100 to whatever the number of actual elements is? Is it possible? Thank you!

Do you know how to use std::vector? Unlike arrays, vectors can be resized. If you're trying to learn programming this would be a good time to learn about vectors. If you're just trying to get the assignment done, why not keep track of the number of integers being read from the data file? So, if you know that you only took x number of integers in from the data file, you can perform your sort only on the first x members of your array.
yes its possible. If you need to store data without knowing how much data you will need to reserve memory for, then u need dynamic memory allocation .

This is simply implemented with C++ vector .
e.g

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector> //for vector

std::vector<int> myVector;

int a;
std::cout<<"Enter integers( 0 to end)"<<std::endl;
std::cin>>a;

while( a != 0 )
{
    myVector.push_back(a);
    std::cin>>a;
}


At the end, the size of my vector is the number of inputs entered.
vectors can be manipulated exactly like arrays.

additional reference:
http://www.cplusplus.com/reference/vector/vector/
Last edited on
thank you hyperfine...that's exactly what I did and it worked out fine. Thank you shadowCODE. I had to use an array in this assignment but I appreciate the info about vectors. Thanks again!
Topic archived. No new replies allowed.