C print an array with loop 🔃

Поділитися
Вставка
  • Опубліковано 7 лис 2024

КОМЕНТАРІ • 27

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

    #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;
    }

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

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

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

    You’re the best,Thank you 🤍

  • @PSIwolf39
    @PSIwolf39 10 місяців тому +3

    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]);
    }
    }

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

    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 6 місяців тому

      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.

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

    This is what i looking for . Thank you .

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

    Very useful! Thanks Bro!

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

    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 Місяць тому +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.

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

    great teaching.

  • @JoeSwick41
    @JoeSwick41 10 місяців тому

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

  • @davidsabacahan9898
    @davidsabacahan9898 9 місяців тому

    Thanks bro

  • @RindirisiaWangira
    @RindirisiaWangira 11 місяців тому

    Thanks bro!!!

  • @naveenkumar1853
    @naveenkumar1853 11 місяців тому

    😊😊😊

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

    thx

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

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

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

    I'm saying it, I miss JavaScript

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

    ❤❤DONE❤❤

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

    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++'.