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!
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?
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. :-)
@@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
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. :-)
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. 🙂
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!
Thank you for the example for my final
You're welcome Ed! Good luck on your final. :-)
I’m not going to fail thanks to this
I'm happy to hear that Isaiah! :-D
int SumOfArray(const int* array, int arraySize) {
int sum = 0;
while (arraySize > 0) {
arraySize --;
sum += array[arraySize];
}
return sum;
}
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?
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. :-)
@@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
There is a way to do this program with calculate the size of the array in the function?
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. :-)
Are you able to do a similar thing with an array of floats?
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. 🙂
@@PortfolioCourses Thank you
@@jackburton4302 You're welcome! 🙂