if bugando inside the vector for in C

Asked

Viewed 100 times

1

Mine ifs within the for vector are bugging and at the end of the execution of the program showing a completely random number. Follow the code:

//Exercicio_08
#include <conio.h>  
#include <stdio.h>  
#define max 8 
//Codigo  
main(){  
    int i;  
    int a[max];  
    int par;  
    par = 0;  
    //entrada vetor A     
    for(i=1; i<=max; i++){  
        printf("\nDigite o %i elemento do vetor A: ", i);  
        scanf("%i",& a[i]);  
    }  

    //mostra o vetor A  
    printf("\n");  
    for(i=1; i<=max; i++){    
        printf("%i ", a[i]);  
    }  
    for(i=1; i<=max; i++){  
        if(a[i] % 2 == 0){  
        printf("\nPar: %i", & a[i]);  
        par = par + 1;      
        }  
    }  
printf("\nQuantidade de pares: %i", &par);          
getch();  
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

  • Be careful because in c the vectors start at 0 Note that from (1 to x being x <= 8 )has the same range as (0 to x being x < 8)

1 answer

4

Do not use conio, this is obsolete only available in compilers considered bad today.

You can use %d to deal with whole which is more common to do not accept hexadecimals.

The main problem that even prevents compiling is that you are using O operator & in the printf(). This operator takes the address of the data, and is trying to print the data, which gives conflict. He probably thought he should do the same as in scanf(). There it’s different. You don’t pass the data to the function, you pass the address wherever the data is stored.

As said by Leandro Godoy Rosa, the array should start at 0 and go up to your size minus 1. It may seem silly and works in some cases going from 1 to Len. But you’re taking something you shouldn’t, violating the memory.

I improved the code.

#include <stdio.h>
#define max 10

int main(void) {
    int a[max];
    for (int i = 0; i < max; i++) {
        printf("\nDigite o %i elemento do vetor A: ", i);
        scanf("%d", &a[i]);
    }
    printf("\n");
    for (int i = 0; i < max; i++) {
        printf("%d ", a[i]);
    }
    int par = 0;
    for (int i = 0; i < max; i++) {
        if (a[i] % 2 == 0) {
            printf("\nPar: %d", a[i]);
            par++;  
        }
    }
    printf("\nQuantidade de pares: %d", par);       
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thank you guy for helping me, it ran perfectly, here when he found even numbers he showed a totally random number and I did not find the persistent error in the uhdsauhda code, only exercise that I appeared a mistake like this.

  • Further information on this depreciation: https://stackoverflow.com/questions/814975/getch-is-deprecated

  • What reason to use %d instead of %i and the two have exactly the same behavior?

  • 1

    @Jeffersonquesado ready, you said.

  • @Leandrogodoyrosa I was already half asleep, nor was it that I was going to answer, but I wanted bed :D I explained why, actually I forgot why it was the same mistake, here it is now.

  • 1

    Okay, just one other thing, when accessing the vector a the index should not be 0 until max - 1? Are you accessing from 1 until max

  • @Leandrogodoyrosa tidied up. Thank you.

Show 2 more comments

Browser other questions tagged

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