0

I'm very new to C. I want to add two one dimensional integer array name a[10] b[10]. I want to put the result in a 2dimentional array c[5][2] like

c[i][j] = a[i]+b[i];

But if I use 2 for loops then how can I access a[9],b[9] value. So I want to use a single for loop which perform a[i]+b[i] and puts the result in c[i][j].

Can anyone tell me how to do this?

  • How do I put the value in 2dimensional array using single for loop?
  • How do I print the value of 2dimensional array using single for loop?
  • Please don't mind my English or question formatting as I'm very poor at English.
2
  • Your question doesn't make sense. You say you want to put the result of a[i]+b[i] in c[i][j], but how does j affect the calculation? Commented May 31, 2011 at 9:47
  • You can also use two for loops with for (i=0; i<2; i++) for (j=0; j<5; j++) c[i][j] = a[i*5+j] + b[i*5+j]; Commented May 31, 2011 at 10:15

4 Answers 4

1

if i undersood your problem, you can do this:

int col=0; //col counter
int row=0; // row counter
for(int x=0;x<9;x++)
{
   if(col>x/2) // check the end of row
   {
    row++; // increment row
    col=0; // reset col counter
   }
    c[row][col]=a[x]+b[x]; // assignement
   col++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for helping me , veru nice logic .now will you tell me how to print the value of this array in single loop.
you can print the values in this loop or print in loop below with the same logic, using printf("%...",c[row][col])
0

This may help (if I understood right):

for ( int i = 0 ; i < 10 ; ++i)
{
     c[i%5][i/5] = a[i] + b[i] ;
}

3 Comments

Please tell me now how to print these value in single loop?
can you please tell me logic behind it
the logic behind such code is that when you are allocating arrays which are STATIC arrays (which means they go into stack not heap) you are sure to get a continues memory of cpu. When you do so you can do as I wrote. to print these values in single loop use this : printf("%d", (c[i%5][i/5] = a[i] +b[i])); if you need new lines after every (i%5=0) do so in a 'if' and print "\n".
0

Ok so you're going to have to use two for loops. This is because if you try to implement what you want with one for look c[i][j] will go up evenly, i.e. i will always equal j. This becomes a problem when you get to any number bigger than 2 as this is j's maximum size. So I would suggest the following format

    int i=0, j=0;
    int a[10], b[10], c[5][2];
      for(i=0; i<10; ++i)
      {
         for(j=0; j<2; ++j)
         {
            a[i]+b[i] = c[i][j];
         }
      }

Place your printf statments within this or after it depending on how you want it. But remember to first initialise your arrays with values otherwise they will print junk. So either use scanf and prompt the user to input values for a[10] and b[10]or else use the formata[10] = {val1, val2, val3...}`

Hope I've been helpful

1 Comment

Thanks for help .sir i want c[i][j]=a[i]+b[i].please note that i want to access a 2-d array in single loop ?Please tell me how to print the value of c[i][j] using single loop?
0

Just transform the number (0 to 9) making the index for a (or b) in base 5:

base 10 | base 5
     0 ==> 00
     1 ==> 01
     ...
     4 ==> 04
     5 ==> 10
     6 ==> 11
     ...
     9 ==> 14

then use each of the base-5 digits in the result for indexing c


Edit: pseudo example

for (k = 0; k < 10; k++) {
    b5 = base5(k);         /* when k is 7, b5 becomes 12 */
    c0 = nthdigit(b5, 0);  /* when b5 is 12, c0 becomes 2 */
    c1 = nthdigit(b5, 1);  /* when b5 is 12, c1 becomes 1 */
    c[c1][c0] = a[k] + b[k];
}

4 Comments

Thanks for help . But i didn't understand your solution at all. Please tell me detailed solution.
See my edit. Maybe that helps ... I don't really want to do your homework :)
Thanks you gave me new information .I'm just trying to learn something.Honestly I still didn't get the concept of base what is this base?
@Golu: see the Wikipedia article for all about base.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.