Reputation: 25
So, I'm trying to recall an array generated in one function into another function to work with it. Now, I've looked around and I can see that you can't call an entire array, but can call its pointer. I am not entirely sure how to do this and my attempts have led me to failure.
So I come to you, here is what my code looks like so far (or at least the two functions I am currently trying to work with):
int CreatePermutation(int n){
int arr[25];
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
while(n<=0){
printf ("Please input a possitive number for a permutation size.\n");
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
}
for (int i=0; i<n; i++)
arr [i] = i+1;
return arr[25];
}
int main (){
int arr[25];
int n;
CreatePermutation(n);
for (int i=0; i<n; i++)
printf("%d", arr[i]);
}
When I tried the CreatePermutation as a main function to see if it worked, it was generating and printing the permutations correctly, so all I really need is a way to get it into the main function.
Upvotes: 0
Views: 3884
Reputation: 3984
I will show simple example to make things clearer , you can get a clue from it and apply in your problem .
If you want then you can do it like this :
#include <stdio.h>
int CreatePermutation(int arr[] , int n)
{
for ( int i = 0; i < n; i++)
arr [i] = i+1;
return arr[n-1];
}
int main ()
{
int arr[25];
int n ;
printf ("Please enter number of elements to be printed : ");
scanf ("%d", &n);//You should enter the number of elements before passing it to the function and in this way you can limit the number of elements you want to print .
CreatePermutation ( arr , n );
for (int i = 0; i < n; i++)
printf ("%d\n" , arr[i] );
}
NOTE-- My example shows a simple way to pass array as per your intention , you can further adjust it according to your needs :)
Upvotes: 0
Reputation: 27812
You can pass the array as an argument:
int CreatePermutation(int *arr){
int n;
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
while(n<=0){
printf ("Please input a possitive number for a permutation size.\n");
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
}
for (int i=0; i<n; i++)
arr [i] = i+1;
return n;
}
int main (){
int arr[25];
int n;
n = CreatePermutation(arr);
for (int i=0; i<n; i++)
printf("%d", arr[i]);
return 0;
}
This way, you can modify the array using the array pointer. You can also return the size of the permutation in your CreatePermutation
function.
Upvotes: 2
Reputation: 2520
I think you want something like this:
int CreatePermutation(int n, int* arr)
{
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
while(n<=0){
printf ("Please input a possitive number for a permutation size.\n");
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
}
for (int i=0; i<n; i++)
arr [i] = i+1;
return arr[n-1];
}
int main (){
int arr[25];
int n = 25; /* Update thanks to PHIFounder */
CreatePermutation(n, arr);
for (int i=0; i<n; i++)
printf("%d", arr[i]);
}
Upvotes: 1