Add an element that is not in the array

Asked

Viewed 848 times

1

I’m new to C, I’ve been trying to find a way to do this program but always returns a result that I can’t understand when printing the array for visualization. Example of entered values: 1,1,2,3,4,4,5,6 (99 to stop the program) I would like this to be the output: 1,2,3,4,5,6 But this returns: -1,6,40,0,4203721,0,3,0 Is there any simpler way to do this?

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

int main() 
{
  int valor,vet[10],i;
  while(1)
  {
    printf("valor:");
    scanf("%d",&valor);
    if(valor==99)
    {
      break;
    }
    int c=0;
    for(i=0;i<10;i++)
    {

      if(vet[1]==valor)
      {
        c++;
      }
    }
    int cc=0;
    if(c==0)
    {
        for(i=0;i<10;i++)
        {
            cc++;
        }
        vet[cc] = valor;
    }

    }



  for (i=0; i < 10; i++)
  {
    printf("%d ", vet[i]);
  }
  return 0;
}
  • 1

    There are several things there that don’t make sense. Do you want to do what exactly ? Go adding elements at the end of the array ? If it is that it is very simple, and it manages to do it only in an instruction: vet[ultima_posicao++] = valor;. Since you need to initialize this last position to 0 at the beginning of the program. Naturally you also have to test if you have already entered more than the available quantity, otherwise you will have indefinite behavior

  • Yes, but the program should not add repeated elements. I will try to use this form you taught to add

  • 1

    You need to control the current insertion position in the vector and test if the read value already exists among these positions. The way you did it you try to test all the numbers in the array by entering the first number. I also think you changed the i for 1. Focé loops to increment the variable cc that will always be worth 10at the end of the loop, which is an invalid index for the array.

  • I understood friend, thank you very much. I will try to study more, is that I am on time to deliver a project

1 answer

3


Let’s start with what’s not right.

Here you got it wrong and used 1 instead of i:

if(vet[1]==valor)
{
    c++;
}

Further down did a for that is not useful:

int cc=0;
if(c==0)
{
    for(i=0;i<10;i++)
    {
        cc++;
    }
    vet[cc] = valor;
}

Note that you are always counting the same amount of elements in for, which are 10. Regardless of the values that are there and add the element in an invalid position, outside the array. Instead you want to add from the top, and increase one unit each time you insert.

Let’s correct these two points I mentioned, keeping the code as close to what I had:

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

int main() {
    int valor,vet[10],i, ultima_posicao = 0; //variavel para ultima posicao começa a 0
    while(1) {
        printf("valor:");
        scanf("%d",&valor);
        if(valor==99) {
            break;
        }
        int c=0;
        for(i=0; i<10; i++) {
            if(vet[i]==valor) {
                c++;
            }
        }

        if(c==0) {
            vet[ultima_posicao++] = valor; //apenas isto para inserir o valor
        }
    }

    for (i=0; i < ultima_posicao; i++) { //mostra só até os que inseriu
        printf("%d ", vet[i]);
    }
    return 0;
}

The instruction vet[ultima_posicao++] = valor; allows you to put the value in the right position, and increases it so that in the next insertion it is also in the correct location.

Personally I think it is much more organized, if you create a function to check if the value exists in the array. And it makes it easier even at the logical level, since it doesn’t even have to count, and it can do return straightforward:

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

int existe(int vet[10], int valor) {
    int i;
    for(i=0; i<10; i++) {
        if(vet[i]==valor) {
            return 1;
        }
    }
    return 0;
}

int main() {
    int valor,vet[10],i, ultima_posicao = 0;
    while(1) {
        printf("valor:");
        scanf("%d",&valor);
        if(valor==99) {
            break;
        }
        if(!existe(vet, valor)) {
            vet[ultima_posicao++] = valor;
        }
    }

    for (i=0; i < ultima_posicao; i++) {
        printf("%d ", vet[i]);
    }
    return 0;
}

Check it out at Ideone

I didn’t put it here in the answer, but it is useful to also prevent the user from entering more values than the size of the array. You can do this by testing if the variable ultima_posicao has a value of 10 or more, in which case the program ends.

  • Solved my problem, thank you very much. I will train more programming arrays, has always been my weakness

  • @No problem, we are here to help. Good training :)

Browser other questions tagged

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