0

I want to create an array using loop.If i don't know what the size will be how i can create this.When i don't know how many number of elements of array user will give as input what i will do then

#include <stdio.h>
int main()
{
    int n,j;
    int arr[n];
    for(j=0;j<n;j++)
    {
        scanf("%d",&arr[j]);
    }
    return 0;
}
1
  • 1
    how you gonna know user end his input Commented Mar 18, 2015 at 3:27

3 Answers 3

1

You need to scan the value of n.

int n,j; 
scanf("%d", &n);
int arr[n]; 

Read about variable length arrays.

You can use realloc to increase the size.

int *arr = NULL;
int j = 0;
do{
    arr = realloc(arr, j+1);
}while(scanf("%d", arr[j++]) == 1)
Sign up to request clarification or add additional context in comments.

5 Comments

Why notmalloc and realloc? OP may not know how many inputs to expect.
Are there any possible way when i scan the values of array it will automatically adjust size.
@misbah; What do you mean by "automatically adjust size"?
When i don't know how many number of elements of array user want to give as input what i will do then.
how inefficient is to realloc an array for each slot. try to save a little by realloc'ing only when some size trigger fires.
0

If you do not know the number of values you will be reading, you will have to dynamically allocate some memory, then allocate some more when you need it, and finally deallocate anything you are no longer using.

You have also need to check the return value of scanf to determine when to stop your loop. Here is an example.

#include <stdio.h>
#include <stdlib.h>

int main() {
// You could reallocate to allow for one extra item at a time, an
// fixed chunk at a time (as shown here), or some other strategy such
// as doubling the allocation size at each realloc
#define CHUNK_SIZE 20

  int n = 0, n_chunks = 0;
  int *arr = 0;

  do {
    if (n == (n_chunks * CHUNK_SIZE)) {
      ++n_chunks;
      arr = realloc(arr, sizeof arr[0] * n_chunks * CHUNK_SIZE);
       if (!arr) { return 1; } // Memory allocation can fail, so check it
    }

  } while (1 == scanf("%d", &arr[n]) && ++n);

  // Trim any excess
  arr = realloc(arr, sizeof arr[0] * n);
  if (!arr && n > 0) { return 1; }

  // Print the values we read in
  printf("Read %d value%s\n", n, (n == 1) ? "" : "s");
  for (int x = 0; x < n - 1; ++x) {
    printf("%d,", arr[x]);
  }
  if (n > 0) { printf("%d\n", arr[n - 1]); }

  // At the end of the program, free the memory we allocated
  free(arr);
  arr = 0;
  return 0;
}

Comments

0

The easiest way is to simply look for user input with scanf() and then set the result as a variable. For clarity I often set the array size variable as arraysize but then set it as int i = arraysize so that if I do some sort of a conditional loop, it is easier to read. For example (using a for() loop like in your question:

#include <stdio.h>

int main(void)
{
    int arraysize, i;

    printf("Please input an array size.\n");
    scanf("%d", &arraysize); //Will set the user input and use it as array size
    getchar(); 
    i = arraysize; //This is for sheer convenience in the for() loop
    int array[i]; //This creates an array of the size inputted by the user
    printf("Please input %d numbers to fill the array.\n", i); 
    for(i = 0; i<arraysize; i++) //Has the user put in a number for every space in the array
    {
         scanf("%d", &array[i]); //The i coordinate updates with the i++
         getchar();

    }
    printf("Your numbers were: \n");
    for(i = 0; i<arraysize; i++) //Same thing as the previous for() loop
    {                            //except we are printing the numbers in the table
         printf("| %d |", array[i]);

    }
}

The output looks like:

[PROGRAM BEGINS]
Please input an array size.
5
Please input 5 numbers to fill the array.
1 2 33 4 5
Your numbers were:
| 1 || 2 || 33 || 4 || 5 |
[PROGRAM ENDS]

Hope that helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.