C++ Week 12

Sample halfway test

Question 1

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:

  1. #include <iostream>
    int main( )
    { cout << "1234";
    }


  2. int x, 2x = 2;
    cin >> x; cout << x * 2x;


  3. int x, y = 0;
    cin >> y;
    if (y > x) cout << "Bigger";


  4. if (s[1] == 'A') cout << "Starts with A";

  5. int totlen = 0;
    while (not infile.fail())
    { getline(infile, s);
      totlen += s.length();
    }


  6. if (x || y > 0) cout << "At least one of them is positive";

  7. for (int i = v.size(); i >= 0; i--)
       v[i] = v[i-1];


  8. 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;

    }

  9. ifstream infile;
    string filename = "a2f.txt";
    infile.open(filename);

Question 2

Give the output of each of the following:

  1. string s = "Who's Who?", t = "Who is Who?";
    cout << (s < t ? s: t);

  2. int x = 12, y = 5;
    switch (x % y)
    { case 1: s = "One";
      case 2: s = "Two";
      default: s = "Neither";
    }
    cout << s;


  3. int x = 9, y = 3;
    if (y > 0)
       if (x % y > 0)
          cout << "x not divisible by y";
    else cout << "y is zero";


  4. char s[] = "ZANZIBAR";
    int notZ = 0;
    for (int i = 0; i <= 8; i++)
       if (s[i] != 'Z') notZ++;
    cout << notZ;


  5. 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;



  6. int n = 0;
    while ( ++n < 5);
       cout << n << " ";


  7. (Assume this input: 35 46.2 I.C.I. 49.3 65 De Beers )
    int i; double d; string s;
    cout << fixed << setprecision(1);
    while (cin >> i >> d >> s)
       cout << i << " " << d << " " << s << endl;

Question 3

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;

Question 4

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.

Question 5

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.

Question 6

Write a procedure that takes an integer as its parameter and which prints out a hollow square of stars (square in the sense that each side has the same number of stars) with a diagonal line going from top right to bottom left. For example, if given 6, it would produce the following:
******
*   **
*  * *
* *  *
**   *
******

Question 7

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.

Some more sample questions

Question 8

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.

Question 9

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.

Question 10

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.

Question 11

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

Question 12

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.

Question 13

Write a complete definition (class and functions) for an Ivec class. An Ivec is like a vector of int with the following functionality:

  1. It has a constructor that takes as its parameters an array of int and an integer giving the length of the array.

  2. If an array-bounds error occurs, the program terminates with the message "Invalid subscript: " followed by the offending subscript value.

  3. It has a there function that takes an integer and returns true if that integer is in the Ivec; otherwise false.

  4. The "+" operator denotes concatenation. For example, given ivec1 with values 4, 6, 22, -5 and ivec2 with values 33, 1, 12, the value of ivec3 after ivec3 = ivec1 + ivec2; would be 4, 6, 22, -5, 33, 1, 12.

  5. It has a 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.

  6. It has a zap procedure that takes an integer and removes all occurrences of that integer from the Ivec.

  7. The output operator can take an Ivec as an argument. The effect is to output the values of the Ivec, in order (first iv[0], then iv[1] and so on) on the same line with a space between one value and the next.

Notes on R. Mitton's lectures by S.P. Connolly, edited by R. Mitton, 2000