Array question....please help

5. Write a loop that will initialize the values in a 100-element floating-point array named x with the square root of the element number. For example, element 25 should be initialized with the square root of 25, which is 5.
Last edited on
closed account (3qX21hU5)
Ok so what are you having trouble with? You need to ask a question... Also please post what you have done and we will try and push you in the right direction. Remember to use codetags (The <> under format) to make your code readable on the forum.
this was one of the questions for an assignment and im not sure exactly what loop i should use. also im not use how to initialize element 25 as 5. all i have to do is write the loop i dont need any declarations because its not needed for the question i just dont know how to initialize 25 as 5 or which loop to use thats all if you could help that would be great.
closed account (3qX21hU5)
Ok what you are going to need is this. I'll give you the basic framework and you can figure out the logic and how it goes together.


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
int main ()
{
    // Your array
    const unsigned arraySize = 100;
    float numbers[arraySize];
    
    // You can do this with any loop really but I chose the for loop
    // though what loop you choose is up to you. This is where you assign
    // everything.
    
    // Use size_t when dealing with array indexs instead of int
    for (size_t index = 0; index != arraySize; ++index) 
    {
        // This is where you assign each element their square root.
        
        // To assign each element I would use subscripts the [].
        
        // The last problem is how to find a square root of each element.
        // To do this look into the sqrt() function that should help greatly
        // If you have to make your own figure out a algorithm to do it.
        
        // Once you know how to square all you need to do is find out the
        // Square root of whatever number index is and that will assign each
        // element in your array its sqaure root.
    }
}


If you get stuck on anything let me know and I will help answer any questions you have.

The key things to think about and learn are these.

1) Look at how the for loop works and runs. Make sure you understand how it uses index to access each element of the array.

2) Make sure you know how array subscripts work (The [] ).

3) If you don't have to make your own square root function, look up how sqrt() works and make sure you understand what it does and how it does it.

Hope this helps a bit and again let me know if you get stuck anywhere or have any questions
Last edited on
I figured it out after you explained it, thank you!
Topic archived. No new replies allowed.