Last element in an array...

The task:
1. Create an array "pipes_thickness" with size 200
2. Assign a random value for every element of the array (range: 1-10)
3. Input a value "thickness0"
4. Find the last element of the array that is less or equal to "thickness0"
5. Display results on the screen.

Sooo, the thing is that when I display the results I get an enormous number (...*10^255 magnitude etc.) and that's surely wrong so I'd need help to fix that. Also I'm not sure about the "Last element of the array". What should I do on this one.

Thank You.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main()
{
    cout << "8B Quiz" << endl;
    const int pipe_thickness_amount=200;
    double pipe_thickness_array[pipe_thickness_amount];
    for(int i=1; i<pipe_thickness_amount; i++)
    {
        pipe_thickness_array[i]=rand()%10+1;
    }
    double thickness0, value;
    cout<<endl<<"input thickness: "; cin>>thickness0; cout<<endl;
    for(int i=1; i<pipe_thickness_amount; i++)
    {
        if(pipe_thickness_array[i]<=thickness0)
        {

        }
    }
    cout<<"Final pipe thickness is: "<<value++<<endl;


    return 0;
}
You are outputting value.

What number have you stored in value? Let's look.

double thickness0, value; It is created with nothing set. It will be whatever random number happens to be in the memory. Could be anything.

Then, what do you do with value?

You only change it once; value++, where you add 1 to it.

So when you output value, what will it be? Remember, it started as some random amount in memory, and you added one to it, so what will it be?

@Repeater, when I tried setting the value to pipe_thickness_array an error occoured so I'm not sure to what quantity should I declare the value to.
The task doesn't actually state what the results are, but you have two possible numbers of interest.

1) The index of the last element of the array that is less or equal to "thickness0"
2) The value of that element

Why not set it to the value of the last element of the array that is less or equal to "thickness0"?
Also, your code skips the first element of the array. That's a mistake.
Topic archived. No new replies allowed.