Question

What is the output of the following. Explain why this output was obtained. int k[3] =...

What is the output of the following. Explain why this output was obtained.
int k[3] = { 10, 20, 30 };
int *kp;
kp = k;
printf("%d\n", *kp + 2);
printf("%d\n", *(kp + 2));

Find the output of the following:
double z = 56.4;
double *p;
p = &z;
printf("%lf\n", *p / 2);

Suppose the following array has already been declared. Declare a pointer to this array and then use the pointer in a loop to multiply each array value by 2.
int ar[] = { 28, 95, -45, 67, -61 };

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int k[3] = { 10, 20, 30 };
int *kp;
kp = k;
printf("%d\n", *kp + 2);
printf("%d\n", *(kp + 2));

Output:-

12

30

Explanation:-

IN the first printf statement , *kp is pointing to the initial value of the array i.e. 10 so *kp will hold the value 10 and adding 2 will result in 12.

In the next printf statement , kp is holding the address of first element of array adding 2 will point to the 3rd element of array so *(kp+2) will give 30

=================================================================

double z = 56.4;
double *p;
p = &z;
printf("%lf\n", *p / 2);​​​​​​​

Output:-

28.200000

Explanation:-

p is storing the address of z , so *p will hold the value of z i.e. 56.4 . so dividing by 2 will result in 28.2

=================================================================

Please Refer to the code below:-

#include <stdio.h>

int main()
{
// Declaring std::array<T, N> ;
int ar[] = { 28, 95, -45, 67, -61 };
// Declaring pointer and loop variable
int *kp,i;
// Finding size of array
int n = sizeof(ar)/sizeof(ar[0]);
// initializing pointer
kp =ar;
// multiplying array by 2 using pointer
for(i=0;i<n;i++)
{
ar[i]=*(kp+i)*2;
}
// printing array
for(i=0;i<n;i++)
{
printf("%d\n", ar[i]);
}


return 0;
}

Output:-

56                                                                                  

190                                                                                 

-90                                                                                 

134                                                                                 

-122    

Please Refer to the images below:-

===============================================

Please upvote

​​​​​​​==============================================

Add a comment
Know the answer?
Add Answer to:
What is the output of the following. Explain why this output was obtained. int k[3] =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 20) What is the output of the following segment of C code: int avg(int n, int*...

    20) What is the output of the following segment of C code: int avg(int n, int* a); int main () {             int array[4]={1,0,6,9};             printf("%d", avg(4, array)+ 1);             system("pause");             return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...

  • Need help with these questions in C programming: 9. What is the output of the following...

    Need help with these questions in C programming: 9. What is the output of the following code? int X[10]={0}; int N=4; for(int k=0; k<N:k++) X[k] = k*2: printf("%d", X[N/2]; 10. Write a single statement to accomplish each of the following. Assume that each of these statements applies to the same program. a. Write a statement that opens file "oldmast.dat" for reading and assigns the returned file pointer to ofPtr. b. Write a statement that opens file "trans.dat" for writing and...

  • the answer should be in C What is the output of the following: int whatIsThat(int *...

    the answer should be in C What is the output of the following: int whatIsThat(int * x); int main() int hold; int x[] = { 2,4,6,8); hold = whatIsThat(x); printf("%d, %d\n", x[0], hold); return 0; } int whatIsThat(int * a) { int i,sum=0; a[0] = a[O] + 6; for(i = 0; i <= 2; i++) (sum+= a[i]; a[i] = a[i] + 12; return sum; a. 20, 20 b. 20, 18 c. 18, 18 d. none of above What is the...

  • what is output #include<stdio.h> pint main() { int i, j, total; for ( 2 { printf("\n");...

    what is output #include<stdio.h> pint main() { int i, j, total; for ( 2 { printf("\n"); for ( 2 { total = i + j; printf("%d", total); بہا } getchar(); return 0; } Find the for loop content | Output of progrm 5 15 1 11 21 31 41 51 61 2 12 22 32 25 3 13 23 33 43 53 63 4 14 24 34 44 54 64 35 45 6 16 26 36 46 56 66 7...

  • (15 pts) Using the following example, int a, p=&a; *p = 20; Give the two major...

    (15 pts) Using the following example, int a, p=&a; *p = 20; Give the two major differences of * and & in the above example. (35 pts) Do the following in a program segment step by step. The next step uses the results of the previous step and earlier, using a single statement only and none for manual work. Declare an array called a of float of size 10 and initialize it to 2.5, -3.3, 5.9, 1.0, 3.5, 4.3, -7.3,...

  • What is the output of the following? int sum=3, z=1; do { printf("The sum:%d*", sum+z); }...

    What is the output of the following? int sum=3, z=1; do { printf("The sum:%d*", sum+z); } while (sum<= 1); printf("0"); * 1 (2 Points) 40 4

  • c program void funcint x, int *y) { 1. Whof the Rings gical ASA All of...

    c program void funcint x, int *y) { 1. Whof the Rings gical ASA All of the above 1.0.2.4) What will be the out of the de int, pat) A) 10 12 (1.03.2) What will be the output of the following int , printf(d, a,b); A) & B) 17 11.12 D) 17.25 13. (L032) An array is a group of memory locations related by the fact that they all have my name and pe A) different different B) same, different...

  • C Programming What is the output of the following code? int x=5: x * = 3:...

    C Programming What is the output of the following code? int x=5: x * = 3: printf("%d\n", x): x++: ++x: x = x/2: printf("%d\n", x): printf("%d\n", x++): printf("%d\n", ++x);

  • How do you do the commented part of this question (its the part that is bolded)?...

    How do you do the commented part of this question (its the part that is bolded)? #include <stdio.h> #include <string.h> main(){ int *p, in=10; p = &in; printf("%d\n", *p ); char arr[]="hello"; char *ptr = arr; // different ways to pass the array, at "pointer" level. printf("%s %s %s\n", arr, &arr[0], ptr); //different ways to access arr[0] printf("%c %c %c %c\n", arr[0], *ptr, *arr, ptr[0]); //different ways to access arr[1] printf("%c %c %c %c\n", arr[1], *(ptr+1), *(arr+1), ptr[1] ); //different...

  • 22. (6 points) Determine the output for the following coe # include < stdio.h > int...

    22. (6 points) Determine the output for the following coe # include < stdio.h > int main(void) ( int z 1, total -0, y printf(d , x) ) //end-for printf("The total %d\n", total) ) //end-main

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT