answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Digiron [165]
2 years ago
7

Use the loop invariant (I) to show that the code below correctly computes the product of all elements in an array A of n integer

s for any n ≥ 1. First use induction to show that (I) is indeed a loop invariant, and then draw conclusions for the termination of the while loop.
Algorithm 1 computeProduct(int[ ] A, int n)
p = a[0]
i = 0
while i < n − 1 do
//(I) p = a[0] · a[1] · · · a[i] (Loop Invariant)
i + +
p = p · a[i]
end while
return p
Computers and Technology
1 answer:
NeTakaya2 years ago
6 0

Answer:

Given Loop Variant P = a[0], a[1] ... a[i]

It is product of n terms in array

Explanation:

The Basic Step: i = 0, loop invariant p=a[0], it is true because of 'p' initialized as a[0].

Induction Step: Assume that for i = n - 3, loop invariant p is product of a[0], a[1], a[2] .... a[n - 3].

So, after that multiply a[n - 2] with p, i.e P = a[0], a[1], a[2] .... a[n - 3].a[n - 2].

After execution of while loop, loop variant p becomes: P = a[0], a[1], a[2] .... a[n -3].a[n -2].

for the case i = n-2, invariant p is product of a[0], a[1], a[2] .... a[n-3].a[n-2]. It is the product of (n-1) terms. In while loop, incrementing the value of i, so i=n-1

And multiply a[n-1] with p, i.e P = a[0].a[1].a[2].... a[n-2].

a[n-1]. i.e. P=P.a[n-1]

By the assumption for i=n-3 loop invariant is true, therefore for i=n-2 also it is true.

By induction method proved that for all n > = 1 Code will return product of n array elements.

While loop check the condition i < n - 1. therefore the conditional statement is n - i > 1

If i = n , n - i = 0 , it will violate condition of while loop, so, the while loop will terminate at i = n at this time loop invariant P = a[0].a[1].a[2]....a[n-2].a[n-1]

You might be interested in
1. Do you consider Facebook, MySpace, and LinkedIn forms of disruptive or sustaining technology? Why?
Mrrafil [7]
It mainly just depends on if you "misuse" them.
8 0
1 year ago
Eric is working on a computer that has a device driver error. Eric can find the name of the device driver however the actual dev
guajiro [1.7K]

Answer:

Windows\System32\drivers folder

Explanation:

6 0
1 year ago
Read 2 more answers
JAVA Write a program that first asks the user to type a letter grade and then translates the letter grade into a number grade. L
ale4655 [162]

Answer:

Follows are the code to this question:

import java.util.*;//import package for user input

class GradePrinter//defining class GradePrinter

{

double numericValue = 0;//defining double variable

String grade = "";//defining String variable

GradePrinter()//defining default constructor  

{

Scanner xb = new Scanner(System. in );//defining Scanner  class object

System.out.print("Enter Grade: ");//print message

grade = xb.nextLine();//input string value  

}

double getNumericGrade()//defining double method getNumericGrade

{

if (grade.equals("A+") || grade.equals("A"))//defining if block that check input is A+ or A

{

numericValue = 4.0;//using  numericValue variable that hold float value 4.0

}

else if (grade.equals("A-"))//defining else if that check grade equals to A-

{

numericValue = 3.7;//using  numericValue variable that hold float value 3.7

}

else if (grade.equals("B+"))//defining else if that check grade equals to B-

{

numericValue = 3.3;//using  numericValue variable that hold float value 3.3

}

else if (grade.equals("B"))//defining else if that check grade equals to B

{

numericValue = 3.0;//using  numericValue variable that hold float value 3.0

}

else if (grade.equals("B-"))//defining else if that check grade equals to B-  

{

numericValue = 2.7;//using  numericValue variable that hold float value 2.7

}

else if (grade.equals("C+"))//defining else if that check grade equals to C+  

{

numericValue = 2.3; //using  numericValue variable that hold float value 2.3

}

else if (grade.equals("C")) //defining else if that check grade equals to C  

{

numericValue = 2.0; //using numericValue variable that hold float value 2.0

}

else if (grade.equals("C-")) //defining else if that check grade equals to C-  

{

numericValue = 1.7;//using umericValue variable that hold float value 1.7

}

else if (grade.equals("D+"))//defining else if that check grade equals to D+  

{

numericValue = 1.3;//using umericValue variable that hold float value 1.3

}

else if (grade.equals("D"))//defining else if that check grade equals to D

{

numericValue = 1.0;//using umericValue variable that hold float value 1.0

}

else if (grade.equals("F"))//defining else if that check grade equals to F

{

numericValue = 0;//using umericValue variable that hold value 0

}

else//defining else block

{

System.out.println("Letter not in grading system");//print message

}

return numericValue;//return numericValue

}

}

class Main//defining a class main

{

public static void main(String[] args)//defining main method

{

GradePrinter ob = new GradePrinter();// creating class GradePrinter object

double numericGrade = ob.getNumericGrade();//defining double variable numericGrade that holds method Value

System.out.println("Numeric Value: "+numericGrade); //print Value numericgrade.

}

}

