1

I am fairly new to programming in C and decided to try out some questions. Whilst I was trying them I encountered a question and was unsure if I was doing the right thing.

The question asked to create a function that initializes an array parameter with a list of integer values provided by the user as suggested. This I think I managed to do.

Also the size needs to be determined by the user input and the largest possible accepted list size is set to 200. When it comes to get the user to enter the size, I managed, but when it comes to the possible largest size it isn't working.

Here you can find my code:

#include <stdio.h>

void init_array(int ar[], int size) {

    int i;
    printf("Please enter %d values: \n");
    for (i = 0; i < size; i++) {
        scanf("%d", &ar[i]);

    }
    printf("Elements in array are:");
    for (i = 0; i < size; i++) {
        printf("%d,  ", ar[i]);
    }
}

int main() {

    int size;
    printf("The size of list:");
    scanf("%d", &size);
    int marks[] = {};
    init_array(marks, size);
    if(size >=200){
        printf(" Limit exceeded");
    }

    return 0;
}

Thanks for any help

ArunPratap
  • 4,816
  • 7
  • 25
  • 43
  • 1
    You're a beginner... so turn on all your compiler warnings and mind them... and leave them on forever :-) – pmg Jan 09 '22 at 12:17
  • 1
    `it isn't working` what does it mean? – Chetan Jan 09 '22 at 12:17
  • 1
    (a) There is no question in your post. You should ask a specific question. (b) Never describe a problem merely as “it isn't working”; that is not useful. Always state how to reproduce the problem, what the observed behavior of the computer is, and what the behavior desired instead is. (c) `int marks[] = {};` is not proper C code. You must give the array a size either by specifying a size inside the `[]` or by giving a list of initializers. You may want `int marks[size];`. The test `if (size >= 200)` should also be before that, not after. – Eric Postpischil Jan 09 '22 at 12:18

4 Answers4

1

There are a couple of major errors in your code and a few aspects that will generate warnings (see: Why should I always enable compiler warnings?) and should be improved.

First, you have omitted the size argument (corresponding to the given %d format specifier) in your first call to printf in the init_array function.

Second, your declaration of marks is wrong – as it stands, it is declaring an array of zero length (or, rather, attempting to do so). So, assuming you have a C compiler that supports variable length arrays (VLAs – most do but some, notably MSVC, do not), you should declare it as int marks[size]; after you have read the value of size (and, preferably, after you have verified that the given value for size is acceptable). Note also that you cannot initialize VLAs with an initializer list ( = {0, 1, } syntax). If you don't want to use a VLA, then you can specify a fixed (most likely 200) maximum size for the array.

Some issues that will generate warnings and/or could be improved:

In C, it is generally better practice (IMHO) to add an explicit void as the formal argument list for functions that do not take arguments. Related reading: What are the valid signatures for C's main() function? Although, in a function definition (as opposed to a prototype), the empty () is valid code.

You really should get into the habit of checking the value returned by a call to scanf (and related functions), to ensure that valid input was given. In the code below, I have added such a check for the call in the main function, but you should also add a similar check in your init_array function.

When a value of over 200 is given for size, your program should, presumably, not run the init_array function; so, you should check that value before initializing the marks array.

Here's a version of your code with the changes I have outlined above made:

#include <stdio.h>

void init_array(int ar[], int size)
{
    int i;
    printf("Please enter %d values: \n", size); // You need to give "size" as an argument
    for (i = 0; i < size; i++) {
        scanf("%d", &ar[i]); // You should add a check for valid input here, as I have done in main

    }
    printf("Elements in array are:");
    for (i = 0; i < size; i++) {
        printf("%d,  ", ar[i]);
    }
}

int main(void) // Better with the explicit "void" argument list specifier
{
    int size;
    printf("The size of list:");
    if (scanf("%d", &size) != 1 || size < 1) { // You should ALWAYS check the return value of a scanf call
        printf("Invalid size input.\n");
        return 1;
    }
    int marks[size]; // Need to specify the size and note that you cannot initialize a VLA
//  int marks[200] = { 0, }; // Alternative if not using VLA.
    if (size >= 200) {
        printf(" Limit exceeded.\n");
        return 2;
    }
    init_array(marks, size);
    return 0;
}

Please feel free to ask for any further clarifications and/or explanations.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • `int main() { … }` in a function definition does fully define the function; it has no parameters. `int main()` in a declaration that is not a definition leaves the parameters unspecified. – Eric Postpischil Jan 09 '22 at 14:07
  • @EricPostpischil Yes, you're right (edited my answer accordingly). I guess I've been using MSVC for too long, which gives: *warning C4255: 'main': no function prototype given: converting '()' to '(void)'*. – Adrian Mole Jan 09 '22 at 16:08
0

You are creating a zero length array, you should change your main funtion to something like below.

int main() {
    int size;
    int marks[200];
    printf("The size of list:");
    scanf("%d", &size);
    if(size < 200){
        init_array(marks, size);
    } else {
        printf(" Limit exceeded");
    }
    return 0;
}
Larcis
  • 134
  • 7
0

In the first printf statement you need to add size as the second parameter. You also need to check the limit before declaring the array and you need to specify the size of the array in function main (arrays in C does not grow automatically as needed). Finally, you should also validate that the input are integers by checking the return value from scanf. Here is a modified version:

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

void read_int(int *i) {
    int n = scanf("%d", i);
    if (n != 1) {
        fprintf(stderr, "Integer expected\n");
        exit(EXIT_FAILURE);
    }
}

void init_array(int ar[], int size) {
    int i;
    printf("Please enter %d values:\n", size);
    for (i = 0; i < size; i++) {
        read_int(&ar[i]);

    }
    printf("Elements in array are: ");
    for (i = 0; i < size; i++) {
        if (i > 0) {
            printf(", ");
        }
        printf("%d", ar[i]);
    }
    putchar('\n');
}

int main(void) {
    const int max_size = 200;
    int size;
    printf("The size of list: ");
    read_int(&size);
    if ((size >= 1) && (size <= max_size)) {
        int marks[size];
        init_array(marks, size);
    } else {
        fprintf(stderr, "Array size must be between 1 and %d\n", max_size);
        exit(EXIT_FAILURE);
    }
    return 0;
}
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
0

First you need to check the size for a limitation, and then create an array of the desired size or stop executing the program.

int main() {

    int size;
    printf("The size of list:");
    scanf("%d", &size);
    int marks[size];
    if(size >=200){
        printf(" Limit exceeded");
        return 1;
    }
    init_array(marks, size);
    return 0;
}
dabdya
  • 78
  • 6
  • This answer says the check should be first, then the array can be created, but the code it shows attempts to create the array with `int marks[size];` and then checks the size. – Eric Postpischil Jan 09 '22 at 14:09