Indication of the first negative number of a vector

Asked

Viewed 908 times

1

I have to make a program in C that saves the Inudice from the first negative number, but I’m not able to do it. The question is this down below:

Prepare a program in C that enters 5 integers in an A vector, and print the index of the first negative number, if any. If no negative numbers exist, print the following message "Negative not found".

I tried several times but none worked, can you help me? The code is like this:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

main(){

bool b = true;
int a[5],i, neg[5],n;

for (i=0;i<5;i++) {
printf ("Digite o numero inteiro para o vetor A[%d]: ",i);
scanf ("%d", &a[i]);
    for (n=0;n<5;n++){
        if (a[i]<0 == b){
            neg[n]=i;
        }
    }
}

if (neg[0]<0){

    printf ("o indice do primeiro numero negativo e A[%d]", neg[0]);
}
else
    printf ("Nao tem numero negativo. ");

getchar ();
}

I have to show the first negative number only, but if the first negative number is in A[4], for example, it does not work. It only works if the negative number appears in a[0]

  • 1

    edit the issue and add the code you’re trying to...

  • edited the question and put the code to see if you can help me

2 answers

2

It is a very simple task, it is not necessary to use a boolean variable for this. Simply fill in the vector and read it again.

Try to find and organize your code more and use correct programming practices. Do not use the main() without a method, we usually declare as int main() or if you want arguments use int main(int argc, char const *argv[]). Always use a return to the main, we usually use the return 0 to indicate success and the return 1 to indicate a fault.

The use of fflush(stdin) was to clear the input buffer to prevent the getchar() "sugue" the last value entered by the user and pass blank, without pausing the execution.

I reworked your code, take a look:

#include <stdio.h>

int main()
{
    int i;
    int A[5];

    for(i = 0; i < 5; i++)
    {
        printf("Digite o numero inteiro para o vetor A[%d]: ", i);
        scanf("%d", &A[i]);
        fflush(stdin);
    }

    for(i = 0; i < 5; i++)
    {
        if(A[i] < 0)
        {
            printf("O indice do primeiro numero negativo e: A[%d]", A[i]);
            getchar();
            return 0;
        }
    }

    printf("Nao tem numero negativo.");
    getchar();
    return 0;
}

As you can see, the first negative number he finds will display and exit the program. If it does not find any negative number it will continue running and will display the final message.

Simply a comparison to see if the number is less than 0.

But if you still want to use the boolean method:

#include <stdio.h>
#include <stdbool.h>

int main()
{
    int i;
    int A[5], N;
    bool isNegative = false;

    for(i = 0; i < 5; i++)
    {
        printf("Digite o numero inteiro para o vetor A[%d]: ", i);
        scanf("%d", &A[i]);
        fflush(stdin);
    }

    for(i = 0; i < 5; i++)
    {
        if(A[i] < 0)
        {
            N = A[i];
            isNegative = true;
            break;
        }
    }

    if(isNegative == true)
    {
        printf("O indice do primeiro numero negativo e: A[%d]", N);
    }
    else
    {
        printf("Nao tem numero negativo.");
    }
    getchar();
    return 0;
}

With the boolean method a variable was declared isNegative that initially receives the value false and if a negative number is detected it turns into true, the whole N takes the value of the negative index and breaks the execution loop with the break because it will not be necessary to go through the rest of the vector.

After that it checks whether the variable isNegative is true or false and gives the answer to the user based on this.

I hope it helped.

-1

Friend, from what I understand we go there. You put five number in a vector and then check if there are negatives?

Skipping the part of putting values in the vector, look if this helps you.

for(counter = 0; counter < sizeof(vector); counter++) { if(vector[counter] < 0){ printf("Negative number", counter); } }

...in this case it will show you, but it can be better by placing an outside counter inside the if where each time the if is true you count how many negative numbers there are.

  • sizeof(vetor) does not specify the number of vector elements.

  • So, I have to show only the Index of the first negative number, but I can’t do it with a boolean variaveel.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.