Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions week03/examples/for.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using namespace std;
int main()
{
int sum = 0;
for(int i = 0; i < 10; i++)
long long sum = 0;
long num;
cout<<"Enter any positive number: ";
cin>>num;
for(int i = 1; i <= num; i++)
{
sum += i;
cout << "Line " << i << endl;
}
cout << "sum = " << sum << endl;
cout << "sum of all numbers till " <<num<<" is: "<< sum << endl;

return 0;
}
Binary file added week03/examples/for.exe
Binary file not shown.
Binary file added week03/examples/if.exe
Binary file not shown.
21 changes: 10 additions & 11 deletions week03/examples/ternary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ using namespace std;

int main()
{
bool isPositive = true;
int factor = 0;
//some operations may change isPositive's value
if(isPositive)
factor = 1;
else
factor = -1;
//the if-else statement can be replaced by a ternary conditional operation
factor = isPositive ? 1 : -1;
// to understand ternary operator

//sometimes the following code can be more efficient.
factor = isPositive * 2 - 1;
int a,b;
cout<<"This code will help you to understand ternary operator better."<<endl;
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;

//ternary operator
int max = a>b?a:b;
cout<<"The greater number is: "<<max;
return 0;
}
Binary file added week03/examples/ternary.exe
Binary file not shown.
7 changes: 4 additions & 3 deletions week03/examples/while.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ using namespace std;
int main()
{
int num = 10;
while(num > 0)
while(num > 0) // when num gets 0 loop will break
{
cout << "num = " << num << endl;
num--;
}

// num gets 0 and priting it out of while loop
cout<<"num = "<<num;
// num = 10;
// do
// do
// {
// cout << "num = " << num << endl;
// num--;
Expand Down
Binary file added week03/examples/while.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion week04/examples/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using namespace std;
int main()
{
int num_array1[5]; //uninitialized array, random values
int num_array1[5]; // 5 is the size of the array and array is uninitialized it will give random values
int num_array2[5] = {0, 1, 2, 3, 4}; //initialization

for(int idx = 0; idx < 5; idx++)
Expand Down
Binary file added week04/examples/array.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion week04/examples/index-bound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ int main()
{
int num_array[5];

for(int idx = -1; idx <= 5; idx++) //out of bounds
for(int idx = -1; idx <= 5; idx++) //out of bounds <- -1 index does not exits and 5 also->
num_array[idx] = idx * idx;

for(int idx = -1; idx <= 5; idx++) //out of bounds
Expand Down
Binary file added week04/examples/index-bound.exe
Binary file not shown.
Binary file added week04/examples/stdstring.exe
Binary file not shown.