Question

But what about the Van Eck Sequence 0, 0, 1, 0, 2, 0, 2, 2, 1,...

But what about the Van Eck Sequence 0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, ...? Let's develop a program that will produce and output the first 100 values in the sequence! Prompt the user for the first value in the sequence Generate the next 99 values in the sequence following the sequencing rules Store all of the first 100 values of the sequence in an integer array Output each of the 100 values to the screen, with tabs separating each value Output each of the 100 values to a text file, with spaces separating each value Please submit only your CPP file c++

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

Program:

#include <iostream>

#include <fstream>

using namespace std;

// function which will tell us if the element is present inside vanEckSequence[] or not

int isPresent(int vanEckSequence[], int element, int index)

{

    for (int i = index; i >= 0; i--)

    {

        if (vanEckSequence[i] == element)

        {

            return i;

        }

    }

    return -1;

}

int main()

{

    int vanEckSequence[100]; // create an integer array to store all of the first 100 values of the sequence

    ofstream file;

    // VanEckSequence.txt is the name of the text file to which each of the 100 values would be written

    file.open("VanEckSequence.txt");

    // Prompt the user for the first value in the sequence

    cout << "Enter the first value in the sequence: ";

    cin >> vanEckSequence[0];

    // loop to generate the next 99 values in the sequence following the sequencing rules

    // and to store all of the first 100 values of the sequence in an integer array

    for (int i = 1; i < 100; i++)

    {

        int found = isPresent(vanEckSequence, vanEckSequence[i - 1], i - 2);

        if (found == -1)

        {

            vanEckSequence[i] = 0;

        }

        else

        {

            vanEckSequence[i] = i - 1 - found;

        }

    }

    for (int i = 0; i < 100; i++)

    {

        // Output each of the 100 values to the screen, with tabs separating each value

        cout << vanEckSequence[i] << "   ";

        // Output each of the 100 values to a text file, with spaces separating each value

        file << vanEckSequence[i] << " ";

    }

    return 0;

}

Note: Please refer to the screenshot of the code to understand the indentation of the code.

Screenshot of the code:

Output and Input:

VanEckSequence.txt:

Add a comment
Know the answer?
Add Answer to:
But what about the Van Eck Sequence 0, 0, 1, 0, 2, 0, 2, 2, 1,...
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
  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13,...

    The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … The next number is found by adding up the two numbers before it. For example, the 2 is found by adding the two numbers before it (1+1). The 3 is found by adding the two numbers before it (1+2). The 5 is found by adding the two numbers before it (2+3), and so on! Each number in the sequence is called...

  • Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5,...

    Fibonacci Sequence The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) The 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on!         Example: the next number in the sequence above is 21+34 = 55 Source:...

  • Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formal...

    Problem 2: (8 pts) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8.,.. Formally, it can be expressed as: fib0-0 fibl-1 fibn-fibn-1+fibn-2 Write a multithreaded program that generates the Fibonacci sequence. This program should work as follows: On the command line, the user will enter the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • Check the words in bold. The Fibonacci sequence is the series of numbers 0,1,1, 2, 3,...

    Check the words in bold. The Fibonacci sequence is the series of numbers 0,1,1, 2, 3, 5, 8,.... Formally, it can be expressed as: fib0 = 0 fib1 = 1 fibn = fibn-1 + fibn-2 Write a multithreaded program that generates the Fibonacci sequence using the Win32 thread library(not pthreads because it does not work on windows). This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program is...

  • • Fibonacci numbers, denoted as Fn, form a sequence, called the Fibonacci sequence, such that each...

    • Fibonacci numbers, denoted as Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. • Fn = Fn-1 + Fn-2 (n > 1) • Fo = 0 and F1 = 1 • Submit the R script to write the first 100 numbers of Fibonacci sequence, i.e., F, to F99, to a file named as Fibonacci_100.txt and put in the folder in path /user/R/output/. •...

  • Write a program in c++ that generates a 100 random numbers between 1 and 1000 and...

    Write a program in c++ that generates a 100 random numbers between 1 and 1000 and writes them to a data file called "randNum.dat". Then re-open the file, read the 100 numbers, print them to the screen, and find and print the minimum and maximum values. Specifications: If your output or input file fails to open correctly, print an error message that either states: Output file failed to open Input file failed to open depending on whether the output or...

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