I'm new to programming and I work on this program.
The requirement is: Write a C program to check a given array (length will be at least 2) of integers and return true if there are two values 15, 15 next to each other.
Although my condition is verified, the function is not returning true unless I exclude the "else". But if I exclude else, it is not returning 0 when false, but any other random number. I don't understand why and how to fix it.
#include <stdio.h>
#include <stdbool.h>
int check_fifteen(int a[], int size);
int main(){
int a[]={1, 6, 15, 15, 3, 5, 15};
int size= sizeof(a) / sizeof(a[0]);
printf( "%d", check_fifteen(a, size));
}
int check_fifteen(int a[], int size){
int i=0;
if(size < 2){
return 0;
}
for(i=0; i<size-1; i++){
if( ( a[i] == 15 ) && ( a[i+1] == 15 ) ){
return true;
}
else
return false;
}
}
Even if one pair doesn't satisfy if condition
you are returning false
. That shouldn't happen, you should check for all pairs and then return false if none satisfies.
if( ( a[i] == 15 ) && ( a[i+1] == 15 ) ){
return true;
}
else
return false;
}
To
for(i=0; i<size-1; i++){
if( ( a[i] == 15 ) && ( a[i+1] == 15 ) ){
return true; // if any pair satisfies you return true there itself
}
}
// you will reach here only if every pair doesn't satisfy the requirement.
return false;
The loop is interrupted as soon as there will be encountered two consecutive elements of the array that either equal each other and to 15
or are not equal.
for(i=0; i<size-1; i++){
if( ( a[i] == 15 ) && ( a[i+1] == 15 ) ){
return true;
}
else
return false;
}
At least place the statement return false
outside the loop.
for(i=0; i<size-1; i++){
if( ( a[i] == 15 ) && ( a[i+1] == 15 ) ){
return true;
}
}
return false;
It would be better to declare and define the function the following way
int check_fifteen( const int a[], size_t size )
{
const int Target = 15;
int success = 0;
for ( size_t i = 1; !success && i < size; i++ )
{
if ( a[i] == Target ) success = a[i-1] == Target;
}
return success;
}
and the variable size
declare like
size_t size = sizeof(a) / sizeof(a[0]);
Also there is no great sense to write a function that checks only that two adjacent elements are equal to 15. You could write a more general function.
Here you are.
#include <stdio.h>
int check_equal_adjacent( const int a[], size_t size, int value )
{
int success = 0;
for ( size_t i = 1; !success && i < size; i++ )
{
if ( a[i] == value ) success = a[i-1] == value;
}
return success;
}
int main(void)
{
int a[] = { 1, 6, 15, 15, 3, 5, 15 };
size_t size = sizeof( a ) / sizeof( *a );
printf( "%s\n", check_equal_adjacent( a, size, 15 ) ? "true" : "false" );
return 0;
}
The program output is
true
The problem is that the function check_fifteen
already returns false
at the first iteration because since the if
condition isn't satisfied (the first two elements of the pointed array doesn't contain the value 15
), inside of the else
statement you immediately return false
.
for (i = 0; i < size-1; i++){
if ( ( a[i] == 15 ) && ( a[i+1] == 15 ) ){
return true;
}
else
return false; // You tell: If the 'if' condition is not matched, return `false`.
}
Place the return false;
after the loop, so the else
statement won't make the function return already at the first iteration when if ( a[i] == 15 && a[i+1] == 15 )
is not true.
for (i = 0; i < size-1; i++){
if ( a[i] == 15 && a[i+1] == 15 ) {
return true;
}
}
return false;
This type of bug is easily avoided if you follow the rule (guideline, stylistic preference) that each function should have only one return point. Just maintain a flag which is set when you see the validating condition. Something like:
int
check_fifteen(const int *a, int size)
{
int status = 0;
const int *b = a + size;
while( a + 1 < b && !status ) {
status = a[0] == 15 && a[1] == 15;
a += 1;
}
return status;
}
Note that you can reduce the work a bit with a simple "optimization" (bearing in mind that premature optimization is the root of all evil):
int
check_fifteen(const int *a, int size)
{
int status = 0;
const int *b = a + size;
while( a + 1 < b && !status ) {
if( a[1] == 15 ) {
status = *a++ == 15;
} else {
a += 2;
}
}
return status;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.