#include
// Array of string pointers to be sorted.
static const char* brothers[] =
{
"Frederick William",
"Joseph Jensen",
"Harry Alan",
"Walter Ellsworth",
"Julian Paul"
};
// Linkage specification.
extern "C"
{
// Prototype of C function.
void SortCharArray(const char**);
// C++ function to be called from the C program
int SizeArray()
{
return sizeof brothers / sizeof (char*);
}
}
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
// Sort the pointers.
SortCharArray(brothers);
// Display the brothers in sorted order.
int size = SizeArray();
for (int i = 0; i < size; i++)
std::cout << brothers[i] << std::endl;
return 0;
}
/*////////////////////////////////////*/
#include
#include
/* Prototype of the C++ function. */
int SizeArray(void);
/*////////////////////////////////////*/
/* The compare function for qsort(). */
/*////////////////////////////////////*/
static int comp(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}
/*////////////////////////////////////*/
/* C function called from C++ program.*/
/*////////////////////////////////////*/
void SortCharArray(char **List)
{
int sz = SizeArray();
qsort(List, sz, sizeof(char *), comp);
}
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
// Declare three integers.
int HourlyRate;
int HoursWorked;
int GrossPay;
// Assign values to the integers.
HourlyRate = 15;
HoursWorked = 40;
GrossPay = HourlyRate * HoursWorked;
// Display the variables on the screen.
std::cout << HourlyRate;
std::cout << ' ';
std::cout << HoursWorked;
std::cout << ' ';
std::cout << GrossPay;
return 0;
}
#include
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
int Ctr, OldCtr, NewCtr;
// Make the assignments
OldCtr = 123; // OldCtr is 123
NewCtr = ++OldCtr; // NewCtr is 124, OldCtr is 124
Ctr = NewCtr--; // Ctr is 124, NewCtr is 123
// Display the results
std::cout << OldCtr;
std::cout << ' ';
std::cout << NewCtr;
std::cout << ' ';
std::cout << Ctr;
return 0;
}
#include
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
float Dues; // dues amount
// Read the dues.
std::cout << "Enter dues amount: ";
std::cin >> Dues;
// Are the dues paid on time?
std::cout << "On time? (y/n) ";
char yn;
std::cin >> yn;
bool Overdue; // true if overdue, false if on time
Overdue = yn != 'y';
float AmountDue; // amount to be computed
// Use conditional operator to compute.
AmountDue = Overdue ? Dues * 1.10 : Dues;
// Display the dues amount.
std::cout << "Amount due: ";
std::cout << AmountDue;
return 0;
}
More Articles …
Page 19 of 21