Question

In: Computer Science

1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...

1. Convert the following code shown below to C++ code:

public class HighwayBillboard {
public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) {
int[] MR = new int[distance + 1];
//Next billboard which can be used will start from index 0 in billboard[]
int nextBillBoard = 0;
//example if milesRes = 5 miles then any 2 bill boards has to be more than
//5 miles away so actually we can put at 6th mile so we can add one mile milesRes
milesRes = milesRes + 1; // actual minimum distance can be between 2 billboards
MR[0] = 0;
for (int i = 1; i <= distance; i++) {
//check if all the billboards are not already placed
if(nextBillBoard < billboard.length){
//check if we have billboard for that particular mile
//if not then copy the optimal solution from i-1th mile
if (billboard[nextBillBoard] != i) {
//we do not have billboard for this particular mile
MR[i] = MR[i - 1];
} else {
//we do have billboard for this particular mile
//now we have 2 options, either place the billboard or ignore it
//we will choose the optimal solution
if(i>=milesRes){
MR[i] = Math.max(MR[i - milesRes] + revenue[nextBillBoard], MR[i - 1]);
}else{
//there are no billboard placed prior to ith mile
//we will just place the billboard
MR[i] = revenue[nextBillBoard];
}
nextBillBoard++;
}
}else{
//All the billboards are already placed
//for rest of the distance copy the previous optimal solution
MR[i] = MR[i - 1];
}
}
//System.out.println(Arrays.toString(MR));
return MR[distance];
}
public static void main(String[] args) {
int[] x = {6, 7, 12, 13, 14};
int[] revenue = {5, 6, 5, 3, 1};
int distance = 20;
int milesRestriction = 5;
HighwayBillboard h = new HighwayBillboard();
int result = h.maxRevenue(x, revenue, distance, milesRestriction);
System.out.println("Maximum revenue can be generated :" + result);
}
}

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;

class HighwayBillboard 
{
    public:
    int maxRevenue(int billboard[], int revenue[], int distance, int milesRes) 
    {
        int MR [distance+1];
        int nextBillBoard = 0;
        milesRes = milesRes + 1;
        MR[0] = 0;
        for (int i = 1; i <= distance; i++) 
        {
            int size=sizeof(billboard)/sizeof(billboard[0]);
            if(nextBillBoard < size)
            {
                if (billboard[nextBillBoard] != i) 
                {
                    MR[i] = MR[i - 1];
                } 
                else 
                {
                    if(i>=milesRes)
                    {
                        MR[i] =max(MR[i - milesRes] + revenue[nextBillBoard], MR[i - 1]);
                        
                    }
                    else
                    {
                        
                    }
                    nextBillBoard++;
                    
                }
                
            }
            else
            {
                MR[i] = MR[i - 1];
            }
            
        }
        return MR[distance];
        
    }
};

int main()
{
    int x[] = {6, 7, 12, 13, 14};
    int revenue[] = {5, 6, 5, 3, 1};
    int distance = 20;
    int milesRestriction = 5;
    HighwayBillboard h;
    int result = h.maxRevenue(x, revenue, distance, milesRestriction);
    cout<<"Maximum revenue can be generated :"<<result;
}

Hey, this the exact C++ code of the Java code you have provide.
You may compare it, not even a single char is changed.

Everything is same except the syntax which is of C++.


Related Solutions

Given the following Java code: class C { public int foo(C p) { return 1; }...
Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
1. Consider the following code: public class Widget implements Serializable { private int x; public void...
1. Consider the following code: public class Widget implements Serializable { private int x; public void setX( int d ) { x = d; } public int getX() { return x; } writeObject( Object o ) { o.writeInt(x); } } Which of the following statements is true? I. The Widget class is not serializable because no constructor is defined. II. The Widget class is not serializable because the implementation of writeObject() is not needed. III. The code will not compile...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main()...
Please convert the following C program into the RISC-V assembly code 1) #include <stdio.h> int main() { int i = 2, j = 2 + i, k; k = i * j; printf("%d\n", k + j); return 0; } 2) #include <stdio.h> int main() { int i = 2, j = 2 + i, k = j / 2; if (k == 1) { printf("%d\n", j) k = k * 2; if ( k == j) { printf("%d\n|, j); }...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document...
GIVEN THE CODE BELOW Given the following class: /** * Document class. */ public class Document { private int words; /** * constructor * pre: none * post: A Document object created. Words initialized to 0. */ public Document() { words = 0; //default words } /** * Changes the number of document words. * pre: none * post: Words has been changed. */ public void setWords(int numWords) { words = numWords; } /** * Calculates the number of pages....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT