Preforming operations on an array

This is a discussion on Preforming operations on an array within the C Programming forums, part of the General Programming Boards category; For my assignment I have to have an array with only zeros. Code: int a[20] = { 0, 0, 0, ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    2

    Preforming operations on an array

    For my assignment I have to have an array with only zeros.

    Code:
    int a[20] = { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0 };
    Then I need to send it into a function that makes the array like this

    Code:
    int a[20] = {0,1,2,3,4,5,6, ... , 19}
    Which I have done here

    Code:
    int initialize(int a[], int n)
    
    {
        int m = 0;
        int i;
        
        printf("\n");
        printf("Now the array is \n");
        
               
        for (i=0; i<20; i++ )
        {   
            
            a[i] = a[i] + m;
            
            m = m + 1 ;
            
            printf ("%3d", a[i]);
            
            }
            
            
    }
    Now I need to do the following with the array.

    I need to take whatever value is in each position and add that value to all of the previous values.

    like this.

    Code:
    a[3] = a[3] + a[2] + a[1] + a[0]
    
    only for every a[i]
    I know that I can code this the long way, but I just can't see to be able to find out how to do this a better way.

    Thanks.

    by the way its my first post.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,160
    For the first one you could have just done:
    Code:
       for (i=0; i<20; i++ )
            a[i] = i;
    For the next one, you'll need to use a nested loop (a loop within a loop).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BCD operations
    By ikki in forum C Programming
    Replies: 4
    Last Post: 01-02-2013, 02:20 PM
  2. Aritmethic operations on Stack (Array implementation)
    By ilerleartik in forum C Programming
    Replies: 12
    Last Post: 03-11-2012, 06:24 AM
  3. Array Operations
    By cswimmer in forum C Programming
    Replies: 5
    Last Post: 02-21-2011, 03:22 PM
  4. doing floating operations using integer operations
    By ammalik in forum C Programming
    Replies: 10
    Last Post: 08-15-2006, 04:30 AM
  5. Replies: 3
    Last Post: 03-23-2002, 03:20 PM

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