0

Problem: while running this code, I am taking 10 inputs but this program storing 9 elements ( list[8] ) initially. Then pressing a random number and ENTER, this program storing the last index( list[10] ) and finally giving me the expected output. But if I remove a space from the line of code(after %f) : scanf("%f ",&list[i]); then this code runs perfectly. As a beginner, I badly want to know why/how does a single space after %f make this code different? Please run this code by yourself, I think you will be more clear by seeing the output.

/******************************************************************************

 In this program, I will read 10 values(Positive,negative and float) and then
 print only Positive Integer(s)

 Test Value/Input: 
 1 2.3 4.5 -10 6.1 7 9.0 -4 -11 44

*******************************************************************************/

#include <stdio.h>
#include<math.h>

int main()
{
    float y=1,list[10];
    
    for(int i=0;i<10;i++)
    {
       scanf("%f ",&list[i]);   //here is the PROBLEM!
       printf("list[%d]: %.2f\n",i,list[i]);
    }

    printf("Positive integers:\n");
    
    for(int i=0;i<10;i++)
    {
        if(list[i]>0 && remainder(list[i],y)==0 )  
        // if,  X MOD 1 == 0 then X is an Integer
        {
            printf("%.0f ",list[i]);
        }
    }       
    return 0;
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39

0 Answers0