Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,916 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,592 people online right now. Registration is fast and FREE... Join Now!




Printing an array

 
Reply to this topicStart new topic

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

nir03
19 May, 2008 - 03:36 AM
Post #1

New D.I.C Head
*

Joined: 14 May, 2008
Posts: 3

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.
CODE
#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
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Printing An Array
20 May, 2008 - 09:18 AM
Post #2

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
Quite easy actually, assuming a is your array.
CODE

for(int y=0;y<M;y++)
{
for(int x=0;x<N,x++)
{
  printf(" %i ",a[y][x]);
}
printf("\n");
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:59AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month