Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
A.Avinash_Goutham
Group Title
can any1 help me regarding "passing multidimensional arrays"?
 2 years ago
 2 years ago
A.Avinash_Goutham Group Title
can any1 help me regarding "passing multidimensional arrays"?
 2 years ago
 2 years ago

This Question is Closed

A.Avinash_Goutham Group TitleBest ResponseYou've already chosen the best response.0
i have a dynamic array and i have to fill it with random values............. so i have to do smthin like randomAssign(&array,size) void randomAssign(int ***array,int size) { int i,j; for(i=1...size) for(j=1...size) array[i][j]=rand(); return; } but this gives a segmentation fault? so wer did i go wrong?
 2 years ago

A.Avinash_Goutham Group TitleBest ResponseYou've already chosen the best response.0
@nick67 @slotema
 2 years ago

A.Avinash_Goutham Group TitleBest ResponseYou've already chosen the best response.0
array is of type int ** int **array; array = builder(size); and my builder function is here int ** builder(int n) { int i; int *array=malloc(n*sizeof(int)); for(i=0;i<n;i++) array[i]=(int *)malloc(n*sizeof(int)); return array; } language s c
 2 years ago

A.Avinash_Goutham Group TitleBest ResponseYou've already chosen the best response.0
@ktobah
 2 years ago

ktobah Group TitleBest ResponseYou've already chosen the best response.0
You know when you want to pass an Array as argument, don't use &, because C/C++ consider arrays as addresses. Try this .
 2 years ago

A.Avinash_Goutham Group TitleBest ResponseYou've already chosen the best response.0
nope tried that.............it dint work
 2 years ago

A.Avinash_Goutham Group TitleBest ResponseYou've already chosen the best response.0
any ideas?
 2 years ago

ktobah Group TitleBest ResponseYou've already chosen the best response.0
I really don't have any idea, sorry.
 2 years ago

A.Avinash_Goutham Group TitleBest ResponseYou've already chosen the best response.0
tsk thanks nyway.......
 2 years ago

ktobah Group TitleBest ResponseYou've already chosen the best response.0
You're welcome.
 2 years ago

nick67 Group TitleBest ResponseYou've already chosen the best response.0
@A.Avinash_Goutham try something like this (with this example you have a 10x10 array with random values up to 99: #include <iostream> #include <ctime> #include <cstdlib> using namespace std; #define ARRAY_SIZE 10 void randomAssign(int *array_ptr,int size) { int i,j; srand ( time(NULL) ); for(i=0;i<size;i++) for(j=0;j<size;j++) { *array_ptr = rand() % 100; array_ptr++; } return; } int main() { int array[ARRAY_SIZE][ARRAY_SIZE]; randomAssign(&array[0][0], ARRAY_SIZE); return 0; }
 2 years ago

A.Avinash_Goutham Group TitleBest ResponseYou've already chosen the best response.0
Sir, the array should be dynamic...........i shud be able to give the size at the run time.....
 2 years ago

A.Avinash_Goutham Group TitleBest ResponseYou've already chosen the best response.0
main() { int n,i,j; printf("enter the size of the maze:"); scanf("%d",&n); static int **array; array=builder(n); if(array) { printf("array creation succesfull\n"); } printf("%d\n",rand()); assign(array,n); randomAssign(array,n); print(array,n); } int ** builder(int n) { int i; int *array=malloc(n*sizeof(int)); for(i=0;i<n;i++) array[i]=(int *)malloc(n*sizeof(int)); return array; } void print(int **array,int n) { int i,j; for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%d ",array[i,j]); printf("\n"); } return; } void assign(int **array,int n) { int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) { printf("enter element array[%d][%d]:",i,j); scanf("%d",&array[i][j]); } return; } void randomAssign(int **array,int n) { int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) array[i][j]=rand()%2; return; }
 2 years ago

A.Avinash_Goutham Group TitleBest ResponseYou've already chosen the best response.0
that was my code..
 2 years ago
See more questions >>>
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.