C Exercises: Check a given array of integers and return true if the array contains three increasing adjacent numbers
Write a C program to check a given array of integers and return true if the array contains three increasing adjacent numbers.
C Code:
#include <stdio.h>
#include <stdlib.h>
// Function prototype for 'test'
int test(int numbers[], int arr_size);
int main(void){
int arr_size;
// Declaration and initialization of an integer array 'array1'
int array1[] = {1, 2, 3, 5, 3, 7};
arr_size = sizeof(array1)/sizeof(array1[0]);
// Printing the result of the 'test' function for 'array1'
printf("%d",test(array1, arr_size));
// Declaration and initialization of an integer array 'array2'
int array2[] = {3, 7, 5, 5, 3, 7 };
arr_size = sizeof(array2)/sizeof(array2[0]);
// Printing the result of the 'test' function for 'array2'
printf("\n%d",test(array2, arr_size));
// Declaration and initialization of an integer array 'array3'
int array3[] = { 3, 7, 5, 5, 6, 7, 5};
arr_size = sizeof(array3)/sizeof(array3[0]);
// Printing the result of the 'test' function for 'array3'
printf("\n%d",test(array3, arr_size));
}
// Definition of the 'test' function
int test(int numbers[], int arr_size)
{
// Looping through the array until the third-to-last element
for (int i = 0; i <= arr_size - 3; i++)
{
// Checking if consecutive elements are in an increasing sequence
if (numbers[i] == numbers[i + 1] - 1
&& numbers[i] == numbers[i + 2] - 2)
{
return 1; // Return 1 if the condition is met
}
}
return 0; // Return 0 if the condition is not met
}
Sample Output:
1 0 1
Pictorial Presentation:
Flowchart:
C Programming Code Editor:
Previous: Write a C program to check a given array of integers and return true if the specified number of same elements appears at the start and end of the given array.
Next: Write a C program to shift an element in left direction and return a new array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics