Write the function boolean five(int[] a), which takes in N-length integer array
ID: 3591823 • Letter: W
Question
Write the function boolean five(int[] a), which takes in N-length integer array a and returns true if 5 is not in the array, also if there is a 5 next to each other it returns true. If 5 is in the array it returns false. For example the function will returns true for the array {1 2 3 4} and {1 5 5 3 4} but false for the array {1 2 3 4 5} because there is alone 5.
Also write a main function that reads in from command line N integers, and call the function five and returns "alone five" when there is a 5 and "there is no alone five" where there is no alone five.
Explanation / Answer
#include "stdio.h"
#include "unistd.h"
#include "stdbool.h"
int num;
bool five(int arr[]){
int count =0;
for(int i=0; i<num;i++){
if(arr[i] ==5)
count = count+1;
}
if(count == 1)
return false;
else
return true;
}
int main(void) {
int array[num];
printf("Enter length of the array : ");
scanf("%d",&num);
for(int i=0; i<num ;i++){
scanf("%d",&array[i]);
}
if(five(array))
printf("there is no alone five");
else
printf("alone five");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.