Home > OS >  Array-manipulating functions in C
Array-manipulating functions in C

Time:01-10

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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;
}
  •  Tags:  
  • Related