Solved

Creating two dimensional arrays

Posted on 2003-11-02
2
474 Views
Last Modified: 2010-04-15
How would one go about creating a two dimensional array like this:
int randomeaten[300][7];
Basically I need 300  7 element arrays of numbers ranging from 2 to 10.
I suppose like this:

randomeaten[i][j] = (rand()%(11-2)+2), (rand()%(11-2)+2), (rand()%(11-2)+2), (rand()%(11-2)+2), (rand()%(11-2)+2), (rand()%(11-2)+2), (rand()%(11-2)+2);

But I'm having troubles making the for statement to create said two dimensional array.
Also, any suggestions on how I can print the array out as well would be greatly appreciated.
0
Comment
Question by:Tabris42
[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
2 Comments
 
LVL 6

Accepted Solution

by:
Ajar earned 250 total points
ID: 9668858
Tabris ,
 
You can not allocate multible elements in a single go as you are doing .

Try to understand this sample code

=================================
#include <stdio.h>
int main(int argc,char * argv[])
{
 int randomeaten[300][7],i,j;
 //ASSIGNING
 for( i = 0; i < 300;i++) for(j = 0; j < 7;j++) randomeaten[i][j]=rand()%(11-2)+2);
 //Printing
 for( i = 0; i < 300;i++) for(j = 0; j < 7;j++) printf("randomeaten[%d][%d] = %d\n",i,j,randomeaten[i][j]);
}
======================================
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9668928
What is your question about - allocating a two dimensional array OR assigning values in a two dimensional array

If your question is about creating/allocating a two dimensional array,

You can do it in 2 ways

Statically
int a[300][7] ;

Dynamicaaly
int **a ;
int i ;
a = ( int ** ) malloc ( sizeof ( int * ) * 300 ) ;
for ( i = 0 ; i < 300 ; i++ ) {
  a [i] = ( int * ) malloc ( sizeof ( int ) * 7 ) ;
}


The Dynamic approach would be better if the array dimensions are to be decided at run time. May be an input from the user


Regarding Assigning values into it
You would have to assign the value to each array location one at a time
Here's the code for it.
int i, j ;
for ( i = 0 ; i < 300 ; i ++ ) {
      for ( j = 0 l j < 7 ; j ++ ) {
           a[i][j] = ( rand ()  % ( 11 - 2 ) + 2 ) ;
      }
}

Printing
for ( i = 0 ; i < 300 ; i ++ ) {
      for ( j = 0 l j < 7 ; j ++ ) {
           printf ( "\na[%d][%d]  =  %d " i, j, a[i][j] ) ;
      }
}








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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

688 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