Need help with C programming Homework Write a function calle
Need help with C programming Homework!
Write a function called is Palindrome which takes an array of type int as an argument and returns a value of type int. The returned value should be 1 if the array is a palindrome, and 0 otherwise. a palindrome is the same series of forwards and backwards. For eg. 1 2 3 4 and 4 3 2 1 are palindromes. First write a function called are Adjacent which takes one argument of type array of int and another two arguments of type int. The function returns the value 1 if the two numbers occur adjacently in the array: and returns 0 otherwise. Then write a program which prompts the user to enter an array of X integers. Then repeatedly prompts the user to enter two numbers. The program displays a message saying whether the two numbers occur adjacently in the array or not. ALL FUNCTIONS MUST TAKE ATLEST 1 POINTER AS AN ARGUMENT Example: Enter the array elements 12 17 89 90 100 Enter x and y 17 89 Numbers are adjacent! Enter the array elements 12 34 25 67 89 Enter x and y 12 67 Numbers are not adjacent!Solution
Palindrome code:
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system(\"pause\") or input loop */
int main(int argc, char *argv[]) {
int num, reversedNumber = 0, rem, originalNumber;
printf(\"Enter an integer: \");
scanf(\"%d\", &num);
originalNumber = num;
// reversed integer is stored in variable
while( num!=0 )
{
rem = num%10;
reversedNumber = reversedNumber*10 + rem;
num /= 10;
}
// palindrome if orignalInteger and reversedNumber are equal
if (originalNumber == reversedNumber)
printf(\"%d is a palindrome.\", originalNumber);
else
printf(\"%d is not a palindrome.\", originalNumber);
return 0;
}
Finding Adjecency elements:
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system(\"pause\") or input loop */
int size=0;
int areAdjecent(int arr[],int num1,int num2){
int a=0,i;
for(i=0;i<size;i++){
if(arr[i] == num1 && arr[i+1] == num2){
printf(\"%d AND %d are Adjecent!\",num1,num2);
a++;
return 1;
}
if(i==size-1){
printf(\"%d AND %d are NOT Adjecent!\",num1,num2);
return 0;
}
}
}
int main(int argc, char *argv[]) {
int arr[10],num1,num2;
int *ptr,i,j;
printf(\"Enter size of your array:\");
scanf(\"%d\",&size);
printf(\"Enter elements of your array:\");
for(i=0;i<size;i++){
scanf(\"%d\",&arr[i]);
}
printf(\"Enter NUM1 and NUM2:\ \");
scanf(\"%d%d\",&num1,&num2);
printf(\"\\t\\t\");
for(i=0;i<size;i++){
printf(\"%d\\t\",arr[i]);
}
printf(\"\ \");
int result = areAdjecent(arr,num1,num2);
return 0;
}
