Unformatted text preview:

21 What does this program print EXPLAIN every line include iostream includes iostream header files for using cout using namespace std uses std namespace int main main function main part of the program starts here int a 3 3 a is declared as an integer array of size 3x3 initialized with values 1 2 3 4 5 6 7 8 9 int pa 3 a 0 a 1 a 2 initializing pa as a pointer to every row of a int p a 0 initializing p as pointer to first element of a int i declaring variable i as a integer For every loop iteration it prints minor diagonal element first element major diagonal element So output is 311 545 779 for i 0 i 3 i for loop incrementing i from 0 to 2 by one cout a i 2 i a i a i i endl cout endl goes to next line in output screen for i 0 i 3 i for loop incrementing i from 0 to 2 by one cout pa i p i endl cout endl return 0 return statement that ends the program For every loop iteration it prints the value of pa which holds every row of a so pa i will print first element of every row p prints every element in first row for each iteration So output is 11 42 73 22 What does this program print EXPLAIN every line Note and have same operator precedence and both have operator associativity as right to left include iostream includes iostream header files for using cout using namespace std uses std namespace int main main function main part of the program starts here int a 0 1 2 3 4 a is initialized with values 0 to 4 int p a a 1 a 2 a 3 a 4 p holds the address of individual elements of a int pp p pp is pointing to the same memory p points to cout a a endl prints address of array a and value of first element of a cout p p p endl prints address of array p and address a and first value at p which Is address of a and value of first element of a cout pp pp pp endl same as above since pp and p points to the same array a a 1 a 2 a 3 a 4 pp pp points second element of p array cout pp p pp a pp endl pp p index difference of second element of p array and first element of p array 1 pp a is difference of a 1 and a 1 pp value at a 1 1 pp pp points to 3rd element of p array This is same as pp as associativity is right to left for these and operators cout pp p pp a pp endl pp p index difference of 3rd element and 1st element of p array 2 pp a is difference of a 2 and a 2 pp value at a 2 2 pp pp points to 4th element of p array This is same as pp cout pp p pp a pp endl pp p index difference of 4th element and 1st element of p array 3 pp a is difference of a 3 and a 3 pp value at a 3 3 pp This is same as pp 4th element a 3 is changed to a 4 in p array cout pp p pp a pp endl pp p index difference of 4th element and 1st element of p array 3 pp a is difference of a 4 and a 4 pp value at a 4 4 pp p pp becomes same as p So both p and pp point to first element a pp This is pp So pp points to second element of p array cout pp p pp a pp endl pp p index difference of 2nd element and 1st element of p array 1 pp a is difference of a 1 and a 1 pp value at a 1 1 pp This is same as pp Value at pp a 1 is incremented by 1 2 nd element of p array becomes a 2 cout pp p pp a pp endl pp p index difference of 2nd element and 1st element of p array 1 pp a is difference of a 2 and a 2 pp value at a 2 2 pp This is same as pp So value at a 2 2 is incremented to 3 cout pp p pp a pp endl pp p index difference of 2nd element and 1st element of p array 1 pp a is difference of a 2 and a 2 pp value at a 2 3 getchar return 0 Final Output 111 222 333 344 111 122 123


View Full Document

UCSB CS 16 - Practice Problem

Documents in this Course
Load more
Loading Unlocking...
Login

Join to view Practice Problem and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Practice Problem and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?