Home > OS >  Number of iterations a for loop has made
Number of iterations a for loop has made

Time:10-26

I'm trying to make a program that looks for a number in an array, says the position of this number, and how many times the for loop has been executed. I'm struggling with the part where I have to count the number of times it needs to find that number. Something like this:

int main(){
    
    array[]=5,1,3,6,4,5,7,12,8,9,10;
    int i = 0, number;
    int NumberFound = 0;
    
    printf("Enter the number you are looking for: \n");
    scanf_s("%d", &number);
    for (i = 0; i < 11; i  ) {
        if (number == data[i]) {
            NumberFound = 1;   //Number was found
                        
            printf("%d was found in the position %d\n", number, i);
            break;
        }
        else {
            NumberFound = 0;
        }
    }
    
    if (NumberFound == 0) {
        printf("%d was not found \n", number);
    }
    
    return 0;
}

I would still like to implement the number of times the for loop has been executed. On the console should be this:

Enter the number you are looking for:
3
Iteration 1
Iteration 2
3 was found in the position 2

CodePudding user response:

The iteration count is i 1.

    for (i = 0; i < 11; i  ) {
        if (number == data[i]) {
            printf("%d was found in the position %d\n", number, i);
            break;
        } else {
            printf("Iteration %d\n", i 1);
        }
    }

You don't need to set NumberFound = 0; in the else block, since it already contains that from the initialization.

CodePudding user response:

Print the value of i before breaking the loop as
Barmar suggested.

if (number == data[i]) {
            NumberFound = 1;   //Number was found
                        
            printf("%d was found in the position %d after %d iterations\n", number, I, i 1);
            break;
        }

CodePudding user response:

For starters there are typos in this declaration

array[]=5,1,3,6,4,5,7,12,8,9,10;

It seems you mean

int array[] = { 5,1,3,6,4,5,7,12,8,9,10 };

Also the name data used in this statement

if (number == data[i]) {

is undefined.

This else statement

    else {
        NumberFound = 0;
    }

does not have an effect and may be removed.

Actually the variable NumberFound is redundant. You may remove its declaration and its using

int NumberFound = 0;

The program can look the following way

#include <stdio.h>

int main( void )
{
    int array[] = { 5, 1, 3, 6, 4, 5, 7, 12, 8, 9, 10 };
    const size_t N = sizeof( array ) / sizeof( *array );

    int number = 0;
    
    printf( "Enter the number you are looking for: " );
    scanf( "%d", &number );

    size_t i = 0;

    while ( i < N && number != array[i] ) 
    {
        printf( "Iteration %zu\n",   i );
    }

    if ( i == N )
    {
        printf( "%d was not found \n", number );
    }
    else
    {
        printf( "%d was found in the position %zu\n", number, i);
    }
    
    return 0;
}
  • Related