#include <stdio.h>
int main(){
int num, counter, numberOfInputs, even, odd, sumEven, sumOdd;
printf("Enter number of inputs:\n ");
scanf("%d", &numberOfInputs);
printf("Enter %d numbers: ", numberOfInputs);
for(counter=1; numberOfInputs>counter; counter ){
scanf("%d", counter);
return 0;
}
I have a project that will let the user enter a number of inputs. I can't seem to solve it. I finished the other parts of the program that will determine the relation on the number of odd and even numbers, I just didn't include it. Please help and tnx
Follow-up question:
Why is that the last number entered always adds by 1 when I put the printf to show the numbers entered?
#include <stdio.h>
int main(){
int num, counter, numberOfInputs, even, odd, sumEven, sumOdd;
printf("Enter number of inputs:");
scanf("%d", &numberOfInputs);
printf("Enter %d numbers:\n", numberOfInputs);
for(counter=0; numberOfInputs>=counter;){
scanf("%d", &counter);
counter ;
}
/*when I put a printf here, the last number is added by 1, it works fine without the printf though*/
CodePudding user response:
Couple of things wrong with the code you posted.
First scanf("%d", counter)
is wrong. The %d
format specifier expects a pointer to int (int*
) while you provided an int
. The logic is also wrong, since you are overriding counter
.
Also, your loop will execute one time less than numberOfInputs
, since you're counting from 1
, it should be numberOfInputs>=counter
.
Lastly, you should check the return value of scanf
to see if it succeeded (try inputting something other than an integer).
for(counter=1; numberOfInputs>=counter; counter ){
if (scanf("%d", &num) != 1)
/* handle error */
}
It would also be a good idea to enable compiler warnings, if you had so it would have detected the scanf
type conflict. A switch such as -Wall
will be a good start.
CodePudding user response:
Answer to original question:
The line
scanf("%d", counter);
is wrong, for several reasons:
The %d
scanf
conversion format specifier requires the address of an int
to write to (i.e. a pointer). However, you are not passing it such an address. Instead, you are passing it the value of the loop counter, which does not make sense.
If you instead change the line to
scanf( "%d", &counter );
then the requirements of the %d
conversion format specifier would be fulfilled. But it still does not make sense, because this would cause the loop counter to be overwritten with the user input.
Another issue is that the line
for(counter=1; numberOfInputs>counter; counter ){
is wrong, as this loop will only do numberOfInputs - 1
iterations, although you probably want to do numberOfInputs
iterations.
Also, it is generally always a good idea to check the return value of scanf
, and provide an error message if it wasn't able to match the input.
In your question, you did not specify whether you want to
- remember all inputs that the user entered, or
- discard all inputs after processing them.
However, based on the variable names sumEven
, sumOdd
, even
and odd
in your code, I assume that you want you only want to calculate the sum of all even and odd numbers, and the number of even and odd numbers. In that case, you don't have to remember all inputs; they can be discarded after processing them. In that case, your code should look something like this:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int numberOfInputs, numEven = 0, numOdd = 0, sumEven = 0, sumOdd = 0;
printf( "Enter number of inputs: " );
if ( scanf( "%d", &numberOfInputs ) != 1 )
{
printf( "input failure!" );
exit( EXIT_FAILURE );
}
printf( "Enter %d numbers: ", numberOfInputs );
for( int counter = 0; counter < numberOfInputs; counter )
{
int num;
if ( scanf( "%d", &num ) != 1 )
{
printf( "input failure!" );
exit( EXIT_FAILURE );
}
//process input
//determine whether number is odd or even
if ( num % 2 == 0 )
{
numEven ;
sumEven = num;
}
else
{
numOdd ;
sumOdd = num;
}
}
printf( "\n" );
printf( "Number of even numbers: %d\n", numEven );
printf( "Number of odd numbers: %d\n", numOdd );
printf( "\n" );
printf( "Sum of even numbers: %d\n", sumEven );
printf( "Sum of odd numbers: %d\n", sumOdd );
return 0;
}
This program has the following output:
Enter number of inputs: 5
Enter 5 numbers: 3 5 6 7 8
Number of even numbers: 2
Number of odd numbers: 3
Sum of even numbers: 14
Sum of odd numbers: 15
Answer to follow-up question:
The following loop will do numberOfInputs 1
iterations, although you probably only want it to do numberOfInput
iterations.
for(counter=0; numberOfInputs>=counter;){
scanf("%d", &counter);
counter ;
}
To fix this, you could change it to the following:
for ( counter = 0; counter < numberOfInputs; counter ) {
scanf("%d", &counter);
}
However, as already stated in my answer to the original question, it does not make sense to overwrite the loop counter with the user input.