-2

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

Vlad from Moscow
313k27 gold badges204 silver badges358 bronze badges
asked Jun 24, 2024 at 12:26
7
  • 4
    You may not declare an array with zero number of elements like that int multi5[five]; int multi9[nine]; Commented Jun 24, 2024 at 12:29
  • 1
    five and nine are both 0. Moreover, these are declaring variable-length arrays. How many elements do you think empty arrays can hold? Commented Jun 24, 2024 at 12:29
  • 1
    In the final loops you should use counter for the array indexes (after you get the 1st issue sorted out) Commented Jun 24, 2024 at 12:31
  • Please edit and show an complete example of input and expected output. Commented Jun 24, 2024 at 12:32
  • 2
    When you declare an array with code like int store[input];, that array will not change size when the value in input changes. The array's size is fixed at the value of input when the array is declared. Commented Jun 24, 2024 at 12:32

2 Answers 2

0

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.

Luis Colorado
13.1k1 gold badge19 silver badges34 bronze badges
answered Jun 24, 2024 at 12:46
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, i edited the wrong answer :)
-1

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.

answered Jun 28, 2024 at 11:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.