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