Output:

Enter Grade: B

Numeric Value: 3.0

Explanation:

In the above-given code, a class "GradePrinter" is defined inside the class a string, and double variable "grade and numericValue" is defined, in which the grade variable is used for input string value from the user end.

After input, the sting value a method getNumericGrade is defined, which uses multiple conditional statements is used, which holds double value in the which is defined in the question.

In the main class, the "GradePrinter" object is created and defines a double variable "numericGrade", that calling method and store its value, and also use print method to print its value.

4 0
1 year ago
c++ You are given an array A representing heights of students. All the students are asked to stand in rows. The students arrive
Lilit [14]

The below code will help you to solve the given problem and you can execute and cross verify with sample input and output.

#include<stdio.h>

#include<string.h>

 int* uniqueValue(int input1,int input2[])

 {

   int left, current;

   static int arr[4] = {0};

   int i      = 0;

     for(i=0;i<input1;i++)

      {

         current = input2[i];

         left    = 0;

         if(current > 0)

         left    = arr[(current-1)];

      if(left == 0 && arr[current] == 0)

       {

       arr[current] = input1-current;

       }

       else

   {

       for(int j=(i+1);j<input1;j++)

       {

           if(arr[j] == 0)

           {

               left = arr[(j-1)];

               arr[j] = left - 1;

           }

       }

   }

}

return arr;

}

4 0
2 years ago
(Displaying a Sentence with Its Words Reversed) Write an application that inputs a line of text, tokenizes the line with String
Serjik [45]

Answer:

I am writing a JAVA and Python program. Let me know if you want the program in some other programming language.

import java.util.Scanner; // class for taking input from user

public class Reverse{ //class to reverse a sentence

public static void main(String[] args) { //start of main() function body

Scanner input = new Scanner(System.in); //create a Scanner class object

   System.out.print("Enter a sentence: "); //prompts user to enter a sentence

   String sentence = input.nextLine(); // scans and reads input sentence

   String[] tokens = sentence.split(" "); // split the sentence into tokens using split() method and a space as delimiter

   for (int i = tokens.length - 1; i >= 0; i--) { //loop that iterates through the tokens of the sentence in reverse order

       System.out.println(tokens[i]); } } } // print the sentence tokens in reverse order

Explanation:

In JAVA program the user is asked to input a sentence. The sentence is then broken into tokens using split() method. split method returns an array of strings after splitting or breaking the sentence based on the delimiter. Here the delimiter used is space characters. So split() method splits the sentence into tokens based on the space as delimiter to separate the words in the sentence. Next the for loop is used to iterate through the tokens as following:

Suppose the input sentence is "How are you"

After split() method it becomes:

How

are

you

Next the loop has a variable i which initialized to the tokens.length-1. This loop will continue to execute till the value of i remains greater than or equals to 0.

for (int i = tokens.length - 1; i >= 0; i--)

The length of first token i.e you is 3 so the loop executes and prints the word you.

Then at next iteration the value of i is decremented by 1 and now it points at the token are and prints the word are

Next iteration the value of i is again decremented by i and it prints the word How.

This can be achieved in Python as:

def reverse(sentence):

   rev_tokens = ' '.join(reversed(sentence.split(' ')))

   return rev_tokens

   

line = input("enter a sentence: ")

print((reverse(line)))

The method reverse takes a sentence as parameter. Then rev_tokens = ' '.join(reversed(sentence.split(' ')))  statement first splits the sentence into array of words/tokens using space as a delimiter. Next reversed() method returns the reversed sentence.  Next the join() method join the reversed words of the sentence using a space separator ' '.join(). If you want to represent the reversed tokens each in a separate line then change the above statement as follows:

rev_tokens = '\n'.join(reversed(sentence.split(' ')))  

Here the join method joins the reversed words separating them with a new line.

Simply input a line of text and call this reverse() method by passing the input line to it.

4 0
1 year ago
Other questions:
  • Email, instant messaging and most web traffic go across the internet in the clear; that is, anyone who can capture that informat
    15·2 answers
  • What will be the output of “AAAAMMMMMHHHVV” using a file compression technique?
    9·2 answers
  • Theresa is a certified teacher. She just had a baby and would like to stay home, but still wants to teach. Which career would be
    11·2 answers
  • Most GUIs provide all of the following except _____.
    12·2 answers
  • Julie is working on a spreadsheet with data about the company's profits. They will be sharing this data with customers. She chec
    6·2 answers
  • Because of the density of vehicle in urban areas, you should ____.
    8·2 answers
  • 9.10: Reverse ArrayWrite a function that accepts an int array and the array ’s size as arguments . The function should create a
    9·1 answer
  • Read first a user's given name followed by the user's age from standard input. Then use an ofstream object named outdata to writ
    6·1 answer
  • 4.2.3: Basic while loop expression. Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Fol
    6·2 answers
  • Write a program that allows you to create a file of customers for a company. The first part of the program should create an empt
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!