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 && 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.)
#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;
}