#include <iostream.h> float scramble (int *a, int n) { for (bool fini=false; !fi
ID: 3613288 • Letter: #
Question
#include <iostream.h>
float scramble (int *a, int n) {
for (bool fini=false; !fini;) {
fini = true;
for (int i=0; i<(n-1); i++) {
if ( *(a+i) > *(a+i+1)) {
int tmp = *(a+i);
*(a+i) = *(a+i+1);
*(a+i+1) = tmp;
fini = false;
};
};
};
return ( (n%2) ? (float)(*(a+n/2)) : (
(float)((*(a+n/2 - 1) + *(a+n/2))) / 2.0));
}
int main () {
int a[100], n(0), i(1);
cout << "Enter data (-9999 to end):";
cin >> a[0];
while (a[i-1] != -9999) cin >>a[i++];
float mystery = scramble (a, --i);
cout << endl << "Results are: " << i<< ": ";
for (int j=0; j<=i; j++) cout << a[j] << "";
cout << ": " << mystery <<endl;
effects ofthe function scrambleas regards to its first
formalparameter, a, asfollows:
a) No effect (because in C and C++ parameters arepassed
by value).
b) The contents of the first n memory locationspointed
to by parameter a are sorted in ascendingorder.
c) The contents of the first n memory locationspointed
to by parameter a are cleared tozero.
d) The contents of the first n memory locationspointed
to by parameter a are scrambled into apseudorandom
order.
e) None of the above.
Explanation / Answer
#include <iostream.h>
float scramble (int *a, int n) {
for (bool fini=false; !fini;) {
fini = true;
for (int i=0; i<(n-1); i++) {
if ( *(a+i) > *(a+i+1)) {
int tmp = *(a+i);
*(a+i) = *(a+i+1);
*(a+i+1) = tmp;
fini = false;
};
};
};
return ( (n%2) ? (float)(*(a+n/2)) : (
(float)((*(a+n/2 - 1) + *(a+n/2))) / 2.0));
}
int main () {
int a[100], n(0), i(1);
cout << "Enter data (-9999 to end):";
cin >> a[0];
while (a[i-1] != -9999) cin >>a[i++];
float mystery = scramble (a, --i);
cout << endl << "Results are: " << i<< ": ";
for (int j=0; j<=i; j++) cout << a[j] << "";
cout << ": " << mystery <<endl;
effects ofthe function scrambleas regards to its first
formalparameter, a, asfollows:
a) No effect (because in C and C++ parameters arepassed
by value).
b) The contents of the first nmemory locations pointed
to by parameter a are sorted inascending order.
c) The contents of the first n memory locationspointed
to by parameter a are cleared tozero.
d) The contents of the first n memory locationspointed
to by parameter a are scrambled into apseudorandom
order.
e) None of the above.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.