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

1) What will the following code do? const int SIZE = 5; double x[SIZE]; for(int

ID: 3540502 • Letter: 1

Question

1) What will the following code do?

const int SIZE = 5;
double x[SIZE];
for(int i = 2; i <= SIZE; i++)
{
   x[i] = 0.0;
}

Answer

a.

Each element in the array is initialized to 0.0

b.

Each element in the array, except the first and the last, is initialized to 0.0

c.

Each element in the array, except the first, is initialized to 0.0

d.

An error will occur when the code runs



2) A program consists of two functions, main and DoCalc. A variable x is declared outside both functions. DoCalc declares two variables, a and b, within its body; b is declared as static. In what function(s) are each of a, b, and x visible, and what is the lifetime of each variable?


3) What is a scope? How is it defined in C++?


4) What should be done so that the following array contains numbers 10, 20, 30 .... 1000?
      int a[100];
      for (int i=0; i<100;i++);
      {
            a[i] = i;
      }

5) How do you define an array without providing a size declarator?

a.

Each element in the array is initialized to 0.0

b.

Each element in the array, except the first and the last, is initialized to 0.0

c.

Each element in the array, except the first, is initialized to 0.0

d.

An error will occur when the code runs

Explanation / Answer

1.

Each element in the array, except the first, is initialized to 0.0

In the, for loop array index starts with the second element therefore, the first element does not initialized.

Example Program:

#include<iostream>

using namespace std;

int main()

{

const int SIZE = 5;

double x[SIZE];

for(int i = 2; i <= SIZE; i++)

{

   x[i] = 0.0;

}

for(int i = 2; i <= SIZE; i++)

{        

cout <<"index"<<i<<": "<<x[i]<<endl;

}

    return 0;

}

Sample Output:

index2: 0                                                                                                                                                                                                                                              

index3: 0                                                                                                                                                                                                                                              

index4: 0                                                                                                                                                                                                                                              

index5: 0

Hence, the correct option is c.

-------------------------------------------------

2.

int x; // A variable x is declared outside both functions.

main()

{

   

}

void DoCalc()

{

    int a; // DoCalc declares two variables, a and b, within its body;

    static int b;// b is declared as static.

}

For variable x:

Scope (visibility) and lifetime of variable x is the entire duration of the program’s execution.

For variable a:

Local scope: "visible" within DoCalc function

Lifetime: The variable “a” has a lifetime that begins when program execution enters the DoCalc () function and ends when execution leaves DoCalc () function.

For variable b:

Local scope: "visible" within DoCalc function

Lifetime: lifetime of variable b is the entire duration of the program’s execution.

------------------------------------------------------------------

3.

:: is called scope resolution operator it is used to identify the scope of variable or class object.

Example:

#include<iostream>

using namespace std;

int n =10;

int main()

{

int n = 20;

cout << n << endl; // this prints 20 because it is accessing local n that is 20

// to access global n, use the scope resolution operator.

cout << ::n << endl; // this prints 10 because it is accessing global n through scope that is 10.

}

Sample output:

20                                                                                                                                                                                                                                                     

10

-----------------------------------------------------------------

4.

Program code:

#include<iostream>

using namespace std;

int main()

{

int a[100];

// use a for loop, to iterate index value from 0 to 100

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

{

// Array contains numbers 10, 20, 30 .... 1000

    a[i] = (i+1)*10;

}

// Display the array elements for 10 to 1000

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

{

    cout<<a[i]<<" ";

}

return 0;

}

Sample Output:

10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450 460 470 480 490 500 510 520 530 540 550 560 570 580 590 600 610 620 630 640 650 660 670 680 690 700 710 720 730 740 750 760 770 780 790 800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990 1000

-----------------------------------------------------------------

5.

To provides an initialized list of elements for the given array. The array is sized to hold the number of values in the array.

Syntax:

Type array_name[]={Array list of elements};

Example:

int a[] = {1,2,3,4,5};