-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.cpp
52 lines (37 loc) · 2.43 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Stephanie Almeida
Example Program
Shows basic elements of C++, will be edited as more and more lessons pass by
4/3/16 */
#include <iostream> //Including necessary libraries and namespace std
using namespace std;
int sum(int, float); //function definition
int main() { //main function
int num1 = 5; //declaring an int variable
float dec1 = 0.5; //declaring a float variable
int arr1[5] = 0; //declaring array of ints with length 5, inital = 0
int arr2[] = {0, 1, 2, 3, 4}; //declaring array and initializing the ints
cout << "Hello World!" << endl; //output statement
cout << num1 * dec1 << endl; //output 2.5
cout << "Please enter num1: ";
cin >> num1; //input num1
cout << "Please enter dec1: ";
cin >> dec1; //input dec1
cout << num1 + dec1 << endl; //output whatever num1 + dec1 is equal to
if (num1 > 1) {
cout << "Num1 is greater than 1!" << endl; //if statement
}
for (int i = 0; i < num1; i++) {
cout << i << " "; //for loop
}
cout << endl;
while (dec1 == num1) {
cout << "dec1 and num1 are the same!" << endl; //while loop
num1++; //will only execute once since num1 changes
}
cout << sum(1, 5) << endl; //calls function and outputs 6
for (int i = 0; i < 5; i++) //initializing arr1 with values from arr2
arr1[i] = arr2[i];
return 0; } //end of program
int sum (int a, float b) { //function itself
return a + b;
}