Printing an array

I have added the array which i want to print to screen but i kee on g

Page 1 of 1

1 Replies - 891 Views - Last Post: 20 May 2008 - 10:18 AM Rate Topic: -----

#1 nir03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-May 08

Printing an array

Post icon  Posted 19 May 2008 - 04:36 AM

im having problem of printing an array thats not from file but from data iv worked out using the summation. il post what i have done so far.
#include <stdio.h>

#define M 21
#define N 2


void main ()
{

	float a[M][N];
   FILE *ifp, *ofp;
   int i, j;
   float sum_x, sum_x2, sum_x3, sum_x4;
   float sum_y, sum_yx, sum_yx2;
   float delta_array[3][3], a1, a2, a3;

   for(i=0;i<M;i++)
   {
	  for(j=0;j<N;j++)
	  {
		 a[i][j]=0;
	  }
   }


   ifp=fopen("in.dat.txt", "r");


   for(i=0; i<M; i++)
   {
	   for(j=0;j<N;j++)
	  {
		  fscanf(ifp," %f", &a[i][j]);
		 printf("%9.3f\t", a[i][j]);
	  }
	  printf("\n");
   }



   for(i=0; i<M; i++)
		 {
	  sum_x+=a[i][0];
	  sum_y+=a[i][1];
	  sum_x2+=sum_x*sum_x;
	  sum_x3+=sum_x2*sum_x;
	  sum_x4+=sum_x3*sum_x;
	  sum_yx+=sum_y*sum_x;
	  sum_yx2+=sum_y+sum_x2;
	  }

   printf("\n");

   printf("The sum of x is %f\n", sum_x);
   printf("The sum of y is %f\n", sum_y);
   printf("The sum of x^2 is %f\n", sum_x2);
   printf("The sum of x^3 is %f\n", sum_x3);
   printf("The sum of x^4 is %f\n", sum_x4);
   printf("The sum of y*x is %f\n", sum_yx);
   printf("The sum of y*x^2 is %f\n", sum_yx2);

   delta_array= {{M, sum_x, sum_x2}, {sum_x, sum_x2, sum_x3}, {sum_x2, sum_x3, sum_x4}};

   for(i=0; i<3; i++)
   {
	   for(j=0;j<3;j++)
	  {
		  printf("Delta array[%f][%f]\n %f", i, j, delta_array);
	  }
	  printf("\n");
   }


   getchar();
}


so if someone can tell me what im doing wrong and what i need to do to correct it it'l help with the other 3 arrays that i need to make as well.

Regards Nir03

Is This A Good Question/Topic? 0
  • +

Replies To: Printing an array

#2 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Printing an array

Posted 20 May 2008 - 10:18 AM

Quite easy actually, assuming a is your array.
for(int y=0;y<M;y++)
{
 for(int x=0;x<N,x++)
 {
  printf(" %i ",a[y][x]);
 }
 printf("\n");
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1