0

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

3
Contributors
11
Replies
17
Views
6 Years
Discussion Span
Last Post by vinitmittal2008
Featured Replies
  • [code] # 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; } [/code] main … Read More

0

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?

0

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

0

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

Edited by vinitmittal2008: n/a

0

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

0

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;
}
0

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

1
# 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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.