Dave or someone please help

This is a discussion on Dave or someone please help within the C Programming forums, part of the General Programming Boards category; Originally posted by Dave_Sinkula Code: --------------------------------------------------------------------------------/* calculation description Y[0] = x(0) * x(0) + x(1) * x(1) + x(2) * ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    38

    Dave or someone please help

    Originally posted by Dave_Sinkula

    Code:
    --------------------------------------------------------------------------------/* calculation description
    Y[0] = x(0) * x(0) + x(1) * x(1) + x(2) * x(2) + x(3) * x(3)
    Y[1] = x(0) * x(1) + x(1) * x(2) + x(2) * x(3)
    Y[2] = x(0) * x(2) + x(1) * x(3)
    Y[3] = x(0) * x(3)
    */
    #include <stdio.h>
    
    #define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))
    
    int main(void)
    {
       int x[] = {1,2,3,4}, y [ ARRAYSIZE(x) ];
       size_t i,j;
       for(j = 0; j < ARRAYSIZE(y); ++j)
       {
          y[j] = 0;
          printf("y[%d] =", (int)j);
          for(i = j; i < ARRAYSIZE(x); ++i)
          {
             printf("%s x[%d] * x[%d]", i != j ? " +" : "", (int)(i - j), (int)i);
             y[j] += x[i - j] * x[i];
          }
          printf(" = %d\n", y[j]);
       }
       return 0;
    }
    
    /* my output
    y[0] = x[0] * x[0] + x[1] * x[1] + x[2] * x[2] + x[3] * x[3] = 30
    y[1] = x[0] * x[1] + x[1] * x[2] + x[2] * x[3] = 20
    y[2] = x[0] * x[2] + x[1] * x[3] = 11
    y[3] = x[0] * x[3] = 4
    */--------------------------------------------------------------------------------
    Dave,
    How about if x[] is not as {1,2,3,4},but is the numerical data stored in a file line by line? how to perform such shifting?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Simple, read a line into an array, and then perform whatever you need to on it. Read the next line, and so on.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seeking insight into a link Dave provided to someone
    By cdalten in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 11:13 AM
  2. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-30-2005, 12:24 AM
  3. Happy Birthday Dave!
    By Stack Overflow in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 03-10-2005, 01:28 AM
  4. TELL me WHY!!!
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-09-2003, 08:31 AM
  5. ?!? wierd. displaying scripts
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-12-2002, 10:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21