The first array is multiples of 5, second array is multiple of 9. I was able to store the input but the printed array seems wrong as it did not print my supposed numbers. Any kind soul could help and provide feedback
#include <stdio.h>
int main()
{
//declaring variable for user input (input)
int input = 0;
//prompting user to input size of array
printf("\n\n\tEnter number of values to store: ");
scanf ("%d", &input);
//declaring and initialising work storage
int counter = 0, five = 0, nine = 0;
int store[input];
int multi5[five];
int multi9[nine];
// Input values into store[]
for (counter = 0; counter < input; counter++)
{
if (counter == input - 1)
{
printf("\n\n\t Please enter the last number: ");
}
else
{
printf("\n\n\t Please enter input number %d: ", counter + 1);
}
scanf("%d", &store[counter]);
}
// Separate multiple of 5 and 9 into separate arrays
for (counter = 0; counter < input; counter++)
{
if (store[counter] %5 == 0)
{
multi5[five] = store[counter];
five++;
}
if (store[counter] %9 == 0);
{
multi9[nine] = store[counter];
nine++;
}
}
// Print the multiples of 5
printf("\n\n\tThe multiples of 5 is : ");
for (counter = 0; counter < five; counter++)
{
printf("%d", multi5[five]);
}
// Print the multiples of 9**
printf("\n\n\tThe multiples of 9 are : ");
for (counter = 0; counter < nine; counter++)
{
printf("%d", multi9[nine]);
}
return 0;
}
I am expecting to print the two arrays like this:
The multiple of 5 is: 15 10 5
The multiple of 9 is: 18 27 36
2 Answers 2
In these declarations
//declaring and initialising work storage
int counter = 0, five = 0, nine = 0;
int store[input];
int multi5[five];
int multi9[nine];
there are declared three variable length arrays two of which have sizes that are explicitly set to zeroes. According to the C23 Standard (6.7.6.2 Array declarators):
5 If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *; otherwise, each time it is evaluated it shall have a value greater than zero.
That is you may not declare an array with the size equal to zero.
Before declaring the two arrays you need to count elements in the array store that are divisible by 5 an by 9 and to use obtained values (if they are gretaer than zero) as sizes of the arrays.
And you need to check that the input of the variable input
scanf ("%d", &input);
was successful and its value is also greater than zero.
Also within these loops
// Print the multiples of 5
printf("\n\n\tThe multiples of 5 is : ");
for (counter = 0; counter < five; counter++)
{
printf("%d", multi5[five]);
}
// Print the multiples of 9**
printf("\n\n\tThe multiples of 9 are : ");
for (counter = 0; counter < nine; counter++)
{
printf("%d", multi9[nine]);
}
you need to use the index counter in calls of printf like
printf("%d", multi5[counter]);
Pay attention to that variable length arrays are conditionally supported by compilers. Instead of using variable length arrays you could use dynamically allocated arrays.
1 Comment
The code following is incorrect (at least):
int counter = 0, five = 0, nine = 0;
int store[input];
int multi5[five];
int multi9[nine];
if five is 0 (same for nine) at the time of multi5 array declaration, it will be declared with zero elements, making it unusable at all.... I'm afraid you mistakenly believe that if you change nine later, the variable will be redimensioned to a different value. It's better if you declare the three arrays as:
int counter = 0, five = 0, nine = 0;
int store[input];
int multi5[input];
int multi9[input];
as this will allow all input numbers to be multiples of 5 or multiples of nine, and the extra declared elements to be unused (you don't know how many of them you will have, can be all of them are multiples of 5 or 9 ---or both).
There's also an extra error, as you don't check the result code of scanf("%d", &input); there can be an error because input will not be initialized if the input syntax doesn't correspond to an integer. This will make the input variable to its initialized value (0), and the program to behave incorrectly (trying to initialize an improper array size or trying to read an incorrect number of values from the input). This is wrong, and there should be some value check of what has been input in the first time.
Comments
Explore related questions
See similar questions with these tags.
fiveandnineare both 0. Moreover, these are declaring variable-length arrays. How many elements do you think empty arrays can hold?counterfor the array indexes (after you get the 1st issue sorted out)int store[input];, that array will not change size when the value ininputchanges. The array's size is fixed at the value ofinputwhen the array is declared.