Sum the Values in an Array | C Programming Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 พ.ย. 2024

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

  • @armanmazka
    @armanmazka 27 วันที่ผ่านมา

    These series of videos are one of the clearest, straight to the point tutorials I have ever seen for grasping the basics of C and programming.
    If you are doing CS50 or any computer science course, watch this first, you'll thank me later (Week 3 Psets, I see you).
    Kevin, you are an amazing teacher. Thank you for sharing your passion with us!

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

    Thank you for the example for my final

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

      You're welcome Ed! Good luck on your final. :-)

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

    I’m not going to fail thanks to this

  • @apoledev
    @apoledev 2 หลายเดือนก่อน +1

    int SumOfArray(const int* array, int arraySize) {
    int sum = 0;
    while (arraySize > 0) {
    arraySize --;
    sum += array[arraySize];
    }
    return sum;
    }

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

    Hello, i try to understand logic of array notation. My question is abput 2D array.
    We had 2D array(or 2D set) . And we want to sum each row. When we do this, we have attained 1D array(or 1D set).
    Coders use
    rowTotal +=nums[i] [j]
    in notation in the "for loop".
    But rowTotal is 1D array, why doesnt square bracket exist?
    In my mind, it should be
    rowTotal[i] +=nums[i] [j]
    I imagine, if something is written in nonarray formate, then it is a scaler. Namely it corresponds to only one value, 0D set.
    Why isnt it like in my mind?

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

      It depends how rowTotal is declared, you could do it either way. You could have something like:
      int rowTotal = 0;
      and then you could use:
      rowTotal += nums[i][j];
      and if it were done in a loop where j is incremented from 0 up until the length of a row (i.e. the number of columns), that should give you the sum of the row. But you could also have something like this:
      int rowTotal[ROWS] = {0};
      where ROWS is the number of rows. And then you could have:
      rowTotal[i] += nums[i][j];
      where again j is incremented from 0 up until the length of a row, and maybe i is incremented inside an outer loop that goes from 0 up until the number of rows.
      So it could be done either way, it depends on how rowTotal is declared and how it is being used in the code. :-)

    • @sukursukur3617
      @sukursukur3617 ปีที่แล้ว

      @@PortfolioCourses first of all, thank you very much for reply.
      For example
      int arr[3][4]={{1,2,3,3},{4,5,6,6},{7,8,9,9}}
      When we sum rows of matrix
      rowTotal[3]={9,21,33} i expect matrix to turn into this 1D set.
      By this reason, it sounds me it should be like this.
      for(i=0,i

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

    There is a way to do this program with calculate the size of the array in the function?

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

      Not really because when we pass an array to a function we're really passing a pointer to the first element in the array. So the solution is generally to just pass the length of the array to the function as well. :-)

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

    Are you able to do a similar thing with an array of floats?

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

      The code will work the exact same way, we just need to change the type from int to float everywhere (except the loop counter variable i and the length function parameter), and then we would output the numbers with %f instead of %d. 🙂

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

      @@PortfolioCourses Thank you

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

      @@jackburton4302 You're welcome! 🙂