Solved

Loop Question

Posted on 2002-03-10
18
262 Views
Last Modified: 2010-04-01
Instead of using to for loops to initialize a 2D array is there another way to initialize a 2D array?
0
Comment
Question by:Andy36
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +8
18 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 25 total points
ID: 6854731
initializers.

For example:

int x[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,};
0
 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6854735
Same as jhance 's comment, here is other example for array 2D


int Arr[2][3] =
{
   { 3 , 5, 7 },
   { 1, 10, 20 }
};

 
0
 

Author Comment

by:Andy36
ID: 6854776
I mean is there a different way you can initialize a 2d array with some other kind of loop like a while or do while?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Andy36
ID: 6854792
I mean is there a different way you can initialize a 2d array with some other kind of loop like a while or do while?
0
 
LVL 32

Expert Comment

by:jhance
ID: 6854794
Just what _ARE_ you asking?  Loops are loops.  

for, do, while, etc. are all pretty much different words for the same thing.  You really have 3 choices:

1) A loop.
2) An initializer.
3) Straight line code.

Perhaps a better question would help as this one coupled with your comment is very confusing.
0
 
LVL 5

Expert Comment

by:yonat
ID: 6854902
In some cases you can use memset(). Or is this homework?
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6855056
You can use memset like yonat said and/or you can make the array static which will initialize it to all zero values.

static int Arr[2][3];
0
 

Author Comment

by:Andy36
ID: 6855303
I Have already initrialized a 2d array like this:

     for(int i = 0; i < n; i++)
          for(int j=0; j<n; j++) Magic[i][j] = 0;
          Magic[0][(n-1)/2]=1;//Middle of first row
                         //(i,j) is the current position.

How could I initialize it with a different loop? Or is it not possible?
0
 

Author Comment

by:Andy36
ID: 6855310
What is memset()? The homework is already done with the for loops. I just wanted to see if I could write it with a different loop.
0
 
LVL 4

Expert Comment

by:MFCRich
ID: 6855486
memset(Magic, 0, sizeof(Magic));

This will fill it with NULL bytes
0
 

Expert Comment

by:wenderson
ID: 6855809
You can also, initialize the first element with an arbitrary value, so, all the others will initialized to zero, ex.:

int array[10][10] = { 1 };

Wenderson
0
 
LVL 39

Expert Comment

by:abel
ID: 6856552
If you really want to use a while-loop, you'll do the counting in it:

int i = 0;
while(i < 10)
{
    i++;
    //do inits
}

which is the same as:

for(int i=0; i<10; i++)
{
    //do inits
}


Cheers,
Abel
0
 
LVL 11

Expert Comment

by:griessh
ID: 6856850
void main()
{
  int i,j;
  int arr[4,4];
  // FOR loop
  for (i=0; i< 3; i++) {
    for (j=0; j<3; j++) {
      arr[i,j] = i+j;
    }
  }
  // do - while loop
  i = 0;
  j = 0;
  do {
    do {
      arr[i,j] = i+j;
      i++;
    }
    while (j<3);
    j++;
  }
  while (i<3);

  // while loop
  i=0;
  j=0;
  while (i<3) {
    while (j<3) {
      arr[i,j] = i+j;
      i++;
    }
    j++;
  }

  // Mix
  i=0;
  while (i<3) {
    for (j=0; j<3; j++) {
      arr[i,j] = i+j;
    }
    j++;
  }
}


======
Werner
0
 

Expert Comment

by:rtenhove
ID: 6858161
For an N x N identity matrix, you could initialize it like so:

int m[N][N];

memset(m, 0, sizeof m );

for (int i = 0; i < N; i++)
    m[i][i] = 1;
You could also initialize the matrix from a file, assuming that you serialized the contents of the matrix previously.

You're probably looking for static initialization.

int m[2][3] = {{1,2,3},{4,5,6}};Alternatively, you could do it in a single loop:

int m[M][N];
int *pm = &(m[0][0]);

for (int i = 0; i < M * N; i++ )
    pm[i] = i;


0
 

Author Comment

by:Andy36
ID: 6884502
Griessh

What is int arr[4,4];
 
Is this a 2d array? I thought all 2d arrays went like arr[4][4];
0
 
LVL 11

Expert Comment

by:griessh
ID: 6884513
Sorry, typo! Should be [4][4] ... <shame on me>

======
Werner
0
 
LVL 11

Expert Comment

by:griessh
ID: 6955328
Dear Andy36

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "jhance"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 6979063
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question