Skip to content

Commit 2d28bb4

Browse files
committed
Merge pull request #27 from publicarray/master
added c++ files
2 parents 8596a87 + 4177a89 commit 2d28bb4

11 files changed

+170
-0
lines changed

C++/31_cppBeginners.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int factorialFinder(int x){ // recursive function
5+
if(x==1){ // base case - end the function when x = 1
6+
return 1;
7+
}else{
8+
return x*factorialFinder(x-1);
9+
}
10+
}
11+
12+
int main()
13+
{
14+
cout << factorialFinder(5) << endl;
15+
}

C++/32_cppBeginners.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int bucky[5] = {66,75,2,43,99}; // array initialiser list
7+
8+
cout << bucky[2] << endl; // the array index starts at 0 so bucky[0] = 66, bucky[1] = 75, and bucky[1] = 2 ...
9+
}

C++/33_cppBeginners.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int bucky[9];
7+
8+
cout << "Element - Value" << endl;
9+
for (int x= 0; x<=8; x++){
10+
bucky[x] = 99;
11+
cout << x << " ----- " << bucky[x] << endl;
12+
}
13+
}

C++/34_cppBeginners.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int tuna[5] = {20,54,76,832,546};
7+
int sum = 0;
8+
9+
for(int x = 0; x < 5; x++){
10+
sum += tuna[x];
11+
cout << sum << endl;
12+
}
13+
}

C++/35_cppBeginners.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void printArray(int theArray[], int sizeOfArray); // prototype
5+
6+
int main(){
7+
int bucky[3] = {20,54,675};
8+
int jessica[6] = {54,24,7,8,9,99};
9+
10+
printArray(bucky, 3);
11+
printArray(jessica, 6);
12+
}
13+
14+
void printArray(int theArray[], int sizeOfArray){
15+
16+
for(int x = 0;x<sizeOfArray; x++){
17+
cout << theArray[x] << endl;
18+
}
19+
}

C++/36_cppBeginners.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
6+
// [rows][columns]
7+
int sally[2][3] = {{2,3,4},{8,9,10}};
8+
9+
// index 0 1 2
10+
// _________
11+
// 0 |2, 3, 4
12+
// 1 |8, 9, 10
13+
14+
cout << sally [1][1];
15+
}

C++/37_cppBeginners.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
6+
int bertha[2][3] = {{1,2,3},{7,8,9}};
7+
8+
for(int row=0; row<2; row++){
9+
for(int column=0; column<3;column++){
10+
cout << bertha[row][column] << " ";
11+
}
12+
cout << endl;
13+
}
14+
}

C++/38_cppBeginners.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
int fish = 5;
6+
cout << &fish << endl; // address operator (&)
7+
8+
int *fishPointer; // a pointer
9+
fishPointer = &fish;
10+
11+
cout << fishPointer << endl;
12+
}

C++/39_cppBeginners.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void passByValue (int x);
5+
void passByReference(int *x);
6+
7+
int main(){
8+
int betty = 13;
9+
int sandy = 13;
10+
11+
passByValue(betty);
12+
passByReference(&sandy);
13+
14+
cout << "betty is now " << betty << endl;
15+
cout << "sandy is now " << sandy << endl;
16+
}
17+
18+
void passByValue (int x){
19+
x = 99;
20+
}
21+
22+
void passByReference(int *x){
23+
*x=66;
24+
}

C++/40_cppBeginners.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main(){
5+
6+
char a;
7+
cout << "size of char: " << sizeof(a) << endl;
8+
9+
int b;
10+
cout << "size of int: " << sizeof(b) << endl;
11+
12+
double c;
13+
cout << "size of double: " << sizeof(c) << endl;
14+
15+
double bucky[10];
16+
cout << "size of bucky[]: " << sizeof(bucky) << endl;
17+
cout << "no. of elements in bucky[]: " << sizeof(bucky) / sizeof(bucky[0]) << endl;
18+
19+
}

0 commit comments

Comments
 (0)