A community for students. Sign up today!
Here's the question you clicked on:
 0 viewing

This Question is Closed

A.Avinash_Goutham
 2 years ago
Best ResponseYou've already chosen the best response.0i 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?

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

A.Avinash_Goutham
 2 years ago
Best ResponseYou've already chosen the best response.0array 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

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

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

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

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

nick67
 2 years ago
Best 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; }

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

A.Avinash_Goutham
 2 years ago
Best ResponseYou've already chosen the best response.0main() { 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; }

A.Avinash_Goutham
 2 years ago
Best ResponseYou've already chosen the best response.0that was my code..
Ask your own question
Ask a QuestionFind more explanations on OpenStudy
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.