(10%) Determine whether the following code has syntax erros or
not. If it is correct, predict its output. If it is
incorrect, point out the
mistake(s).
// Exchange Sort
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
int main()
{
char a[][8] = { "Brazil", "Spain", "Gerard",
"Miami", "Denmark", "Lisbon",
"Venice", "Hawaii"};
int N = sizeof(a)/sizeof(a[0]);
char temp[10];
for (int i=0; i<N-1; i++)
{
for (int j=i+1; j<N; j++)
if (a[i][0] > a[j][0])
{
strcpy(temp, a[i]);
strcpy(a[i], a[j]);
strcpy(a[j], temp);
}
for (int i=0; i<N; i++)
cout << a[i] << '\t';
cout << endl;
}
return 0;
}
(10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
// Arrays of Pointers to char (P.152)
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
int main()
{
const char* a[] = { "Brazil", "Spain", "Gerard",
"Miami", "Denmark", "Lisbon",
"Venice", "Hawaii"};
cout << sizeof(a) << sizeof(a[0]) << endl;
int N = sizeof(a)/sizeof(a[0]);
const char* p[N];
int i;
for (i=0; i<N; i++)
{
cout << a[i] << '\t';
p[i] = a[i];
}
cout << endl;
const char* temp;
for (int i=0; i<N-1; i++)
{
for (int j=i+1; j<N; j++)
if (*p[i] < *p[j])
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
for (int i=0; i<N; i++)
cout << p[i] << '\t';
cout << endl;
}
for (int i=0; i<N; i++)
cout << a[i] << '\t';
cout << endl;
return 0;
}