Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C
#include <stdio.h>

int main()
{
    int num;
    float input[3],sum;
    printf("Please enter a limit number :");
    scanf("%d",&num);

    for(int i=1;i<=num;i++){
        for(int j=0;j<=2;j++){
            scanf("%1.1f",&input[j]);
            sum=(input[0]*2.0)+(input[1]*3.0)+(input[2]*5.0);
        }
        printf("%1.1f\n",sum/10.0);
    }
    return 0;
}


What I have tried:

I want to find weighted average but i can't give input on this program when i use scanf("%1.1f",&input[j]);(in bold and underline). Now what to do and why??
Posted
Updated 1-Apr-22 7:20am
v3

The scanf() format specifier "%1.1f" is invalid. For scanf you can specify a field width, but not a precision (i.e. digits after the decimal). Even if you could specify precision, the format "%1.1f" is problematic: it defines a field that is 1 character wide and has 1 digit after the decimal. The smallest input string that one could use would be ".1", which is at least 2 chars wide, so its not clear what scanf would do to satisfy both the field width of 1 char and 1 char after the decimal.
 
Share this answer
 
Quote:
but i can't give input on this program when i use scanf("%1.1f",&input[j]);(in bold and underline). Now what to do and why??

You may have to read documentation about scanf.
Try
C++
scanf("%1.1f",&input[j]);

Another problem
C++
for(int i=1;i<=num;i++){
    for(int j=0;j<=2;j++){
        scanf("%1.1f",&input[j]);
        sum=(input[0]*2.0)+(input[1]*3.0)+(input[2]*5.0); // this line
    }
    sum=(input[0]*2.0)+(input[1]*3.0)+(input[2]*5.0); // goes here
    printf("%1.1f\n",sum/10.0);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900