// create an array of 10 doubles const int ArrSize = 10; double myArray[ArrSize]; |
// create and initialize an array of 3 floats const int ASize = 3; float arr[ASize] = { 10.5, 6.123, 3.14 }; |
myArray[4] = 1.0; // put the value one in array position 4 |
float x = arr[1]; // copy a value from array position 1 to variable x |
// put the value 0.5 in each position of the array for (int pos = 0; pos < ArrSize; pos++) { myArray[pos] = 0.5; } |
// prototype for a function to print the contents of an array void printArray(double arr[], int size); // ... suitable call to the function from somewhere inside main printArray(myArray, ArrSize); |
const int Size = 256; char text[Size]; text[0] = '\0'; |
char str[Size]; strcpy(str, "put this text in the array"); |
strcpy(str1, str2); |
strcat(str1, str2); |
std::cout << "the contents are: " << str1; |
const int MaxSize = 64; char str[MaxSize]; std::cout << "Please enter a line of text:"; std::cin.getline(str, MaxSize); |
int len = strlen(str); |
int result = strcmp(str1, str2); if (result == 0) { cout << "the two strings are the same, both are " << str1; } else if (result < 0) { cout << str1 << " is alphabetically before " << str2; } else { cout << str2 << " is alphabetically before " << str1; } |
// assumes we used #include <cctype> earlier, // and that str is null-terminated void classifyChars(char str[]) { int pos = 0; while (str[pos] != '\0') { char ch = str[pos]; std::cout << ch " is a"; if (isdigit(ch)) { std::cout << " digit"; } else if (isupper(ch)) { // there is also an islower std::cout << "n uppercase alphabetic char"; } else if (isalpha(ch)) { // there is also an alnum for alpha-numeric std::cout << "n alphabetic char"; } else if (isspace(ch)) { // ... and there are quite a number of others ... std::cout << " whitespace character"; } else { std::cout << " character of some other type"; } } } |