Question

​what is the output?Consider the following program: #include <stdio.h #include stdlib.h> #define size 3 void func (int **a) int tmp; for (int i 0; i size; i++) f for (int j i; j k size, j++) (a+i)+j); tmp (a+i)+j) (a +j)+i); int main() int arr malloc (sizeof (int*) size) for (int i 0; i size; i++) arr [i] malloc(sizeof (int) size); for (int j 0; j k size, j++) arr [i][j] 2 i 3 j; func (arr); printf(%d I %d I %d I %d An *(arr[1]+2), arr+2)+0), (arr [2]+2) arr+2)) return 0;

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

#include <stdio.h>
#include <stdlib.h>

#define size 3


void func(int **a)
{
   int tmp;
   for(int i=0;i<size;i++)
   {
       for(int j=0;j<size;j++)
       {
           tmp = *(*(a+i)+j);        //swapping rows and columns using temp
           *(*(a+i)+j) = *(*(a+j)+i);
           *(*(a+j)+i) = tmp;
          
       }
   }
  
}

int main(void)
{
  
   int **arr = malloc(sizeof(int*)*size); // allocate memory for 3(size) pointers to pointers(**arr)
   for(int i=0;i<size;i++)
   {
       arr[i] = malloc(sizeof(int)*size); // allocate memory for 3 rows
       for(int j=0;j<size;j++)
       {
          arr[i][j] = 2*i + 3*j;
          //a[0][0] = 0 a[0][1] = 3 a[0][2] = 6
          //a[1][0] = 2 a[1][1] = 5 a[1][2] = 8
          //a[2][0] = 4 a[2][1] = 5 a[2][2] = 10
       }
   }
       //func(arr);
      
       printf("%d | %d | %d | %d \n",*(arr[1]+2),*(*(arr+2)+0),*(arr[2]+2),*(*arr+2));
  
      //*(*(arr[1]+2) = 8 *(*(arr+2)+0) = 4 *(arr[2]+2) = 10 *(*arr+2)= 6
   return 0;
}


Output: Explaination in comments

8  | 4 | 10 | 6 
Add a comment
Know the answer?
Add Answer to:
​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void...
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
  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • 1. Select all that are true for the following code. #include <stdio.h> #include <stdlib.h> void range(int...

    1. Select all that are true for the following code. #include <stdio.h> #include <stdlib.h> void range(int start, int end, int *results) { int length = end - start; results = malloc(sizeof(int) * length); for (int i=start; i<end; i++) { *results = i; results++; } } void printArray(int *arr, int length) { for (int i=0; i<length; i++) { printf("%d ", arr[i]); } } int main() { int *x; int s = 2; int e = 11; range(s, e, x); printArray(x, e...

  • computers. 1. How many times will this loop repeat? include <stdlib.h> #include <stdio.h> void main(void) int...

    computers. 1. How many times will this loop repeat? include <stdlib.h> #include <stdio.h> void main(void) int 10 for(= 0; i < 101) printf("d", 1); A. 10 times B. 1 time C. It will run forever D. 0 times 2. What will be the output of this program? #include <stdlib.h> #include <stdio.h> int main() int a = 100, b = 200, C = 300; if(!a >= 500) b = 300; C = 400; printf("%d, 8d, &d", a, b, c); return 0;...

  • Debug to print output "2 3 5 6 8 11": #include <stdio.h> #include <stdlib.h> int main()...

    Debug to print output "2 3 5 6 8 11": #include <stdio.h> #include <stdlib.h> int main() {    int i, j, n; int A[] = {2, 3, 5, 5, 5, 6, 8, 11, 11, 11};    n = sizeof(A) / sizeof(int);    for (i = 0; i < n - 1; i++){        if (A[i] != A[i + 1]) {            for (j = 1; j < n - 1; j++) {                A[j]...

  • please help, no explanation just need answer #include <stdio.h> #include <stdlib.h> int main(void) { int -x5);...

    please help, no explanation just need answer #include <stdio.h> #include <stdlib.h> int main(void) { int -x5); for (int = 0; i < 5; i++) { x[i] = malloc(sizeof (int) - 5); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { x[i][j] = i j ; modify(x, 5, 5); return 0; Which of the implementations of method modify below set all elements of the matrix x to zero? 1. void...

  • Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h>...

    Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h> struct node_int; typedef struct node int *node; struct node_int void *data; node next; typedef struct list_int (node first;} *list; void main(int argc, char *argv[]) list shopping, t; node n; int x=25; shopping=(list) malloc(sizeof(struct list_int)); n= (node) malloc(sizeof(struct node_int)); n->data=shopping; n->next=NULL; shopping->first=n; t=(list) (shopping->first->data); // ***** t->first=NULL; // ***** n->data=&x; printf("%d\n", *((int *) (shopping->first->data))); a What will be displayed on the screen if the above...

  • How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include...

    How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include void program_one (); void program_two (); void program_three (); void program_four(); void program_five(); int main() { int menu_option = 0; while (menu_option != 9) { printf(" = 1\n"); //Change this to your first program name. Nothing else. printf(" = 2\n"); //Change this to your second program name. Nothing else. printf(" = 3\n"); //Change this to your third program name. Nothing else. printf(" =...

  • If void * is a pointer to void is a "generic" pointer type, and a void...

    If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void...

    #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void insertionSort(int arr[], int n); void merge(int a[], int l1, int h1, int h2); void mergeSort(int a[], int l, int h) { int i, len=(h-l+1); //Using insertion sort for small sized array if (len<=5) { insertionSort(a+l, len); return; } pid_t lpid,rpid; lpid = fork(); if(lpid<0) { //Lchild proc not created perror("Left Child Proc. not created\n"); _exit(-1); } else if (lpid==0) { mergeSort(a,l,l+len/2-1); _exit(0); } else...

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