In: Computer Science
Can someone convert this code to use printf & scanf instead of cin and cout
#include <iostream> int main() { int n, i = 0; float RF, PFD, Tn = 0, Ts; std::cout << "**** Welcome to the Time Study Program ****" << std::endl; std::cout << "\nEnter the number of elements for the given operation being performed" << std::endl; std::cout << "Number of Elements: "; std::cin >> n; if (n < 0) { std::cout << "\nNumber of elements cannot be negative! Try again."; return 0; } std::cout << "\nEnter the Performance Rating (PR) factor as a percentage" << std::endl; std::cout << "RF: "; std::cin >> RF; if (RF < 0) { std::cout << "\nPercentage cannot be negative! Try again."; return 0; } RF = RF / 100.0; std::cout << "\nEnter the Personal Fatigue Delay (PFD) value as a percentage" << std::endl; std::cout << "PFD: "; std::cin >> PFD; if (PFD < 0) { std::cout << "\nPercentage cannot be negative! Try again."; return 0; } PFD = PFD / 100.0; std::cout << "\nNow enter the measured cycle times in hundredths of a minute for each element" << std::endl; std::cout << "(Note: Time entered should be relative not absolute)\n"; float ET[n], temp[n]; while (i < n) { std::cout << "\n Element " << i + 1 << ": "; std::cin >> ET[i]; if (ET[i] < 0) { std::cout << "\nTime cannot be negative! Try Again."; return 0; } i++; } /* temp[] array stores the one hundredths of minutes of corresponding elements */ i = 0; while (i < n) { if (i == 0) temp[i] = ET[i] / 100; else temp[i] = (ET[i] - ET[i - 1]) / 100; i++; } i = 0; while (i < n) { Tn = Tn + temp[i]; i++; } Tn = Tn * RF; Ts = Tn * (1 + PFD); std::cout << "\n**** Time Study Results ****"; std::cout << "\nThe Time Standard is: Ts = " << Ts << " minutes"; return 0; }
Program rewritten with printf:
#include <iostream>
int main()
{
int n, i = 0;
float RF, PFD, Tn = 0, Ts;
printf("**** Welcome to the Time Study Program ****\n");
printf("Enter the number of elements for the given operation being performed\n");
printf("Number of Elements: ");
scanf("%d",&n);
if (n < 0)
{
printf("\nNumber of elements cannot be negative! Try again.\n");
return 0;
}
printf("Enter the Performance Rating (PR) factor as a percentage\n");
printf("RF: ");
scanf("%f",&RF);
if (RF < 0)
{
printf("\nPercentage cannot be negative! Try again.");
return 0;
}
RF = RF / 100.0;
printf("\nEnter the Personal Fatigue Delay (PFD) value as a percentage\n");
printf("PFD: ");
scanf("%f",&PFD);
if (PFD < 0)
{
printf("\nPercentage cannot be negative! Try again.");
return 0;
}
PFD = PFD / 100.0;
printf("\nNow enter the measured cycle times in hundredths of a minute for each element\n");
printf("(Note: Time entered should be relative not absolute)\n");
float ET[n], temp[n];
while (i < n)
{
printf("\n Element %d: ",i + 1);
scanf("%f",&ET[i]);
if (ET[i] < 0)
{
printf("\nTime cannot be negative! Try Again.");
return 0;
}
i++;
}
/*
temp[] array stores the one hundredths of minutes of corresponding elements
*/
i = 0;
while (i < n)
{
if (i == 0)
temp[i] = ET[i] / 100;
else
temp[i] = (ET[i] - ET[i - 1]) / 100;
i++;
}
i = 0;
while (i < n)
{
Tn = Tn + temp[i];
i++;
}
Tn = Tn * RF;
Ts = Tn * (1 + PFD);
printf("\n**** Time Study Results ****");
printf("\nThe Time Standard is: Ts = %f minutes",Ts);
return 0;
}
Output for your reference: