What's new

C & C++ Can someone enlighten me, please.

PHC rem

Forum Veteran
Elite
Joined
Jan 23, 2017
Posts
1,553
Solutions
2
Reaction
460
Points
674
Age
23
#include <stdio.h>

int main() {
int a[] = {1,0,2,1,9};
int b[] = {1,1,2,1,0,1};

printf("%d", a[b[a[1]]]+a[0]);

return 0;
}

ano po ba talaga ang output dito? my answer is 1.

but when i ask chatGPT about it, why is it saying that the output is 2.
 
Let's analyze the code to understand the output.

First, we have two integer arrays: a and b.

The printf statement a[b[a[1]]]+a[0] is used to calculate the output.

Let's break it down step by step:

1. a[1] refers to the 2nd element of array a, which is 0.
2. b[0] refers to the 1st element of array b, which is 1.
3. a[0] refers to the 1st element of array a, which is 1.

So far, we have a[b[a[1]]]+a[0] as a[1]+a[0].

4. a[1] refers to the 2nd element of array a, which is 0.
5. a[0] refers to the 1st element of array a, which is 1.

Now, we have a[1]+a[0] as 0+1, which equals 1.

Therefore, the correct output should be 1.

If "chatGPT" is suggesting that the output is 2, it might be misunderstanding the code or providing incorrect information. The correct output is indeed 1.
 
a[b[a[1]]] + a[0]

value ng a[0] is 1
value ng a[1] is 0

so replace a[0] with 1
replace a[1] with 0

a[b[0]] + 1

value ng b[0] is 1

so replace b[0] with 1

a[1] + 1

value of a[1] is 0

so replace a[1] with 0

0 + 1 = 1

Maski nung na compile ko 1 yung sagot

1708049581232.png
 

Attachments

a[b[a[1]]] + a[0]

value ng a[0] is 1
value ng a[1] is 0

so replace a[0] with 1
replace a[1] with 0

a[b[0]] + 1

value ng b[0] is 1

so replace b[0] with 1

a[1] + 1

value of a[1] is 0

so replace a[1] with 0

0 + 1 = 1

Maski nung na compile ko 1 yung sagot

View attachment 2888502
kaya nga eh, iwan ko ba diyan sa chatGPT hahaha. tas nag aargue pa yung kaklase ko sakin. bakit daw 1 yung sagot ko hahaha
 

Similar threads

Back
Top