Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task B. Print all integers from the requested interval. Write a program print-in

ID: 3745851 • Letter: T

Question

Task B. Print all integers from the requested interval.

Write a program print-interval.cpp that asks the user to input two integers L and U(representing the lower and upper limits of the interval), and print out all integers in the range L i < U separated by spaces. Notice that we include the lower limit, but exclude the upper limit.

Example

You can use any loop construct to achieve this result, but the most idiomatic way to do such an iteration in C++ is to use a for loop that starts with a counter variable i = L and keeps iterating while i < U:

As a side note, also notice that if you change the middle condition in the loop to i <= U, then the iteration would go from L to U inclusive its upper bound (L i U).

Introducing arrays.

If you need to store values in a table-like fashion, C++ provides us with the array data structure. The following code

creates an array called myData with 10 cells each storing integer values:

The cells of the array are indexed from 0 through 9, and can be accessed by specifying their index in square brackets, myData[i]. You can treat arrays as tables whose elements can be accessed by their index. For example:

Task C. Using arrays to store, update, and retrieve numbers.

Write a program edit-array.cpp that creates an array of 10 integers, and provides the user with an interface to edit any of its elements. Specifically, it should work as follows:

Create an array myData of 10 integers.

Fill all its cells with value 1 (using a for loop).

Print all elements of the array on the screen.

Ask the user to input the cell index i, and its new value v.

If the index i is within the array range (0 i < 10), update the asked cell, myData[i] = v, and go back to the step 3. Otherwise, if index i is out of range, the program exits.

The repetition of the steps 3-4-5 can be implemented with a do while loop:

The program should keep running until the user inputs an out-of-range (invalid) index.

Example:

Test your program. Check that all array elements are editable, for instance, use the program interface to make it print out sequence 5 10 15 20 25 30 35 40 45 50. Check that all possible edge cases are correctly handled, and the program exits when invalid index is input.

Task D. Computing Fibonacci Numbers with Loops and Arrays 0, 1, 1, 2, 3, 5, 8, 13…

First, a quick intro:

Fibonacci numbers is a sequence of numbers that starts with F(0) = 0 and F(1) = 1, with all the following numbers computed as the sum of two previous ones, F(n) = F(n1) + F(n2):

0
1
1 (=1+0)
2 (=1+1)
3 (=2+1)
5 (=3+2)
8 (=5+3)
13 (=8+5)
… and so on …

To make a C++ program to keep track of the previous numbers so that we can compute the new ones, we can use an array of integers:

The task:

Write a program fibonacci.cpp, which uses an array of ints to compute and print all Fibonacci numbers from F(0) to F(59).

Example:

Once your program is complete and works, check carefully the values printed on the screen. Specifically, what is happening when the numbers approach two billions? We expect that at some point the numbers start diverging from what they should be. Describe what you observe and explain why it is happening in a program comment.

Explanation / Answer

Task B:

#include<iostream>
using namespace std;

int main()
{
   int L,U;
   cout<<"Please enter L:";
   cin>>L;
   cout<<"Please enter U:";
   cin>>U;
   for(int i = L; i < U; i++)
   {
       cout<<i<<" ";
   }
}
/*
output-->
neeta@localhost ~]$ g++ print_interval.cpp
[neeta@localhost ~]$ ./a.out
Please enter L:-5
Please enter U:10   
-5   -4   -3   -2   -1   0   1   2   3   4   56   7   8   9  
*/

Task C:

#include<iostream>
using namespace std;

int main()
{
   int myData[10],k,i,v;
   for(k=0;k<10;k++)
       myData[k]=1;
  
   cout<<" Array is : ";
   for(k=0;k<10;k++)
       cout<<myData[k];
   do{
   cout<<" Array is : ";
   for(k=0;k<10;k++)
       cout<<" "<<myData[k];
   cout<<" Input index : ";
   cin>>i;
   cout<<" Input value : ";
   cin>>v;
   if(i>=0 && i<10)
       myData[i]=v;
   else
       cout<<" Index out of range :Exit";
      
   }while(i>=0 && i<10);
return 0;
}

/*
Output-->

[neeta@localhost ~]$ ./a.out

Array is :   1111111111
Array is :       1   1   1   1   1   1   1   1   1   1
Input index : 3

Input value : 45

Array is :       1   1   1   45   1   1   1   1   1   1
Input index : 2

Input value : 56

Array is :       1   1   56   45   1   1   1   1   1   1
Input index : 9

Input value : 342

Array is :       1   1   56   45   1   1   1   1   1   342
Input index : 1

Input value : 33

Array is :       1   33   56   45   1   1   1   1   1   342
Input index : 0

Input value : 56

Array is :       56   33   56   45   1   1   1   1   1   342
Input index : 12

Input value : 44

Index out of range :Exit

Taks D:

#include<iostream>
using namespace std;

int main()
{
   unsigned long int fib[60];
   int U=59;

   fib[0] = 0;
   cout<<fib[0]<<endl;
   fib[1] = 1;
   cout<<fib[1]<<endl;
   for(int i=2;i<=U;i++)
   {
       fib[i] = fib[i-1] + fib[i-2];  
       cout<<i<<"="<<fib[i]<<endl;
   }
return 0;}

/*
[neeta@localhost ~]$ ./a.out
0
1
2=1
3=2
4=3
5=5
6=8
7=13
8=21
9=34
10=55
11=89
12=144
13=233
14=377
15=610
16=987
17=1597
18=2584
19=4181
20=6765
21=10946
22=17711
23=28657
24=46368
25=75025
26=121393
27=196418
28=317811
29=514229
30=832040
31=1346269
32=2178309
33=3524578
34=5702887
35=9227465
36=14930352
37=24157817
38=39088169
39=63245986
40=102334155
41=165580141
42=267914296
43=433494437
44=701408733
45=1134903170
46=1836311903
47=-1323752223
48=512559680
49=-811192543
50=-298632863
51=-1109825406
52=-1408458269
53=1776683621
54=368225352
55=2144908973
56=-1781832971
57=363076002
58=-1418756969
59=-1055680967
[neeta@localhost ~]


Actual values
47 : 2971215073
48 : 4807526976
49 : 7778742049
50 : 12586269025
51 : 20365011074
52 : 32951280099
53 : 53316291173
54 : 86267571272
55 : 139583862445
56 : 225851433717
57 : 365435296162
58 : 591286729879
59 : 956722026041
As you can see here after 47 Fibonacci Number exceeds the range of a 32Bit/4Byte integer.
Everything after that will become negative.
To prevent negativeness, we can use unsigned long int which makes all your results positive and gives the ability to model "slightly" bigger numbers.

However still we get the wrong results.
Still data type is too small.
To fix this we can use unsigned long long or uint64_t.

Remember even for such big datatypes that can represent numbers up to approx. 18 trillion/quintillion (10^18) the Fibonacci numbers exceed this at the 89th iteration.

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote