Question

I need this program converted to c++ Please help if you can import java.util.*; // Add...

I need this program converted to c++ Please help if you can

import java.util.*;

// Add two arrays of the same size (size)
// Each array is a representation of a natural number
// The returned array will have the size of (size + 1) elements
public class Fibonacci
{

private static String arrToString(int[] arr) {
          String s = "";
          for (int i = 0; i < arr.length; i++) {
              s = s + arr[i];
          }
          return s;
      }
private static String getStringOfIntArrayNum(int[] num) {
          String result = arrToString(num); // Might contain leading zeros
          result = removeLeadingZeros(result);
       
          return result;
      }

private static int[] addTwoArrays(int[] arr1, int[] arr2) {
int size = arr1.length;
int[] arrTotal = new int[size + 1];
for (int i = 0; i < size; i++) {
    arrTotal[i] = 0;
}

int remaider = 0;
for (int i = size - 1; i >= 0; i--) {
    int temp = arr1[i] + arr2[i] + remaider;
    arrTotal[i + 1] = temp % 10;
    remaider = temp / 10;
}
arrTotal[0] = remaider;

return arrTotal;
}


private static int[] getFibArray(int n, int size) {
      // Return F(n) in the form of an array, with (size + 1) elements
      int[] fibArr1 = new int[size];
      int[] fibArr2 = new int[size];
      int[] fibResultArr = new int [size + 1];

      // Initially set up
      for (int i = 0; i < size; i++) {
        fibArr1[i] = fibArr2[i] = fibResultArr[i] = 0;
      }

      if (n == 0) {
        // return fibArr1;
        return (addTwoArrays(fibArr1, fibArr1));
      }

      if (n == 1) {
        fibArr2[size - 1] = 1;
        // return fibArr2;
        return (addTwoArrays(fibArr1, fibArr2));
      }

      /*
      // Do the Recursive way
      fibResultArr = addTwoArrays(getFibArray(n - 1, size - 1),
          getFibArray(n - 2, size - 1));
      */

      // Do the Iterative way
      fibArr2[size - 1] = 1;
      for (int i = 0; i < n - 1; i++) {
        fibResultArr = addTwoArrays(fibArr1, fibArr2);
        fibArr1 = fibArr2;

        int[] fibArr2Temp = new int[fibArr2.length];
        for (int j = 0; j < fibArr2.length; j++) {
          fibArr2Temp[j] = fibResultArr[j + 1];
        }
        fibArr2 = fibArr2Temp;
      }

      return fibResultArr;
    }


    private static String removeLeadingZeros(String s) {
      // "0" returns "0", "0012" returns "12"
      if (s.length() < 2)
        return s;

      int i;
      for (i = 0; i < s.length() - 1; i++) {
        char c = s.charAt(i);
        if (c != '0')
          break;
      }

      if (i == 0) {
        return s;
      }

      return s.substring(i);
    }


private static String getBiggestFib(int size) {
// Return the biggest F(n) that has less than (size) character.
String result = "";
int n = 0; // could have started with a 'near' value, such as 400
int[] fib; // getFibArray(n, size) has (size + 1) = 99 digits
if (size == 2)
    return "8";

while (true) {
    fib = getFibArray(n, size - 2);

    if (fib[0] != 0)
      break;
    n++;
}
int low = n;
// System.out.println("Low index is: " + low);
// low = 472 = min F(n) that has 99 digits

int[] fibAbove;
while (true) {
    fibAbove = getFibArray(n, size - 1);

    if (fibAbove[0] != 0)
      break;
    n++;
}
int high = n;


for (int i = low; i <= high; i++) {
    int[] f = getFibArray(i, size - 1);
    if (getStringOfIntArrayNum(f).length() >= size) {
      n = i - 1; // right before i that makes F(n) 100 digits
      break;
    }
}

result = getStringOfIntArrayNum(getFibArray(n, size - 1));

return result;
}

public static void main(String args[])
{
System.out.println("Biggest Fibonacci number that has less than 100 digits : " + getBiggestFib(100));
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;

string removeLeadingZeros(string s) {
// "0" returns "0", "0012" returns "12"
if (s.length() < 2)
return s;

int i;
for (i = 0; i < s.length() - 1; i++) {
char c = s[i];
if (c != '0')
break;
}

if (i == 0) {
return s;
}

return s.substr(i,s.length());
}
  
string arrToString(int *arr,int n) {       //n is the size of the array
string s = "";
for (int i = 0; i < n; i++) {
   char c = '0'+arr[i];
s = s + c;
}
return s;
}

string getStringOfIntArrayNum(int *num,int n) {   //n is the length of the array
string result = arrToString(num,n); // Might contain leading zeros
result = removeLeadingZeros(result);
return result;
}

int* addTwoArrays(int *arr1, int *arr2,int size) {       //n is the size of arr1
  
   int *arrTotal = new int[size + 1];
   for (int i = 0; i < size; i++) {
   arrTotal[i] = 0;
   }

   int remaider = 0;
   for (int i = size - 1; i >= 0; i--) {
   int temp = arr1[i] + arr2[i] + remaider;
   arrTotal[i + 1] = temp % 10;
   remaider = temp / 10;
   }
   arrTotal[0] = remaider;
   return arrTotal;
}


int* getFibArray(int n, int size) {
// Return F(n) in the form of an array, with (size + 1) elements
int *fibArr1 = new int[size];
int *fibArr2 = new int[size];
int *fibResultArr = new int [size + 1];

// Initially set up
for (int i = 0; i < size; i++) {
fibArr1[i] = fibArr2[i] = fibResultArr[i] = 0;
}

if (n == 0) {
// return fibArr1;
return (addTwoArrays(fibArr1, fibArr1,size));
}

if (n == 1) {
fibArr2[size - 1] = 1;
// return fibArr2;
return (addTwoArrays(fibArr1, fibArr2,size));
}

/*
// Do the Recursive way
fibResultArr = addTwoArrays(getFibArray(n - 1, size - 1),
getFibArray(n - 2, size - 1));
*/

// Do the Iterative way
fibArr2[size - 1] = 1;
for (int i = 0; i < size - 1; i++) {
fibResultArr = addTwoArrays(fibArr1, fibArr2,size);
fibArr1 = fibArr2;

int *fibArr2Temp = new int[size];
for (int j = 0; j < size; j++) {
fibArr2Temp[j] = fibResultArr[j + 1];
}
fibArr2 = fibArr2Temp;
}

return fibResultArr;
}


  

string getBiggestFib(int size) {
// Return the biggest F(n) that has less than (size) character.
string result = "";
int n = 0; // could have started with a 'near' value, such as 400
int *fib; // getFibArray(n, size) has (size + 1) = 99 digits
if (size == 2)
return "8";

while (true) {
fib = getFibArray(n, size - 2);

if (fib[0] != 0)
break;
n++;
}
int low = n;
// System.out.println("Low index is: " + low);
// low = 472 = min F(n) that has 99 digits

int *fibAbove;
while (true) {
fibAbove = getFibArray(n, size - 1);

if (fibAbove[0] != 0)
break;
n++;
}
int high = n;


for (int i = low; i <= high; i++) {
int *f = getFibArray(i, size - 1);
if (getStringOfIntArrayNum(f,size).length() >= size) {
n = i - 1; // right before i that makes F(n) 100 digits
break;
}
}

result = getStringOfIntArrayNum(getFibArray(n, size - 1),size);

return result;
}

int main()
{
cout<<"Biggest Fibonacci number that has less than 100 digits : "<<getBiggestFib(100);
}

There are no errors in it, please check for logical correctness of the program.

Please appreciate the solution if you find it helpful.

Add a comment
Know the answer?
Add Answer to:
I need this program converted to c++ Please help if you can import java.util.*; // Add...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • I need to make this code access the main method I suppose, but I don't know...

    I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...

  • Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static...

    Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static String solve(char[][] grid) {        // TODO        /*        * 1. Construct a graph using grid        * 2. Use BFS to find shortest path from start to finish        * 3. Return the sequence of moves to get from start to finish        */               // Hardcoded solution to toyTest        return...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • need help to write the main method as the template import java.util.*; public class oBST {...

    need help to write the main method as the template import java.util.*; public class oBST { static float optimalBST(String words[], float freq[], int n) { //2D dp matrix float dp[][] = new float[n + 1][n + 1]; int root[][] = new int[n+1][n+1]; // For a single word, cost is equal to frequency of the word for (int i = 0; i < n; i++) dp[i][i] = freq[i]; // Now consider for size 2, 3, ... . for (int L =...

  • i need the help to fix thie problrm in this code ( The output is 0 0, which means both are not the same? The assignment has already stated the first 2 are considered the same.) t...

    i need the help to fix thie problrm in this code ( The output is 0 0, which means both are not the same? The assignment has already stated the first 2 are considered the same.) this is the question ••• E6.10Write a function bool same_elements(int a[], int b[], int size) that checks whether two arrays have the same elements in some order, with the same multiplicities. For example, 1 4 9 16 9 7 4 9 11 and 11...

  • PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Given...

    PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count = 0; for (int i = 0; i < SIZE; i++) { if (arri[i] > arr2[i]) count++; } return count;...

  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint:...

    PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint: the number array in task 2 is work for task 3 in figure 1.   Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count = 0; for (int i...

  • Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{       ...

    Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{        File prg8 = new File("program8.txt");        Scanner reader = new Scanner(prg8);        String cName = "";        int cID = 0;        double bill = 0.0;        String email = "";        double nExempt = 0.0;        String tExempt = "";        int x = 0;        int j = 1;        while(reader.hasNextInt()) {            x = reader.nextInt();}        Customers c1 [] = new Customers [x];        for (int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT