AD1

Saturday, 27 May 2017

Reverse Two Arrays Of The Same Size

write a C program to reverse two array elements of the same size 
as example if we have int a[]={1,2,3,4},int b[]={5,6,7,8}
after reversing we would have int a[]={5,6,7,8},int b[]={1,2,3,4}

#include <stdio.h>
#include <stdlib.h>
#define size 4
void swap(int *x1,int *x2)
{
int temp=*x1;
*x1=*x2;
*x2=temp;
}
int main(int argc, char** argv) {
int a[size]={1, 2, 3, 4};
int b[size]={5, 6, 7, 8};
for(int j=0;j<size;j++)
{
swap(&a[j],&b[j]);
}
printf("after reversing\n");
for(int i=0;i<size;i++)
{
printf("first array element %d\t%d\n",i,a[i]);
printf("second array element %d\t%d\n",i,b[i]);
}
return (EXIT_SUCCESS);
}
view raw revarray.c hosted with ❤ by GitHub

Monday, 1 May 2017

Find the output of the following code


#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
unsigned int i=257;
int *iptr=&i;
for(int j=0;j<=3;j++)
{
printf("%d\t",*((char*)iptr+j));
}
return (EXIT_SUCCESS);
}
view raw q148.c hosted with ❤ by GitHub
Output 

1    1    0    0

Explanation