Amy
Amy

Reputation: 31

How to repeat scanf() n times?

I ask the user to input two integers, n and x. After that, I need to ask them for a value for the a variable n times. I need to create a new a variable for each value. How do I do it? I have absolutely no idea.

Also, I need to do it in one line, so the input will be, for example, 50 30 21, not

50
30
21

Thanks.

#include <stdio.h>

int main (void) {
  int a, n, x;
  int i = 0;

  scanf ("%d%d", &n, &x);

  scanf ("%d", &a); /* what should I do here? */
}

Upvotes: 3

Views: 3372

Answers (5)

Mihir Khandekar
Mihir Khandekar

Reputation: 108

When you do scanf, it takes the input until the space character, which is a delimiter. You do not need to specify anything extra for that. If you do not need to use the value of n again, you could use this code.

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

int main (void) {
  int n, x, i;
  scanf ("%d%d", &n, &x);
  int *a = (int *) malloc (n * sizeof(int));    //This will allocate 'n' integer sized memory for 'a'
  for(i = 0; i < n; i++) {
        scanf ("%d", &a[i]);
  }
}

Upvotes: 0

abhiarora
abhiarora

Reputation: 10430

After that, I need to ask them for a value for the a variable n times.

High Level Programming Languages and even Assembly Language (Conditional/Unconditional Jump) provide Loop constructs to a programmer. In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached. In C/C++, we have for, while, do while loop constructs.

So, i order to ask a user for a value for n times, you can use for loop in your program. I have given an example below:

#include <stdio.h>
#define MAX_ARR_LEN    100
int main (void) 
{
    int a[MAX_ARR_LEN];
    int n, x;
    int i = 0;

    scanf ("%d%d", &n, &x);

    if (n > MAX_ARR_LEN) {
        printf("You can't enter more than - %d\n", MAX_ARR_LEN);
        return -1;
    }

    for(i = 0; i < n; i++) {
        scanf ("%d", &a[i]);
    }
    return 0;
}

Upvotes: 0

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

Using dynamic memory:

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

int main (void) {
  int *a = NULL;
  int n, x, i;

  scanf("%d%d", &n, &x);

  if (n <= 0) {
    fprintf(stderr, "n must be > 0\n");
    return 1;
  }

  a = malloc(n * sizeof(int));
  if (a == NULL) {
    fprintf(stderr, "failed to allocate memory for "
        "%d integers\n", n);
    return 1;
  }

  /* reading the user input */
  for (i = 0; i < n; i++) {
    scanf ("%d", &a[i]);
  }

  /* usage */
  for (i = 0; i < n; i++) {
    printf("a[%d] = %d\n", i, a[i]);
  }
  printf("x = %d\n", x);

  free(a);

  return 0;
}

Upvotes: 2

vivek
vivek

Reputation: 1605

Try the below code:

#include <stdio.h>

    int main (void) {
    int a[100];
    int n, x;
    int i = 0;

    scanf ("%d%d", &n, &x);//n cannot be greater than 100 in this case.

    for(i = 0; i < n; i++)
    {
        scanf ("%d", &a[i]);
    }
    return 0;
}

Upvotes: 2

msc
msc

Reputation: 34598

Try this:

int arr[100]; // static allocation but you can also allocate the dynamically memory

printf("Enter the number for how many time repeat scanf()\n"); 
scanf("%d",&n);

for (int i = 0; i < n; i++)
{
    scanf("%d",&arr[i]);
}

Upvotes: 2

Related Questions