typedef signed int sint;
#define N 100
static int n = N;
inline int f(const int n, int& i, int* j, int t[2], int p=11)
{
i*=i;
(*j)--;
if ( n<=0 )
{
cout << ::n << ">\n";
return t[0];
}
else return f(n-1, i, j, t) + t[n];
}
int main()
{
int n = 4;
int x = 1U;
sint y = 10;
int (*fptr)(const int, int&, int*, int*, int) = f;
int* t = new int[n];
int& r = *(t+3);
(*t) = 1;
*(t+1) = 2;
t[2] = 3;
r = 4;
int z = (*fptr)(5, x, &y, t, 12);
for(int i = 0; i < 2*n; i++)
{
if( i == n )
continue;
if( i > n )
break;
cout << t[i] << "\n";
};
cout << x << ", " << y << ", " << z << "\n";
delete[] t;
}
This is what my professor gave as part of the final exam. The purpose of giving us this code was to get us used to seeing different ways the C++ syntax can be used and figure out what the output is.
string sum_numbers(string a, string b) {
char ca, cb, ci, out, co = '0';
string result = "";
while (a.size() > 0 || b.size() > 0 || co != '0') {
ci = co; ca = '0'; cb = '0';
if (a.size() > 0) {ca = a.back(); a.pop_back();}
if (b.size() > 0) {cb = b.back(); b.pop_back();}
result = ((((ca != cb) ? '1' : '0') != ci) ? '1' : '0') + result;
co = ((((ca == '1' && cb == '1') ? '1' : '0') == '1' ||
((((ca != cb) ? '1' : '0') == '1' && ci == '1') ? '1' : '0') == '1') ? '1' : '0');
}
return result;
}
Scary stuff
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iterator>
constexpr int N = 10;
int main() {
std::vector<int> even;
std::vector<int> odd;
even.resize(N);
// Fill up the 'even' vector with integers starting from from 1 through 'N'
std::iota(even.begin(), even.end(), 1);
// Segregate the odd and even integers from each other
for (auto it = even.begin(); it != even.end(); ++it)
if (*it % 2 != 0) {
// If the number is odd, put it in the 'odd' vector
odd.push_back(*it);
// Remove the number from the even vector
even.erase(it);
}
// Print the result
std::cout << "Even numbers: ";
std::copy(even.begin(), even.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\nOdd numbers: ";
std::copy(odd.begin(), odd.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
}
Seems fine to me... C++ couldn't possibly be THAT evil to introduce another nuanced and verbose complexity in there, right?
int main() {
int error_code = some_function();
if (error_code == 1) {
// In case of error, crash the program by eating up memory
while (true) new int;
}
}