Question

In: Computer Science

The Trabb Pardo-Knuth algorithm is often used to illustrate the basic syntax of programming languages. ask...

The Trabb Pardo-Knuth algorithm is often used to illustrate the basic syntax of programming languages.

ask for 11 numbers to be read into a sequence S
for each item e in the reversed sequence of S
    compute the value sqrt(abs(e))+5*e
    if result is greater than 500
        alert user
    else
        print result

Implement the algorithm in at least two programming languages.

Solutions

Expert Solution

Java:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int S[] = new int[11];

System.out.println("Enter 11 numbers: ");

for (int i=0; i<11; i++) {

S[i] = sc.nextInt();

}

for (int i=10; i>=0; i--) {

double result = Math.sqrt(Math.abs(S[i])) + 5*S[i];

if (result > 500) {

System.out.println(S[i] + " term is not eligible");

} else {

System.out.println(S[i] + " -> " + result);

}

}

}

}

C++:

#include <iostream>

#include <cmath>

using namespace std;

int main() {

int S[11];

    cout << "Enter 11 numbers: ";

    for (int i=0; i<11; i++) {

      cin >> S[i];

    }

    for (int i=10; i>=0; i--) {

      double result = sqrt(abs(S[i])) + 5*S[i];

      if (result > 500) {

        cout << S[i] << " term is not eligible" << endl;

      } else {

        cout << S[i] << " -> " << result << endl;

      }

    }

  }


Related Solutions

Programming languages Explain orthogonality and its importance.
Programming languages Explain orthogonality and its importance.
The syntax of the monkey languages is quite simple, yet only monkeys can speak it without...
The syntax of the monkey languages is quite simple, yet only monkeys can speak it without making mistakes. The alphabet of the language is {a, b, d, #}, where # stands or a space. The grammar is <stop> ::= b|d <plosive> ::= <stop>a <syllable> ::= <plosive>|<plosive><stop>|a<plosive>|a<stop> <word> ::= <syllable>|<syllable><word><syllable> <sentence> ::= <word>|<sentence>#<word> Using parse trees, which of the following speakers is the secret agent masquerading as a monkey? Chimp: abdabaadab#ada Baboon: dad#ad#abaadad#badadbaad
Write an essay about the primary use of the Programming Languages
Write an essay about the primary use of the Programming Languages
It is standard in some programming languages for the number zero to represent one of the...
It is standard in some programming languages for the number zero to represent one of the truth values (either true or false) and for positive numbers to represent the other truth value. If assigned correctly, the operations of addition and multiplication will then correspond to conjunction and disjunction, in some order. Figure out how to make this all work out neatly.
Using SAS programming Please include the syntax and output of the information: In a study of...
Using SAS programming Please include the syntax and output of the information: In a study of factors thought to be responsible for the adverse effects of smoking on human reproduction, cadmium level determinations (nanograms per gram) were made on placenta tissue of a sample of 14 mothers who were smokers and an independent random sample of 18 nonsmoking mothers. The results were as follows- Nonsmokers: 10.0, 8.4, 12.8, 25.0, 11.8, 9.8, 12.5, 15.4, 23.5, 9.4, 25.1, 19.5, 25.5, 9.8, 7.5,...
Using SAS programming Please include the syntax and output of the information: In a study of...
Using SAS programming Please include the syntax and output of the information: In a study of factors thought to be responsible for the adverse effects of smoking on human reproduction, cadmium level determinations (nanograms per gram) were made on placenta tissue of a sample of 14 mothers who were smokers and an independent random sample of 18 nonsmoking mothers. The results were as follows- Nonsmokers: 10.0, 8.4, 12.8, 25.0, 11.8, 9.8, 12.5, 15.4, 23.5, 9.4, 25.1, 19.5, 25.5, 9.8, 7.5,...
The number of syntax errors in a programming assignment for 20 randomly selected students are as...
The number of syntax errors in a programming assignment for 20 randomly selected students are as follows: 21, 5, 44, 44, 22, 49, 2, 48, 17, 27, 21, 34, 24, 12, 43, 12, 30, 10, 15, 15. a. Construct an ordered stem-and-leaf display for this data. b. Find the sample variance, S2 , and the sample standard deviation, S, using the short-cut method. c. Find the sample lower quartile Q1 , median Q2 and Upper-Quartile Q3 . d. Construct a...
Design and construct a computer program in one of the approved languages (C++) that will illustrate...
Design and construct a computer program in one of the approved languages (C++) that will illustrate the use of a fourth-order explicit Runge-Kutta method of your own design. In other words, you will first have to solve the Runge-Kutta equations of condition for the coefficients of a fourth-order Runge-Kutta method. See the Mathematica notebook on solving the equations for 4th order RK method. That notebook can be found at rk4Solution.nb . PLEASE DO NOT USE a[1] = 1/2 or a[2]...
Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages...
Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages which support the user of the programming language deciding which to use when creating their method. (Programming Languages)
Bromophenol Blue is often used as an acid-base indicator. In neutral and basic conditions it is...
Bromophenol Blue is often used as an acid-base indicator. In neutral and basic conditions it is blue while in acidic conditions it is yellow. The yellow form of bromophenol blue absorbs at 440nm with a molar absorptivity of 5.85x10^4 (assume that the blue form does not absorb at this wavelength). The pKa of bromophenol blue is 4. you would like to make 3ml solution of bromophenol blue that has an absorbance at440= 0.560. Select the buffer that you would use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT