C print an array with loop 🔃

Поділитися
Вставка
  • Опубліковано 30 січ 2025

КОМЕНТАРІ • 32

  • @BroCodez
    @BroCodez  3 роки тому +29

    #include
    int main()
    {
    double prices[] = {5.0, 10.0, 15.0, 25.0, 20.0, 30.0};

    //printf("%d bytes
    ", sizeof(prices));
    for(int i = 0; i < sizeof(prices)/sizeof(prices[0]); i++)
    {
    printf("$%.2lf
    ", prices[i]);
    }
    return 0;
    }

    • @nixnachname7078
      @nixnachname7078 10 місяців тому +1

      thanks you are the best :)

    • @Konrado28
      @Konrado28 Місяць тому

      Isn't better actually strlen(prices) ?
      Instead of sizeof

  • @kazinabil_
    @kazinabil_ 2 місяці тому +7

    the for(int i = 0; i < sizeof(prices)/sizeof(prices[0]); i++) part is absolutely genius

  • @harukasetsuna5154
    @harukasetsuna5154 3 роки тому +3

    Thanks for tutorial. Please up more C tutorial. I already learn Java from your video but C make it more mindful.

  • @ajwan4531
    @ajwan4531 2 роки тому +2

    You’re the best,Thank you 🤍

  • @masterofboarding
    @masterofboarding 10 місяців тому +4

    Hi,
    whats the difference between your for loop and this?
    for(int i = 0; i < prices[ i ]; i++)
    {
    printf("%.2lf
    ", prices[ i ]);
    }
    it still prints all the values in an array.

    • @Aymanne-io6vb
      @Aymanne-io6vb 9 місяців тому +3

      So here's how your for loop works:
      Here is the array : prices[ ] = { 50, 60 , 50 , 30, 60 };
      After the first iteration, the value of *price[ i ]* is *greater than *i* , so the condition is true and the loop continues, however if you print the value of price[ 5 ] , you get zero, because it has no value assigned to it in the initial array. So what happens? The loop exits(condition becomes false), since now the *i* variable is *greater* than *prices[ ]*.
      Now that we understand this , we can see why it's different from Bro code's code:
      sample Array 2 : prices[ ] = { 50, 60 , 50 , 30, 60, -67 };
      The code will not print -67, because the loop will exit since -67 is less than 4, which is the value of *i* when it iterates for the fourth time before reaching -67. So your code will never print 0 or negative numbers. if the first element was negative the loop would exit before starting since the i < prices[ i ] condition would be false.
      TLDR; The loop would exit when an element in negative or zero, since i < prices[ i ] would be false.
      Also from chatGPT( Use it , use whatever resource you have for learning please) :
      It's worth noting that the behavior of this loop depends on the values stored in the prices array. If the values in the prices array are not arranged in a way that makes the loop condition false at some point, the loop may continue indefinitely, leading to an infinite loop. Therefore, it's important to ensure that the loop condition is appropriate for the specific task.

  • @PSIwolf39
    @PSIwolf39 Рік тому +6

    Here's the code I made with this:
    #include
    #include
    #include
    #include
    #include
    int main(){
    int answers[] = {12,23,43,22,66,45,65,777,4985,2,487,59};
    for(int i = 0; i < sizeof(answers)/sizeof(answers[0]); i++){
    printf("The answer for question #%d is %d
    ", i + 1,answers[i]);
    }
    }

  • @Inequalito
    @Inequalito Рік тому +4

    Very useful! Thanks Bro!

  • @lemonmelon1954
    @lemonmelon1954 2 роки тому +7

    This is what i looking for . Thank you .

  • @umutaydn6184
    @umutaydn6184 Рік тому +1

    great teaching.

  • @JoeSwick41
    @JoeSwick41 Рік тому +1

    You are the Ultimate Bro!. thank you for your videos

  • @MainulHossainAnik
    @MainulHossainAnik 4 місяці тому +1

    ❤❤DONE❤❤

  • @AravindA-q8t
    @AravindA-q8t 8 місяців тому

    Why would we need to divide the overall Array Size by the size of the first array element? Significance in that?

    • @OuTLaW-j9g
      @OuTLaW-j9g 4 місяці тому +1

      I am 4 months late. I hope you found your answer by now but if you didn't, it's to calculate the length of the array.

  • @naveenkumar1853
    @naveenkumar1853 Рік тому

    😊😊😊

  • @RindirisiaWangira
    @RindirisiaWangira Рік тому +1

    Thanks bro!!!

  • @davidsabacahan9898
    @davidsabacahan9898 Рік тому +1

    Thanks bro

  • @asondubuisi5807
    @asondubuisi5807 2 роки тому +5

    Why didn't you use len of array instead of sizeof?

    • @Baka-mx4yc
      @Baka-mx4yc 2 роки тому +11

      Because size matters

    • @krekx3980
      @krekx3980 2 роки тому +3

      @@Baka-mx4yc 💀💀💀

    • @zinumw
      @zinumw Рік тому +4

      @@Baka-mx4yc

  • @darkseansamm6653
    @darkseansamm6653 Рік тому +2

    You didn't explain why you used i. I don't understand. I'm trying it, it's not working.

    • @louiszanedejesus9057
      @louiszanedejesus9057 Рік тому +3

      indicator of the index

    • @SCRKT007
      @SCRKT007 Рік тому +5

      "i" is just a variable name, you can call it whatever you want.
      It pretty much reads as: "do this loop while variable called 'i' is still less than the condition (in this case the condition is the amount of stuff inside the prices) - and while doing so, keep adding 1 to 'i's value after every time."
      The 'for loop' will keep on going until "i" is no longer less than the amount inside the prices, it starts at 0 and then increases by 1 everytime because of 'i++'.

    • @Konrado28
      @Konrado28 Місяць тому

      This is a counter to do this loop so many times the counter goes to the certain point - our condition

  • @MeowKiritoMeow
    @MeowKiritoMeow Рік тому +8

    I'm saying it, I miss JavaScript

  • @mohammedabdullhafeed4602
    @mohammedabdullhafeed4602 Рік тому

    thx