write a loop that will initialize in a 100-element floating point array with the square root of the element number?

0 votes
asked Mar 29 by TylerBlachowiak (230 points) 1 flag

This is what the question said:

Write a loop that will initialize 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.

I am completly lost. I need a lot of help.

2 Answers

0 votes
answered Mar 30 by Bambofy (480 points)
selected Apr 1 by TylerBlachowiak
 
Best answer

Here is my solution, which bit don't you understand?

Here is a link where you can see the code better: https://onlinegdb.com/HyD-twJDU

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include <math.h>

int main()
{
    const int numElements = 100; // this is the number of elements in the array

    float x[numElements]; // here we define 'x' which is an array of floats with size 'numElements'
    
    // here we count from 0 to 'numElements'
    for (int index = 0; index < numElements; index++)
    {
        float indexAsFloat = (float)index; // we need to convert it to a floating point type.
        
        float value = sqrtf(index); // find the square rooot of this index.
        
        x[index] = value; // the element of array x at this index is now set to value.
    }
    
    
    
    
    for (int i = 0; i < numElements; i++)
    {
        float value = x[i];
        printf("Index: %u = %f\n", i, value);
    }

    return 0;
}

commented Mar 31 by TylerBlachowiak (230 points)
Ok I understand where I was getting messed up now, but I am having trouble with the output statement. You used printf, but I can only use cout. How do you make the output statement with cout equivalent to the output statment you have?
0 votes
answered Mar 30 by kummari manoj kumar manu (140 points)
#include<stdio.h>
#include<math.h>
int
main ()
{
  float a[5];
  int i;
  int value;
  for (i = 0; i < 5; i++)
    {
      printf ("enter the numbers\n");
      scanf ("%d", &value);
      a[i]=sqrt(value);
    }
    printf("\n");
  for (i = 0; i < 5; i++)
    {
      printf("%f\n",i+1,a[i]);}
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...