Hi I need to have an array in my program that is defined by the user. I just need the user to enter the size of the array and then enter the integer values that they want to store in the array.
I know how to declare the array myself, and Ive looked around and I cant find a straightforward solution to my problem. Any help/suggestions will be greatly appreciated.
Thankyou
TrueCoding

scanf is the input function in C. You'll want to ask the user for the size of the array, and then have a loop that reads their numbers. Do you know how to do a loop?

Hmm never heard of scanf how would you use that?
I do know about loops but how would you use a loop to read their numbers?
Sorry but Im new to this - Thankyou for the quick reply

if you are not new to C language then you can use malloc or calloc function to create your Arrays..

I am new to C language and I have no idea what those are functions? Will it be easier to use?

it's easier to use but cause you are new then you should use predefined arrays.

Ok thanks so can you help me because I still dont understand?

ok

# define MAX 100

int main(void)
{
  int array[MAX],n;
  // scan array size from user but it must not be greater than MAX
  scanf("%d",&n);
  // now run your loop from 0 to less than n and scan array values

return 1;
}

Thanks for the quick reply...What loop would you suggest? A for loop? Im not sure how to use the loop to scan the numbers?
Also what does Return 1 do?
Thanks again

# include <stdio.h>
# define MAX 100
int main(void)
{
    int array[MAX],i,n;
    
    printf("\n Enter Array size (1 - %d):",MAX);
    
    scanf("%d", &n);
    
    for(i = 0; i < n; i++)
    {
       printf("\n Enter %d value :",i+1); 
       scanf("%d", &array[i]);
    }  
    
    // whatever else you want to do....... 

    return 1;
}

main function returns a integer vaule.. that is why return 1 is used.

Thankyou very much I understand now!Ill mark as solved Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.