There is something wrong with each of the following. (The first one is meant to be a complete program; the rest are program fragments.) In each case, explain briefly what is wrong:
#include <iostream>
int main( )
{ cout << "1234";
}
int x, 2x = 2;
cin >> x; cout << x * 2x;
int x, y = 0;
cin >> y;
if (y > x) cout << "Bigger";
if (s[1] == 'A') cout << "Starts with A";
int totlen = 0;
while (not infile.fail())
{ getline(infile, s);
totlen += s.length();
}
if (x || y > 0) cout << "At least one of them is positive";
for (int i = v.size(); i >= 0; i--)
v[i] = v[i-1];
bool isthere (const vector<int>& v, int q)
{ for (int i = 0; i < v.size(); i++)
if (v[i] == q) return true;
else return false;
ifstream infile;
string filename = "a2f.txt";
infile.open(filename);
Give the output of each of the following:
string s = "Who's Who?", t = "Who is Who?";
cout << (s < t ? s: t);
int x = 12, y = 5;
switch (x % y)
{ case 1: s = "One";
case 2: s = "Two";
default: s = "Neither";
}
cout << s;
int x = 9, y = 3;
if (y > 0)
if (x % y > 0)
cout << "x not divisible by
y";
else cout << "y is zero";
char s[] = "ZANZIBAR";
int notZ = 0;
for (int i = 0; i <= 8; i++)
if (s[i] != 'Z') notZ++;
cout << notZ;
int i = 4, j = 10;
double d = 2, e = 8.8;
cout << fixed << setprecision(1) << j/i << "
" << e/d << " ";
cout << e/i << " " << d + j / i << "
";
d = i; j = e;
cout << d << " " << j << endl;
int n = 0;
while ( ++n < 5);
cout << n << " ";
int i; double d; string s;
cout << fixed << setprecision(1);
while (cin >> i >> d >> s)
cout << i << " " << d <<
" " << s << endl;
Write down the output of this program:
#include <iostream> #include <string> using namespace std; int a = 0; int x = 0; int main( ) { int a = 0, b = 0; extern int c; void f1(int x); void f2(int& x); cout << "m1: " << a << " " << b << " " << c << " " << x << endl; f1(a); cout << "m2: " << a << " " << b << " " << c << " " << x << endl; f2(b); cout << "m3: " << a << " " << b << " " << c << " " << x << endl; } void f1(int x) { int b = 0; extern int c; a++; x++; b++; c++; cout << "f1: " << a << " " << b << " " << c << " " << x << endl; } void f2(int& x) { int b = 0; extern int c; a += 2; x += 2; b += 2; c += 2; cout << "f2: " << a << " " << b << " " << c << " " << x << endl; } int c = 0;
A firework company keeps records of its sales in a text file. Part of the file might look like this:
Mount Vesuvius 80965 Emerald Spray 46589 Crackerjack 65890 Snow Storm 64578 Golden Rain 63865 Giant Catherine Wheel 76354 Air Bomb 34672 Firestorm 9685 Standard sparklers 153876 Coloured sparklers 84750 Giant sparklers 120853
Write a program to read this file and to output the number of the best-selling firework sold and the number of the worst-selling. (For the above file, the output would be 153876 and 9685.) You may assume there is only one best-selling firework and only one worst-selling.
Your program should read the file only once. You may read the file using
standard input or by opening an ifstream
, as you wish.
Write a function that takes a vector of integers as its parameter and which returns true if every odd number is followed by an even number and every even one by an odd one (except for the last, obviously). If there are fewer than two numbers in the vector, the function should return true.
****** * ** * * * * * * ** * ******
An experimental psychologist has designed the following class in C++ to perform a (very) crude simulation of short-term memory:
class Brain { public: Brain(); void add_an_item(string s); void bump( ); void display( ) const; private: static const int MAX = 7; vector<string> memory; };
A Brain
is initially empty. add_an_item
adds a string to the memory.
The memory can only contain up to seven strings. The addition of another
string causes the first (oldest) string to be forgotten. A bump
on the head
causes the most recently added string to be forgotten. display
displays the
current contents of memory.
Write the definitions of the interface functions.
A text file contains lines varying in length; you may assume
that no line is longer than
100 characters. Write a program that reads this file and outputs the
number of blank lines and the length of the longest
line. The program should read the file only once. You do not know in
advance how long the file is. You may read it from standard input
or by opening an ifstream
, as you wish.
Modify the program of the previous question so that it also outputs the most frequently occurring line length. (If, for example, there were more lines of 62 characters than of any other single length, it would output 62.) You may assume that just one line length is the most frequent. Just write the modifications; there is no need to write out the whole program again.
Write a function that takes a vector of first-names (such as "Audrey", "David") as its
parameter and which returns true
if the names are in alphabetical order.
If there are fewer than two names in the vector, the function should
return true
.
Write a procedure that takes a string as its parameter and which prints out a pattern made out of the string. For example, given the string "XMAS", it would output the following:
X MM AAA SSSS
Given the following:
class Weekday { public: Weekday(int); Weekday(string); private: int day; static const string dayname[]; friend ostream& operator<<(ostream&, const Weekday&); }; ......... int main() { Weekday wd(1); Weekday wd2("Fri"); Weekday wd3(8); cout << wd << " " << wd2 << " " << wd3 << endl; }the output would be:
Sun Fri ?i.e. any invalid value for a constructor gives rise to some dummy value for
day
which appears as a question mark on the output.
Write the definitions of the array and of the member functions.
Write a complete definition (class and functions) for an Ivec class. An Ivec is like a vector of int with the following functionality:
there
function that takes an integer and returns true
if
that integer is in the Ivec; otherwise false
.ivec3 = ivec1 + ivec2;
would be 4, 6, 22, -5, 33, 1, 12.remove
procedure that takes an integer and removes the
first occurrence of that integer from
the Ivec, closing up the gap without changing the order of the values
in the Ivec. If the integer does not occur in the Ivec, it does nothing.zap
procedure that takes an integer and removes all
occurrences of that integer from the Ivec.Notes on R. Mitton's lectures by S.P. Connolly, edited by R. Mitton, 2000