In: Computer Science
C program
1.// rotate the values pointed to by three pointers// so values move from xp to yp, yp to zp and zp to xpvoid rotate(int *xp, int *yp, int *zp) { return;}
2.// Write a function that returns 0 if x is 0, returns -1// if x < 0, returns 1 if x > 0// Your code must follow the Bit-Level Integer Coding Rules// on the textbook (located between hw 2.60 and 2.61).// You can assume w = 32.// The only allowed operations in your code are:// ! ~ & ^ | + << >>// This requirement is more restrictive than the coding rules.int sign(int x) { return 0;}
3.// Given a source string, and a destination string, write the// reversed source string into destination. Make sure you terminate// the reversed string properly with an integer value 0.// You may assume that the destination array is big enough to hold// the reversed string.void reverse(const char source[], char destination[]) { return;}
CODE IN C:
#include <stdio.h>
#include<string.h>
void rotate(int *xp, int *yp, int *zp){
int temp = *zp;
*zp = *yp;
*yp = *xp;
*xp = temp;
}
int sign(int x){
int val = 1 + (x >> 31) - (-x >> 31);
switch(val){
case 0:
return -1;
case 1:
return 0;
case 2:
return 1;
}
}
void reverse(const char source[],char destination[]){
int length = strlen(source);
int i,j=0;
for(i=length-1;i>=0;i--){
*(destination+j) = source[i];
j++;
}
}
int main()
{
int xp=10,yp=20,zp=30;
printf("Before calling the rotate method:\n");
printf("xp=%d, yp=%d, zp=%d",xp,yp,zp);
rotate(&xp,&yp,&zp);
printf("\nAfter calling the rotate method:\n");
printf("xp=%d, yp=%d, zp=%d",xp,yp,zp);
int num;
printf("\nEnter a number:");
scanf("%d",&num);
switch(sign(num)){
case -1:
printf("You entered negaative number...");
break;
case 0:
printf("You entered zero...");
break;
case 1:
printf("You entered positive number...");
break;
}
int n;
printf("\nEnter the length of a string:");
scanf("%d",&n);
char source[n],destination[n];
printf("Enter the source String:");
scanf("%s",source);
reverse(source,destination);
puts(destination);
return 0;
}
OUTPUT: