Ubuntu-Elephant
Ubuntu-Elephant

Reputation: 1

Why is this float array not printed properly?

I've tested various ways to fix this error and couldn't find a solution. Added a comment where It is supposed to be finding out all numbers who are outside of the (4.6, 9.7) loop. Could there be a mistake in the syntax?

int main()
{
    int i,n;

    float pom=0;
    printf("vnesi broj na elementi na nizata\n");

    scanf("%d",&n);
        float arr1[n], arr2[n];
    printf("vnesi elementi \n");
    for(i=0;i<n;i++)
    {
        scanf("%f",&arr1[i]);

    }

    pom =((arr1[0]+arr1[n-1])/2);

    int k = 0;

    for(i=0;i<n;i++)
    {

        if(arr1[i]<=4.6 && arr1[i]>=9.7) // the problem lies here
        {
            arr2[k]=arr1[i];
            k++;
            printf("%f", arr1[i]);
        }
    }

    printf("\n elementi od prva niza: \n");
    for(i=0; i<n; i++)
    {
        printf("%.2f ", arr1[i]);
    }

    printf("\n\n elementi od vtora niza: \n");


    for(i=0; i<k; i++)
    {
        printf("%f", arr2[i]); // is not printing the array

    }

}

Upvotes: 0

Views: 61

Answers (1)

Ardent Coder
Ardent Coder

Reputation: 3995

if(arr1[i]<=4.6 && arr1[i]>=9.7) // the problem lies here

Exactly. How can a number be both less than or equal to 4.6 and greater than or equal to 9.7?! It could be either less than or equal to 4.6 or greater than or equal to 9.7. So your condition is always false.

Solution:

Replace that && with ||

Upvotes: 4

Related Questions