Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to take size of array from user and thn passing that size to user defined function?

–2 votes
asked Apr 2, 2021 by MUHAMMAD USAMA BIN SAEED (120 points)
i want to know how to take size of array from user in C language and then after taking value of size by user i want to pass that size to a function made outside main function.

2 Answers

0 votes
answered Apr 9, 2021 by Peter Minarik (86,040 points)

I'm not exactly sure what you're after. Your question wasn't clear enough for me, sorry.

I made a small program that asks the user to provide the size of the array (no check is done on the input, the user can enter anything silly, not even numbers). Then this number is used to initialize an array with this size. Then I fill the array with some data (Fibonacci-sequence, because why not?) then read it.

A very important thing to note here: the variable myArray has a limited lifetime: it can only be accessed (directly or via a pointer) for the duration of its scope (while SetSize() hasn't terminated).

Another note: you only need what's highlighted, the rest is just me playing around and demonstrating that the array works.

#include <stdio.h>

static void PutFibonacci(unsigned long * array, size_t size)
{
    for (size_t i = 0u; i < size; i++)
    {
        switch (i)
        {
            case 0u: array[i] = 0ul; break;
            case 1u: array[i] = 1ul; break;
            default: array[i] = array[i - 1] + array[i - 2]; break;
        }
    }
}

static void PrintArray(unsigned long * array, size_t size)
{
    for (size_t i = 0u; i < size; i++)
        printf("F(%lu):\t%lu\n", i, array[i]);
}

static size_t GetSizeFromUser()
{
    size_t size;
    printf("Please, enter the size of the array: ");
    scanf("%lu", &size);
    return size;
}

static void SetSize(size_t size)
{
    unsigned long myArray[size];
    
    // Do something with it. I'll just put the Fibonacci-sequence in the array then read them back
    PutFibonacci(myArray, size);
    PrintArray(myArray, size);
}

int main()
{
    size_t size = GetSizeFromUser();
    SetSize(size);
}

If this is not quite what you were after, please try to be more specific of what you're trying to achieve. Maybe write some code and we can help when it doesn't work correctly.

0 votes
answered May 6, 2021 by 2005 A41104 (190 points)
#include<stdio.h>

int main()

{ int a[10], n;

printf("enter the size of array : );

scanf("%d",&n);

printf("enter array elements : ");

for(i=0;i<n;i++)

{

scanf("%d",&a[i]);

}

printf("array elements are : ");

for(i=0;i<n;i++)

{

printf("%d",a[i]);

}

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...