Question

In: Computer Science

The question is about Compiler Construction: Here's the current project: /*Project.txt*/ n=49; k=6; result=1; while(n!=k &&...

The question is about Compiler Construction:

Here's the current project:

/*Project.txt*/

n=49;
k=6;
result=1;

while(n!=k && k!=0){
result=result*n/k;
n=n-1;
k=k-1;
}

output result;

n=100;

for(i=2..n){
a[i]=1;
}

for(i=2..n){
if(a[i]==1){
output i;
j=2*i;
while(j<n){
a[j]=0;
j=j+i;
}
}
}

The new features to implement are the following:

1. Conditional branching like if (x==0 || y<z) then z=1; where conditions can contain comparison of expressions (equal, smaller,...), and Boolean connectives (and, or, not). You can also implement an optional else-clause if you like.

2. For-loops of the form for(i=2..n) where we limit the exibility of the for loop as compared to languages like Java and C++ where where can make more complex conditions and updates: here we simply specify two expression that denote the minimum (in the example 2) and maximum (here n) value of the variable of the for loop. If you like you can instead implement a more Java/C++ style syntax.

3. Arrays of the form a[e] where a is the name of the array and e is the index in the array to read or write. (We do not consider any complicated arrays like multi-dimensional arrays or arrays of arrays.)

Solutions

Expert Solution

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void implement_conditional_branching()
{
srand((unsigned) time(0));
int num=1+(rand()%100);
if(num==1)
cout<<"Number is one\n";
else if(num%10==0)
cout<<"Number is multiple of 10\n";
else if(num%2==0 && num%3==0)
cout<<"Number is multiple of 6\n";
else
cout<<"Number is neither multiple of 10 nor of 6. Also it is not 1.Hard luck!\n";
}
void implement_for_loop()
{
//print only odd numbers upto 100
cout<<"Odd numbers upto 100 are :\n";
for(int i=1;i<=100;i+=2)
{
cout<<i<<" ";
}
cout<<"\n";
}
void implement_array()
{
int arr[10]={1,2,3,4,5,6,7,8,9,10};
cout<<"Elements of array are :\n";
for(int i=0;i<10;i++)
{
cout<<arr[i]<<" ";
}
cout<<"\n";
}
int main()
{
implement_conditional_branching();
implement_for_loop();
implement_array();
return 0;
}


Related Solutions

Prove combinatorially: {{n \ choose x)= {{k+1 \ choose n-1) This question is asking to prove...
Prove combinatorially: {{n \ choose x)= {{k+1 \ choose n-1) This question is asking to prove combinatorially whether  n multi-choose k is equal to k+1 multi-choose n-1 Thank you
A flood control project has a construction cost of $10 million in year 1; $6 million...
A flood control project has a construction cost of $10 million in year 1; $6 million in year 2; and $2 million in year 3, when it is completed. Beginning in year 4, annual O&M costs are $200,000/year. The interest rate is 6%. Benefits also begin accruing in year 4, valued at $1.5 million that year. Thereafter, the benefits grow at a uniform rate of $30,000/year until the end of the project life of 50 years. Make a cash flow...
1​The Question Barings Bank collapsed about two decades ago as a result of derivative trading. Identify...
1​The Question Barings Bank collapsed about two decades ago as a result of derivative trading. Identify any other high profile corporate bankruptcy attributable to derivative trading and. I. Describe the events that created the problem outlining the differences and similarities between Barings Bank and your chosen institution II. What are the lessons that financial institutions, investors and regulators can learn from the occurrence of such events. 2​The Write Up The write up should follow this outline: i. Title page (University,...
Question 6 (1 point) Which of the following is NOT true about the incremental process model?...
Question 6 (1 point) Which of the following is NOT true about the incremental process model? Question 6 options: It has lower risk of overall project failure than waterfall model. Requirements are prioritized. There is no single “requirements phase” or “design phase”. It has distinct phases. Question 7 (1 point) Extreme programming (XP) uses user stories to represent software requirements. Question 7 options: True False Question 8 (1 point) Which of the following is an example of user stories? Question...
Question 6 Gunther Plc provides the following information on its acquisitions of non-current assets: (1) A...
Question 6 Gunther Plc provides the following information on its acquisitions of non-current assets: (1) A non-current asset (asset a) was acquired on 1 January 2016 for £100,000. It had no residual value and a useful economic life of 10 years. On 1 January 2019, the useful economic life was revised to 6 years. The company depreciates similar assets using the straight line method. (2) A non-current asset (asset b) was acquired for £12,500 at the beginning of 2017. It...
Question 2 0.73/1 View Policies Show Attempt History Current Attempt in Progress Sage Construction Company began...
Question 2 0.73/1 View Policies Show Attempt History Current Attempt in Progress Sage Construction Company began work on a $415,500 construction contract in 2020. During 2020, Sage incurred costs of $286,000, billed its customer for $209,500, and collected $178,500. At December 31, 2020, the estimated additional costs to complete the project total $152,650. Prepare Sage’s journal entry to record profit or loss, if any, using (a) the percentage-of-completion method and (b) the completed-contract method. (Credit account titles are automatically indented...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT