CS402 Lab 8

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

1.

Identify the output and show the steps of the following program segments:

(a) main()
{
int i, j=0;
int aryA[3]={0}, aryB[10]={2,3,1,2,2,3,1,2,1,3};
for(i=0; i<10; i++)
{ if (aryB[i]==1)
aryA[0]++;
else if (aryB[i]==2)
aryA[1]++;
else if (aryB[i]==3) aryA[2]++;
j+=aryB[i];
}
cout<<aryA[0]<<" "<<aryA[1]<<" "<<aryA[2]<<endl;
}

arrayB[i] == arrayA[0]++ arrayB[i]==2 arrayA[1]++ arrayB[i]== arrayA[2]++ i++


i < 10 1 3
- T 1 F - 0=0+1
0 F
- F - T 1 2=1+1
1 F
1 F - F - 3=2+1
2 T
- T 2 F - 4=3+1
3 F
- T 3 F - 5=4+1
4 F
F - F - T 2 6=5+1
5
T 2 F - F - 7=6+1
6
F - T 4 F - 8=7+1
7
T 3 F - F - 9=8+1
8
F - F - T 3 10=9+1
9
- - - - - - -
10
b)

j j!=10 j%2==1 num[j]=1 Print j++

0 T F 0 Element 0 = 0 1= 0+1

1 T T 1 Element 1 = 1 2= 1+1

2 T F 0 Element 2 = 2 3= 2+1

3 T T 1 Element 3 = 3 4= 3+1

4 T F 0 Element 4 = 4 5= 4+1

5 T T 1 Element 5 = 5 6= 5+1

6 T F 0 Element 6 = 6 7= 6+1

7 T T 1 Element 7 = 7 8= 7+1

8 T F 0 Element 8 = 8 9= 8+1

9 T T 1 Element 9 = 9 10= 9+1

10 - - - - -

Element 0 = 0

Element 1 = 1

Element 2 = 2

Element 3 = 3

Element 4 = 4

Element 5 = 5

Element 6 = 6

Element 7 = 7

Element 8 = 8
Element 9 = 9

j j<5 Print k k<5 j++ Print k++


0 T No.1 = 11 0 T 1=0+1 No.1 = 0 1=0+1
1 T No.2 = 12 1 T 2=1+1 No.2 = 0 2=1+1
2 T No.3 = 13 2 T 3=2+1 No.3 = 0 3=2+1
3 T No.4 = 14 3 T 4=3+1 No.4 = 0 4=3+1
4 T No.5 = 15 4 T 5=4+1 No.5 = 0 5=4+1
5 F

No.1 = 11

No.2 = 12

No.3 = 13

No.4 = 14

No.5 = 15

No.1 = 0

No.2 = 0

No.3 = 0

No.4 = 0

No.5 = 0
2. Answer the following questions regarding an array called numbers.

a) Define a constant variable SIZE and initialized it to 10.

int SIZE=10;

b) Declare an array with SIZE elements of type double and

initialize the elements to 1.

double [ ] numbers= new double[SIZE];

c) Name the 4th element of the array.

numbers[3]

d) Assign the value 72.5 to the third element of the array.

numbers[2]=72.5;

e) Use a for epetition statement to store each statement of the

array by reading the values from the user and print all the array

elements.

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

numbers[i]= number[i];

You might also like