Question

In: Computer Science

C++ CLion using Namespace std Draw a picture of the array that would be created by...

C++ CLion

using Namespace std

Draw a picture of the array that would be created by the following code


int data[10] = {1, 1};
for( int index = 2; index < 8; ++index )
data[ index ] = data[index – 1] + data[index – 2];

Solutions

Expert Solution


   int data[10] = {1, 1}; // will create an array of int data of size 10 where the first 2 elements i.e index 0 and 1 will have value 1 and other locations will have value of 0 (since by default integer variables are set to 0 in C++)

data[0] = 1

data[1] =1

data[2] ... data[9] = 0

   // loop that starts from 2 and continues till index 7 (inclusive)
   for(int index = 2; index < 8; ++index)
       data[index] = data[index-1] + data[index-2]; // sets data at index to sum of values of data at index-1 and index-2

The above loop iterations :

1st Iteration, index = 2,

data[2] = data[1] + data[0]

= 1 + 1 = 2

2nd Iteration , index = 3

data[3] = data[2] + data[1]

= 2 + 1 = 3

3rd Iteration , index = 4

data[4] = data[3] + data[2]

= 3 + 2 = 5

4th Iteration , index = 5

data[5] = data[4] + data[3]

= 5 + 3 = 8

5th Iteration , index = 6

data[6] = data[5] + data[4]

= 8 + 5 = 13

6th Iteration , index = 7

data[7] = data[6] + data[5]

= 13 + 8 =21

7th Iteration , index = 8

Since index = 8 , it exits the loop

After the loop, array:

array = {1,1,2,3,5,8,13,21,0,0}

After the execution of above code the array will look like:

1 1 2 3 5 8 13 21 0 0

Related Solutions

Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std;...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std; #include "Stack.h" template <typename DataType> class StackArray : public Stack<DataType> { public: StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE); StackArray(const StackArray& other); StackArray& operator=(const StackArray& other); ~StackArray(); void push(const DataType& newDataItem) throw (logic_error); DataType pop() throw (logic_error); void clear(); bool isEmpty() const; bool isFull() const; void showStructure() const; private: int maxSize; int top; DataType* dataItems; }; //-------------------------------------------------------------------- // // // ** Array implementation of the Stack ADT...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
C++ exercise: The statements in the file main.cpp are in incorrect order. using namespace std; #include...
C++ exercise: The statements in the file main.cpp are in incorrect order. using namespace std; #include <iostream> int main() { string shape; double height; #include <string>    cout << "Enter the shape type: (rectangle, circle, cylinder) "; cin >> shape; cout << endl;    if (shape == "rectangle") { cout << "Area of the circle = " << PI * pow(radius, 2.0) << endl;    cout << "Circumference of the circle: " << 2 * PI * radius << endl;...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
Writing an nth root program in C++ using only: #include using namespace std; The program must...
Writing an nth root program in C++ using only: #include using namespace std; The program must be very basic. Please don't use math sqrt or pow . For example, the 4th root of 16 is 2 because 2 * 2 * 2 * 2 = 16. The 4th root of 20 is 2 because 2 * 2 * 2 * 2 = 16 and 16 is less than 20, and 3 * 3 * 3 * 3 = 81, which...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned char c1; unsigned char c2; unsigned char c3; unsigned char c4; unsigned long i1 (0xaabbccee); _asm { } cout.flags (ios::hex); cout << "results are " << (unsigned int) c1 << ", " << (unsigned int) c2 << ", " << (unsigned int) c3 << ", " << (unsigned int) c4 << endl; } Inside the block denoted by the _asm keyword, add code to...
Can someone make this code work without using template? C++ using namespace std; template < typename...
Can someone make this code work without using template? C++ using namespace std; template < typename T>    void print_array(T arr[], int size)    {        ofstream outfile;        outfile.open("/Users/android/Desktop/outfile.txt");        cout << "Printing Array: " << endl;        for (int i = 0; i < size; i++)        {            cout << arr[i] << endl;            outfile << arr[i] << endl;        }    } template < typename T>...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std;...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std; int main() { char answer; char grade; cout << "Are you taking a class (enter y, Y, N or N): " << endl; cin >> answer; if (answer == 'N' || 'n'); { cout << "Thanks for using the system" << endl; } else if (answer == 'Y' || 'y'); { cout << "Enter a letter grade (A, B, C, D, or F): "...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void selectionSort(string [], int); void displayArray(string [], int); void readNames(string[], int); int main() {    const int NUM_NAMES = 20;    string names[NUM_NAMES];    // Read the names from the file.    readNames(names, NUM_NAMES);    // Display the unsorted array.    cout << "Here are the unsorted names:\n";    cout << "--------------------------\n";    displayArray(names, NUM_NAMES);    // Sort the array.    selectionSort(names, NUM_NAMES);    //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT