Description
Problem Descriptions:
The purpose of this assignment is to reinforce the lecture material on variable definition /usage
and DU path. For each of the source code fragments below:
1) Construct a table listing all the line numbers where a variable is defined or used. You must list all
the variables in each source code fragment.
2) Construct a DU Path table showing all paths from any definition to usage of every variable.
A sample example is given below:
1. int main() {
2. char operator;
3. double 1stNum, 2ndNum;
4. printf(“Enter an operator (+, -, *,): “);
5. scanf(“%c”, & operator);
6. printf(“Enter two operands: “);
7. scanf(“%lf %lf”, & 1stNum, & 2ndNum);
8. if (operator == ‘+’) {
9. printf(“%.1lf + %.1lf = %.1lf”, 1stNum, 2ndNum, 1stNum + 2ndNum);
10. } else if (operator == ‘-‘) {
11. printf(“%.1lf – %.1lf = %.1lf”, 1stNum, 2ndNum, 1stNum – 2ndNum);
12. } else if (operator == ‘*’) {
13. printf(“%.1lf * %.1lf = %.1lf”, 1stNum, 2ndNum, 1stNum * 2ndNum);
14. } else if (operator == ‘/’) {
15. printf(“%.1lf / %.1lf = %.1lf”, 1stNum, 2ndNum, 1stNum / 2ndNum);
16. } else {
17. printf(“Error! operator is not correct”);
18. }
19. return 0;
20. }
DEF –USE Table:
Variable DEF USE
operator 2, 5 8, 10, 12, 14
1stNum 3, 7 9, 11, 13, 15
2ndNum 3, 7 9, 11, 13, 15
DU Path Table:
Variable # DU Path
operator 1 5-6-7-8
2 5-6-7-8-10
3 5-6-7-8-10-12
4 5-6-7-8-10-12-14
1stNum 1 7-8-9
2 7-8-10-11
3 7-8-10-12-13
4 7-8-10-12-14-15
2ndNum 1 7-8-9
2 7-8-10-11
3 7-8-10-12-13
4 7-8-10-12-14-15
Problem 1
1 #include
2 void main()
3 {
4 float testWeight=0.5;
5 float testGrade, hwGrade;
6 printf(“Input the values for test grade and homework grade : “);
7 scanf(“%f %f”,&testGrade,&hwGrade);
8 float finalGrade = testGrade * testWeight + hwGrade * (1-testWeight);
9 if( finalGrade >= 90 ){
10 printf(“test grade %f homework grade %f result an A.\n”, testGrade, hwGrade);}
11 else if( finalGrade >= 80 && finalGrade < 90){
12 printf("test grade %f homework grade %f result an B.\n", testGrade, hwGrade);}
13 else if( finalGrade >= 70 && finalGrade < 80){
14 printf("test grade %f homework grade %f result an C.\n", testGrade, hwGrade);}
15 else if( finalGrade > =60 && finalGrade < 70){
16 printf("test grade %f homework grade %f result an D.\n", testGrade, hwGrade);}
17 else{
18 printf("test grade %f homework grade %f result an F.\n", testGrade, hwGrade);}
19 }
Problem 2
1 int main() {
2 double running;
3 double runCa;
4 double swimming;
5 double swimCa;
6 double goal;
7 double totalCa;
8 cout << "How long did you run? (in minutes) ";
9 cin >> running;
10 cout<<”How many the calories from running 1 minute?”
11 cin>>runCa;
12 cout << "How long did you swim? (in minutes) ";
13 cin >> swimming;
14 cout << " How many calories from swimming 1 minute? ";
15 cin >> swimCa;
16 cout << “What’s your goal for today? (in calories)”;
17 cin >> goal;
18 totalCa = swimming * swimCa + running * runCa;
19 if (totalCa >= goal){
20 cout << "Take a break for the rest of your day."}
21 else {
22 goal = goal – totalCa;
23 cout<<”Your left work towards today’s goal is:”;
24 cout<