Question

In: Computer Science

Can someone convert this code to use printf & scanf instead of cin and cout #include...

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;
}

Solutions

Expert Solution

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:


Related Solutions

For C code: Do not use any function other than getchar, scanf, and printf Q2.A) Write...
For C code: Do not use any function other than getchar, scanf, and printf Q2.A) Write a program to do the following: Read in two values into variables X   and y from the keyboard (they may be decimal values like 2.3). Read in a 3rd value (which is really small < 1.0 ) into variable E. Using a loop find out the value of N in the below expression. i.e. Each iteration of the loop reduced the value of Y...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT   = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE =...
1) As mentioned in this chapter, C++ predefined identifiers such as cout and cin can be...
1) As mentioned in this chapter, C++ predefined identifiers such as cout and cin can be redefined by the programmer. However, why is it not wise to do so? 2) The text mentioned that the char data type can be cast into an int. What are some possible uses of this functionality? 3) Introduce the C++ data type string, which is a programmer-defined data type available in the C++ library. Define a string as a sequence of zero or more...
Objectives Ability to receive input from keyboard Use the printf statement as an alternative to cout....
Objectives Ability to receive input from keyboard Use the printf statement as an alternative to cout. Ability to use For loop Ability to use Do-while loop Program 1:                                                                                                                          Create a program called CountUpBy5. Input: The program should accept a positive integer n from the user as input. If the given input is not a positive input it should prompt the user to retry Hint: use a While-loop for this Output: The program should then count all numbers from 0 up...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j);...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j); // need to validate both being positive while (i != j) { i > j ? (i -= j) : (j -= i); } printf("GCD: %d\n", i); return 0; } The above code returns a GCD from two numbers entered. Use the above program to design a C program to compute of integer coefficients a and b such that gcd(i, j) = a xi...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
Can someone explain how to use z table? How to convert and use it in Stats?
Can someone explain how to use z table? How to convert and use it in Stats?
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); }...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); } void response2 (int sig_no) { printf("43"); }     int main() {      int id = getpid();      signal(SIGUSR1, response); signal(SIGKILL, response2); for(int i=0; i<4; i++) { sleep(1); if (i % 3 == 0) { kill(id, SIGUSR1); } else { kill(id, SIGKILL); } } return 0; }
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT