Thread: How to calc Two array

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    9

    Question How to calc Two array

    Hi,
    I have like array
    a[2][3]+b[2][3]=c[2][3]
    can some tell how to calc both in carray
    =====================

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I'm not quite sure if this is what you're asking, so I'll explain in a little more detail.

    You say you have two arrays, and you want to add each element of the similar indexes to a new array total. Like for instance if you had two weeks of data by day, you'd have each week and a total
    Code:
         Week 1       Week 2       Total
    Sun    50           62          112
    
    Mon    66           61          127
    
    Tue    45           53           98
    
    Wed    57           60          117
    
    etc...
    If this is the case, then you need to loop through your arrays and total each index at one time.
    Code:
    for(int i = 0; i < ROW; i++)
       for(int j = 0; j < COL; j++)
          arryTot[i][j] = arry1[i][j] + arry2[i][j];
    Last edited by SlyMaelstrom; 06-03-2006 at 12:31 PM. Reason: A really dumb error
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    Thanks for ur .....
    Plz I need some like this.

    A [1][1] b[1][1] C
    1 2 2 6 3 8
    4 5 + 4 2 = 8 7

    I need calc A array + arryB result in C array.
    for(i=0;i<2;i++)

    for(j=0;j<2;j++)
    cin>>[i][j] /* this will add element of A

    for(i=0;i<2;i++)
    for(j=0;j<2;j++)
    cin>b[i][j];/*** this i am add B elemnt is that correct.

    now
    How I will sum a[][]+b[][]
    then save in c[][]
    Ineed that loop




    for I need some like c


    thx
    Last edited by miziri; 06-03-2006 at 03:41 AM.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Code:
        const int arr_size = 2;
        const int arr_size2 = 3;
        int a[arr_size][arr_size2] = {2, 3, 5, 6, 8, 6};
        int b[arr_size][arr_size2] = {1, 4, 7, 5, 4, 1};
    
        int c[arr_size][arr_size2];
    
        for (int i = 0; i != arr_size; ++i) {
            for (int z = 0; z != arr_size2; ++z) {
                c[i][z] = a[i][z] + b[i][z];
            }
        }
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    Thanks alot.
    I will try that.

    Final.
    Plz

    I have this array
    A
    1 5 6
    2 7 8
    3 4 2


    exchnage
    with first line with last to be liker this

    3 4 2

    2 7 8

    1 5 6

    Thax-Iraq

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Did you try to sort the array in decending order with any one of the sort algorithms? My suggestion of course, hinges on the fact that an array[x][y] is also an array[xy], and sorting the first row would result in an order like the one you want.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    friend
    Sort wll not solve my problem in my example sort will be I want code all
    example.

    thx

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Show your attempt at writing the exchange code. It is very much like a simple swap, except you would want to loop through the columns of the rows being swapped, rather than just a simple assignment.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-01-2009, 12:06 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM