Array Help/Question

Hello,
It must be array week I see a lot of recent questions in this forum that have to do with arrays! Unfortunately, I don't see anything that will be helpful to me! I need to make the below function know how many values in the array contain useful data. I know an extra parameter is needed, but I am just not quite sure how to do this. Right now when I run this it shows me all 10 spots, even though I only entered 3 values into my program.

Any advice or links to any information on this would be greatly appreciated!

1
2
3
4
5
6
//prints array values to screen
void printValues(double array[], double length)
{
     for(int i=0; i<length; i++)
     cout<<i<<array[i]<<endl;

If you entered 3 values, pass 3 for length.
length should be an integer. you can't get the 3.5th element of an array.
if zero is considered "useful" data then it would be better to use std::vector to achieve what you want

1
2
3
4
5
6
7
std::vector<double> Values;

void printValues(std::vector<double> &V)
{
  for(int ord = 0, std::vector<double>::iterator idx = V.begin(); idx != V.end(); ++idx, ++ord)
    std::cout << ord << *idx << endl;
}


otherwise just check if array[i] is zero (assuming this indicates you didn't enter any more data into the array) and break the loop.
Topic archived. No new replies allowed.