In: Computer Science
My program is working until it gets to val2 / 1000000. I keep getting my answer to be 0. Can someone help please?
int main()
{
int n, num, num1, num2, time, val1, val2, Ts;
printf("**** Welcome to the Time Study Program
****\n");
printf("\n");
printf("Enter the number of elements for the give
operation being perform\n");
printf("Number of elements: ");
scanf_s("%d", &n);
printf("\n");
printf("Enter the Performance Rating (PR) factor as
a percentage\n");
printf("RF: ");
scanf_s("%d", &num1);
printf("\n");
printf("Enter the Personal Fatigue Delay (PFD)
value as a perventage\n");
printf("PFD: ");
scanf_s("%d", &num2);
printf("\n");
printf("Now enter the measured cycle times in
hundredths of a minute for each element\n");
printf("<Note: Time entered should be relative not
absolute");
printf("\n");
//Allowing the user to enter a time for each
element
for (num = 1; num <= n; num++)
{
printf("Element\t%d: ",
num);
scanf_s("%d", &time);
printf("\n");
}
if (time >= 0)
{
val1 = ((time) * (num1)); //solving
for normal time
val2 = ((val1) * (100 + num2));
//solving for standard time
printf("The answer is: %d",
val2);
Ts = (val2) / (1000000);
//converting answer to hundreths of a minute
printf("The answer is: %d",
Ts);
printf("**** Time Study Results
****\n");
printf("The Time Standard is: TS =
%d minutes", Ts);
}
return 0;
}
When you divide to integers you answer will be displayed with out decimals numbers to get the decimal values we have to use the variables as float or double so that we can get the decimal values when we devide
%f is used to print the floating point numbers on the screen
Code:
#include<stdio.h>
int main()
{
int n, num, num1, num2, time, val1;
float Ts,val2;
/*Just place Ts and val2 as floating point numbers*/
printf("**** Welcome to the Time Study Program ****\n");
printf("\n");
printf("Enter the number of elements for the give operation being
perform\n");
printf("Number of elements: ");
scanf_s("%d", &n);
printf("\n");
printf("Enter the Performance Rating (PR) factor as a
percentage\n");
printf("RF: ");
scanf_s("%d", &num1);
printf("\n");
printf("Enter the Personal Fatigue Delay (PFD) value as a
perventage\n");
printf("PFD: ");
scanf_s("%d", &num2);
printf("\n");
printf("Now enter the measured cycle times in hundredths of a
minute for each element\n");
printf("<Note: Time entered should be relative not
absolute");
printf("\n");
//Allowing the user to enter a time for each element
for (num = 1; num <= n; num++)
{
printf("Element\t%d: ", num);
scanf_s("%d", &time);
printf("\n");
}
if (time >= 0)
{
val1 = ((time) * (num1)); //solving for normal time
val2 = ((val1) * (100 + num2)); //solving for standard time
printf("The answer is: %f\n", val2);
/*insted of %d place %f as it is a floating point
number */
Ts = (val2) / (1000000); //converting answer to hundreths of a
minute
printf("The answer is: %f\n", Ts);
/*insted of %d place %f as it is a floating point number */
printf("**** Time Study Results ****\n");
printf("The Time Standard is: TS = %f minutes", Ts);
/*insted of %d place %f as it is a floating point
number */
}
return 0;
}
Output:
Indentation: