J.Doe J.Doe - 1 month ago 6
C Question

How do you print arrays of double in C?

I've looked all over the place and i'm trying to find a way to print double array in my c program. I'm not trying to print 2d array but an array that is double.

here is my code:

#include <stdio.h>
void printarray(double array[]){
int i;
for(int i=0;i<=5;i++){
printf("%.2f\n",array[i])
}
}
int main()
{
double array={1.0,2.0,3.0,4.0,5.0};

}

Answer

Working code below. You should usually pass the size of the array as a 2nd parameter. But this is the basic functionality that I guess you want happening.

#include <stdio.h>

void printArray(double array[])
{
  int i;
  for(i = 0; i<5; i++)
    {
      printf("%.2f ", array[i]);
    }
}


int main()
{

  double myArray[5] = {1.0, 2.0, 3.0, 4.0, 5.0};

  printArray(myArray);

  return 0;
}
Comments