Delete All Array Elements That Match A User Input Value | C Programming Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ม.ค. 2025

ความคิดเห็น •

  • @sethleifheit11
    @sethleifheit11 2 ปีที่แล้ว +1

    Hello PLEASE HELP! I am trying to clear an array and I am still confused. If I just reduce the size variable, It doesn't seem like that actually deletes the data stored in the array. It would just reduce the amount of elements that are printed. Am I wrong to assume this??

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      Great question Seth! 🙂 And no you're not wrong to assume that, that's correct. We can only truly re-size an array if we've used dynamic memory allocation. Otherwise all we can do is use a size variable to keep track of the size of the array (and perhaps "delete" unused elements by setting them to 0 or something else).

  • @manmod8116
    @manmod8116 2 ปีที่แล้ว

    this video helps me a lot, thank you very much !!!

  • @tsad4337
    @tsad4337 2 ปีที่แล้ว

    How could we delete all array elements that much a user input value from a 2D array and in their place put the whitespace character in order to maintain the initial form of the array??

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      You would need to have a loop nested in a loop, with the counter variable for one loop going over all row index and the counter variable for the other loop going over all column indexes. Then you could use an if statement inside the inner loop to check for the matching value at the row/column index given by the counter variables, and if a match is found, replace it with the whitespace character. :-)

    • @tsad4337
      @tsad4337 2 ปีที่แล้ว

      @@PortfolioCourses Do I have to use ASCII for this or something, because I have to replace an integer value with a character?

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      @@tsad4337 Yes, you could replace the int in the array with the integer value that represents the whitespace character (space, tab and newline are all whitespace characters).

    • @tsad4337
      @tsad4337 2 ปีที่แล้ว

      @@PortfolioCourses Thank you!!

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      @@tsad4337 You're welcome! 🙂

  • @mongraal2272
    @mongraal2272 3 ปีที่แล้ว +3

    Thaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanks!!!!!!!

  • @younes-yu1fy
    @younes-yu1fy 3 ปีที่แล้ว +3

    happy new year for everyone🥰

  • @najdadgc02
    @najdadgc02 2 ปีที่แล้ว

    can this also be used for deleting odd/even numbers in an array?

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +1

      Yes, the condition that's checked for would need to change, but the same general idea should work.

    • @TechieMohit
      @TechieMohit 2 ปีที่แล้ว

      @@PortfolioCourses can you please make a video on that

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      @@TechieMohit I will add it to my list of ideas, but the code for this video is posted here: github.com/portfoliocourses/c-example-code/blob/main/delete_elements_user_input.c. And right here:
      if (buffer[i] == delete)
      is where you would want to check if the number is even or odd, instead of checking if it matches delete.
      if (bufffer[i] % 2 == 0)
      would check if the number is even.
      if (buffer[i] % 2 == 1)
      would check if the number is odd. And that should do it. 🙂 This video explains checking for even and odd numbers in more detail: th-cam.com/video/6Zd69FQpc3A/w-d-xo.html.

  • @zaidabbassi6598
    @zaidabbassi6598 2 ปีที่แล้ว

    hello thanks in the first place but could you do a program where you give user the right to fill the array then he choses a number to delete it

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +1

      Good question Zaid, yes you could do that! :-) This video goes over how to fill an array with user input: th-cam.com/video/5nyMb7hJ7Xs/w-d-xo.html. You could then ask the user for the number using scanf: th-cam.com/video/jr0hX1iMxrE/w-d-xo.html. And then from there the code in this video could be used to delete that number.

    • @zaidabbassi6598
      @zaidabbassi6598 2 ปีที่แล้ว

      @@PortfolioCourses in that case all the repetitions will be delted

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +1

      @@zaidabbassi6598 Yes, if you only want to delete one element I suppose you could just stop after that first matching element that is found (if one is found).

    • @zaidabbassi6598
      @zaidabbassi6598 2 ปีที่แล้ว

      @@PortfolioCourses thanks that was really helpful

  • @dinakh9990
    @dinakh9990 2 ปีที่แล้ว

    How can we delete only the first occurence

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +1

      You could put in a break statement to stop after the first occurrence is found:
      while (i < size)
      {
      if (buffer[i] == delete)
      {
      if (i < (size - 1))
      for (int j = i; j < (size - 1); j++)
      buffer[j] = buffer[j + 1];
      size--;
      break;
      }
      else i++;
      }

    • @dinakh9990
      @dinakh9990 2 ปีที่แล้ว

      @@PortfolioCourses thank you

  • @vino9587
    @vino9587 2 ปีที่แล้ว

    Thanks sir

  • @ryanalnaser914
    @ryanalnaser914 2 ปีที่แล้ว

    whatever anything thanks whatever anything I do or did or anything