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

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




Wont print summation

 
Reply to this topicStart new topic

Wont print summation

nir03
15 May, 2008 - 02:38 AM
Post #1

New D.I.C Head
*

Joined: 14 May, 2008
Posts: 3

CODE
#include <stdio.h>

#define M 21
#define N 2

float sum_array(float a[][N], float nrows)
{
   int i,j, xsum=0;
   for (i=0; i<nrows; i++)
   {
       for(j=0;j<N;j++)
      {
             xsum += a[i][j];
       }
   }
   return(xsum);
}

void main ()
{
    float a[M][N];
   FILE *ifp, *ofp;
   int i, j;
   float sum_x, sum_y;

   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");
   }

   getchar();

   for(i=0; i<M; i++)
       {
      sum_x+=a[i][0];
      sum_y+=a[i][1];
      }
    printf("The sum of x is %f\n", sum_x);
   printf("The sum of y is %f\n", sum_y);
}


well in this code i want to print an array from another file in this case in.dat. i did that fine but i am having problems adding the columns of the array, which i want to be called x and y. it will be helpful if it was a function but im not sure if the function i wrote was correct and how i should add it to the main.
User is offlineProfile CardPM
+Quote Post

jeronimo0d0a
RE: Wont Print Summation
15 May, 2008 - 07:07 AM
Post #2

D.I.C Head
**

Joined: 3 Mar, 2008
Posts: 141


My Contributions
It does print the sums of the columns, but you have your getchar() before that so they don't stay on screen. Are you trying to line them up under the columns? If you could clarify the question I'd be happy to help.
User is offlineProfile CardPM
+Quote Post

nir03
RE: Wont Print Summation
19 May, 2008 - 01:40 AM
Post #3

New D.I.C Head
*

Joined: 14 May, 2008
Posts: 3

QUOTE(jeronimo0d0a @ 15 May, 2008 - 08:07 AM) *

It does print the sums of the columns, but you have your getchar() before that so they don't stay on screen. Are you trying to line them up under the columns? If you could clarify the question I'd be happy to help.


thanx but when i looked at it again i noticed the getchar(), but now 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

Reply to this topicStart new topic
Time is now: 12/1/08 09:16AM

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