• Nov 18, 2022 •AustinLeath
0 likes • 0 views
#include <iostream> #include <cmath> #include <string.h> using namespace std; int main() { string tickerName; int numOfContracts; float currentOptionValue; cout << "Enter a stock ticker: "; getline(cin, tickerName); cout << "Enter the current number of " << tickerName << " contracts you are holding: "; cin >> numOfContracts; cout << "Enter the current price of the option: "; cin >> currentOptionValue; cout << "The value of your " << tickerName << " options are: $" << (currentOptionValue * 100.00) * (numOfContracts); cout << endl; return 0; }
• Sep 7, 2022 •LeifMessinger
#include <iostream> #include <cstring> int main(int argc, char** argv){ //With decimal if(strstr(argv[1], ".") != nullptr){ int i = 0; //Skip i to first non 0 digit while(argv[1][i] < '1' || argv[1][i] > '9') ++i; //If digit comes before decimal if((argv[1] + i) < strstr(argv[1], ".")){ //Good example of pointer arithmetic std::cout << strlen(argv[1] + i) - 1 << std::endl; //Another good example }else{ //If digit is after decimal std::cout << strlen(argv[1] + i) << std::endl; } }else{ //Without decimal int m = 0; int i = 0; while(argv[1][i] < '1' || argv[1][i] > '9') ++i; //In case of some number like 0045 for(; argv[1][i] != '\0'; ++i){ if(argv[1][i] >= '1' && argv[1][i] <= '9') m = i + 1; } std::cout << m << std::endl; } return 0; }
• Apr 15, 2025 •hasnaoui1
0 likes • 4 views
int main()
• Oct 7, 2023 •AustinLeath
0 likes • 12 views
#include <iostream> #include <cstring> #include <unistd.h> #include <sys/utsname.h> int main() { char newHostname[] = "newhostname"; // Replace with the desired hostname if (sethostname(newHostname, strlen(newHostname)) == 0) { std::cout << "Hostname set to: " << newHostname << std::endl; // Optionally, update the /etc/hostname file to make the change permanent FILE *hostnameFile = fopen("/etc/hostname", "w"); if (hostnameFile != NULL) { fprintf(hostnameFile, "%s\n", newHostname); fclose(hostnameFile); } else { perror("Failed to update /etc/hostname"); } } else { perror("Failed to set hostname"); } return 0; }
• Apr 16, 2023 •LeifMessinger
#include <iostream> #include <string> //Should already be in iostream #include <cstdlib> //A word score adds up the character values. a-z gets mapped to 1-26 for the values of the characters. //wordScore [wordValue] //Pipe in the input into stdin, or type the words yourself. //Lowercase words only int characterValue(const char b){ return ((b >= 'a') && (b <= 'z'))? ((b - 'a') + 1) : 0; } int main(int argc, char** argv){ //The first argument specifies if you are trying to look for a certain word score int wordValue = (argc > 1)? std::atoi(argv[1]) : 0; std::string line; while(std::getline(std::cin, line)){ int sum = 0; for(const char c : line){ sum += characterValue(c); } if(wordValue){ //If wordValue is 0 or the sum is the correct value if(wordValue == sum){ std::cout << line << std::endl; } } else { std::cout << sum << "\t" << line << std::endl; } } return 0; }
1 like • 7 views
#include <iostream> using namespace std; int main() { int arr[5]; for(int i = 0; i < 5; i++) { arr[i] = i; } for(int i = 0; i < 5; i++) { cout << "Outputting array info at position " << i + 1 << ": " << arr[i] << endl; } for(int i=0;i<5;i++) { for(int j=i+1;j<5;j++) { if(arr[i]>arr[j]) { int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } cout << endl; for(int i = 0; i < 5; i++) { cout << "Outputting sorted array info at position " << i + 1 << ": " << arr[i] << endl; } return 0; }