#include <iostream>
#include <vector>
using namespace std;
int main() {
int a[] = { 2,4,6,8,10,12,14,16,18,20 }, * p;
p = &a[0] // p points to first elem of array a
for (int i = 0; i < 10; i++) {
cout << *p << ", "; // print the elem p points to
p++; // increment p so it points to the next elem
}
cout << '\n';
}
>>>
2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
int main() {
int a[] = { 2,4,6,8,10,12,14,16,18,20 },
* begin, * end, * cursor;
begin = a; // begin points to the first elem of array a
end = a + 10; // end points to just after the last elem
//print out the content of the array
cursor = begin;
while (cursor != end) {
cout << *cursor << ' '; //print the elem
cursor++; // increment cursor so it points to the next elem
}
cout << '\n';
}
>>>
2 4 6 8 10 12 14 16 18 20
int main() {
double sum = 0.0;
double* numbers; // number is a pointer not an array
int size; // actual number of entries
cout << "Please enter number of values to process: ";
cin >> size;
if (size > 0) {
cout << "Please enter " << size << " numbers: ";
numbers = new double[size]; // dynamically allocated array
for (int i = 0; i < size; i++) {
cin >> numbers[i]; //[] can be used with pointer
sum += numbers[i];
}
cout << "The average of ";
for (int i = 0; i < size - 1; i++)
cout << numbers[i] << ", ";
// no comma following last elem
cout << numbers[size - 1] << " is "
<< sum / size << '\n';
delete[] numbers; //free up the space held by numbers
}
}
>>>
Please enter number of values to process: 5
Please enter 5 numbers:
3
78
8
54
32
The average of 3, 78, 8, 54, 32 is 35
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
const int ROWS = 3, COLUMNS = 5;
//two-dimensional array of double-precision
using Matrix = double[ROWS][COLUMNS];
void populate_matrix(Matrix m) {
cout << "Enter the " << ROWS << " rows of the matrix.\n";
for (int row = 0; row < ROWS; row++) {
cout << "Row #" << row << " (enter " << COLUMNS << " values): ";
for (int col = 0; col < COLUMNS; col++)
cin >> m[row][col];
}
}
//declare m constant because printing a matrix should not change it
void print_matrix(const Matrix m) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLUMNS; col++)
cout << setw(5) << m[row][col];
cout << "\n";
}
}
int main() {
Matrix mat;
populate_matrix(mat);
print_matrix(mat);
}
>>>
Enter the 3 rows of the matrix.
Row #0 (enter 5 values): 1 2 3 4 5
Row #1 (enter 5 values): 5 4 3 2 1
Row #2 (enter 5 values): 3 4 5 1 2
1 2 3 4 5
5 4 3 2 1
3 4 5 1 2
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
bool find_char(const char* s, char ch) {
while (*s != '\0') {
if (*s == ch)
return true;
s++;
}
return false;
}
int main() {
const char* phrase = "this is a phrase";
for (char ch = 'a'; ch <= 'z'; ch++) {
std::cout << '\'' << ch << '\'' << " is ";
if (!find_char(phrase, ch))
std::cout << "NOT ";
std::cout << "in " << '\"' << phrase << '\"' << '\n';
}
}
>>>
'a' is in "this is a phrase"
'b' is NOT in "this is a phrase"
'c' is NOT in "this is a phrase"
'd' is NOT in "this is a phrase"
'e' is in "this is a phrase"
'f' is NOT in "this is a phrase"
'g' is NOT in "this is a phrase"
'h' is in "this is a phrase"
'i' is in "this is a phrase"
'j' is NOT in "this is a phrase"
'k' is NOT in "this is a phrase"
'l' is NOT in "this is a phrase"
'm' is NOT in "this is a phrase"
'n' is NOT in "this is a phrase"
'o' is NOT in "this is a phrase"
'p' is in "this is a phrase"
'q' is NOT in "this is a phrase"
'r' is in "this is a phrase"
's' is in "this is a phrase"
't' is in "this is a phrase"
'u' is NOT in "this is a phrase"
'v' is NOT in "this is a phrase"
'w' is NOT in "this is a phrase"
'x' is NOT in "this is a phrase"
'y' is NOT in "this is a phrase"
'z' is NOT in "this is a phrase"
'Code > C++' 카테고리의 다른 글
[C++] 계좌만들기/ private & public (0) | 2019.11.02 |
---|---|
[C++] class & object/ private & public (0) | 2019.11.02 |
[C++] Vector Matrix (0) | 2019.10.18 |
[C++] Array / Dynamic array/ static array (0) | 2019.10.18 |
[C++] (example) Vector (0) | 2019.10.17 |