// partition the specified array segment,
// returning the crossover point determined by partioning
int partition(int arr[], int lower, int upper)
{
if (lower >= upper) return lower;
int partValue = arr[lower];
int low = lower+1;
int high = upper;
do {
while ((arr[low] <= partValue) && (low < high)) low++;
while ((arr[high] >= partValue) && (low < high)) high--;
if (low < high) {
swap(arr[low], arr[high]);
low++;
high--;
}
} while (low < high);
int crossover = high;
if (arr[lower] <= arr[crossover]) crossover--;
swap(arr[lower], arr[crossover]);
return crossover;
}
|
The program should open the input file for reading and the output file for writing, and (assuming both opened successfully) copy all the digits from the input file to the output file. (Reminder: the isdigit(c) function will return true if the passed character is a digit, false otherwise.)
The program should display an error message if passed an incorrect number of command line arguments or either file fails to open, and be sure to close any successfully-opened files before the program ends.
struct Fraction {
unsigned long numerator;
unsigned long denominator;
};
Based on that definition, provide an implementation for the GreaterThanOne function below:
// display all the Fractions in the array for which the numerator is greater than the denominator, // outputting them in the form numeratorvalue/denominatorvalue, e.g. 17/12 void GreaterThanOne(Fraction arr[], int size)
#include <iostream>
using namespace std;
struct MyInts {
int x;
int *y;
};
void print1(int* i)
{
cout << (*i) << endl;
}
void print2(int i)
{
cout << i << endl;
}
int main()
{
int data = 25;
MyInts* moreData = new MyInts;
moreData->x = 6;
moreData->y = &data;
print1(moreData->y);
print1(&moreData->x);
print2(*moreData->y);
print2(moreData->x);
}
|
The routine should attempt to open a file named "mydata" for writing (output), and write the integers 1..N into the file, where N is the integer from the command line argument.
Be sure to generate an error message if the file fails to open or an incorrect number of command line arguments are passed or if the value passed isn't a positive integer, and to close the file before ending the program (if it opened successfully).
Tip: atoi(somechararray) will return the integer equivalent of the contents of the character array, and returns 0 if the contents don't represent a valid integer.
struct CarInfo {
string make;
string model;
int year;
};
(i) Assuming the array has been filled with N valid CarInfo structs, complete
the oldestMake function described below.
// return the make for the car with the oldest (smallest/earliest) year // Error handling: return "" if N is less than one string oldestMake(CarInfo allcars[], int N)(ii) If all the cars in the array had the same year but different models, which one would your code return? (E.g. the first one, the last one, ...?)
Further, suppose the program uses logic similar to the following: