Exam Practice!
True/False Problems
1. T
Multiple Choice Problems
Memory Snapshot Problems
9. name=>20.5 x=>20.5
a=>14
Vector Functions
/*——————————————————————–*/
}
/*——————————————————————–*/
/*——————————————————————–*/
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter9_16 */
/* */
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter9_17 */
}
/*——————————————————————–*/
Character Functions: Pattern Recognition
/*——————————————————————–*/
/* Problem chapter9_18 */
int charcnt(char *ptr, char c)
{
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter9_18s */
/* */
length = str.length();
/* Count the occurances of the letter. */
for (i=0; i < length; i++)
if (str[i] == c)
occur++;
int repeat(char *ptr)
{
/* Declare and initialize variables. */
int i, length, repeaters=0, allchars[NUM_CHAR];
/* Initialize the array of character counts. */
/* Return count of repeated characters. */
return repeaters;
}
/*——————————————————————–*/
int repeat2(char *ptr)
{
/* Declare and initialize variables. */
int i, length, repeaters=0, allchars[NUM_CHAR];
/* Initialize the array of character counts. */
for (i=0; i<NUM_CHAR; i++)
allchars[i] = 0;
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter9_21 */
/* */
/* This function receives pointers to two characters string */
/* and returns a count of the number of times that the second */
/* string occurs in the first. It does not allow overlap of */
/* Repeat until no more copies of the pattern in the string. */
do
{
/* Get a pointer to the beginning of the pattern. */
tmp = strstr(tmp,ptr2);
}
/*——————————————————————–*/
/*——————————————————————–*/
/* Problem chapter9_22 */
/* */
/* This function receives pointers to two characters string */
/* and returns a count of the number of times that the second */
/* string occurs in the first. It allows overlap of occurrences. */
const int MAXLENGTH=100;
do
{
/* Get a pointer to the beginning of the pattern. */
tmp = strstr(tmp,ptr2);
/* Check to see if a pattern was found. */
if (tmp != NULL)
{
/*——————————————————————–*/
/* Problem chapter9_23 */
/* */
/* This function receives a pointer to a character string and */
/* returns the number of repeated characters that occur in */
/* the string. It ignores repeated blanks in the string. If a */
/* character occurs more than once in the string, it is counted */
/* as one repeated character. */
/* Ignore control characters and spaces. Start at the ASCII ‘!’ */
for (i = (int)’!’; i<NUM_CHAR; i++)
if (allchars[i] > 1)
int repeat2(string str)
{
/* Declare and initialize variables. */
int i, length, repeaters=0, allchars[NUM_CHAR];
/* Return the number of pairs of the same character. */
return repeaters;
}
/*——————————————————————–*/
/* Copy the first string to a temporary variable. */
tmp = str1;
/* Repeat until no more copies of the pattern in the string. */
while( pos != -1 )
{
}
/* Return a count of number of non-overlapping patterns. */
return occurances;
}
/*——————————————————————–*/
/*———————————————————————–*/
/* Problem chapter 9_26 */
void get_word(istream& in_stream, string& w);
void insert_word(string word, list<string> &wordlist);
void display_list(ostream& out_stream, list <string> wordlist);
int main()
{
ifstream in_stream;
}//end else
return 0;
}
/*———————————————————————-*/
/*———————————————————————-*/
/* This function will insert word into a wordlist */
/*———————————————————————-*/
/*———————————————————————-*/
/* This function returns the next word from the input stream. */
/* All non-alpha characters are treated as delimiters. */
/* The function will ignore all leading non-alpha characters, */
/* then read and store the following alpha characters */
/* until it reaches the next non alpha character. */
void get_word(istream& in_stream, string& w)
{
/* This function outputs the list of words to an output stream. */
/* Three words per column are printer. */
void display_list(ostream& out_stream, list<string> wordlist)
{
int columns(3), counter(0);
/*———————————————————————–*/
/* Problem chapter 9_27 */
/* This program builds a concordance of a text file by first inserting */
/* all words into a list. It also keeps a count of the number of times */
/* a word appears. */
/* */
#include <iostream>
void countWords(list<string> wordlist,list<int> &wordCount);
void display_list(ostream& out_stream, list <string> wordlist,
list <int> wordCount);
int main()
{
cout << “fail to open file ” << infile << endl;
else {
out_stream.open(outfile.c_str());
list <string> wordlist;
//Remove duplicate words after the list is sorted.
wordlist.unique();
out_stream << “There were ” << wordlist.size()
<< ” distinct words. \n”;
out_stream << “\nHere is the ordered list of words preceded by ” <<
“their counts\n”;
display_list(out_stream,wordlist,wordCount);
wordlist.insert(iter, word);
}
/*———————————————————————-*/
/*———————————————————————-*/
/* This function returns the next word from the input stream. */
/* All non-alpha characters are treated as delimiters. */
ch = tolower(ch);
w += ch;
in_stream.get(ch);
}
}
/*———————————————————————-*/
/*———————————————————————-*/
while(iter !=wordlist.end()){
if (*iter==word){
num=wordCount.back()+1;
wordCount.pop_back();
wordCount.push_back(num);
} else {
int columns(3), counter(0);
list<string>::iterator iter;
list<int>::iterator iterC;
out_stream << setiosflags(ios::left);
// Position iter at beginning of list.
iter = wordlist.begin();
iterC = wordCount.begin();
/* */
/* This function fills the first n elements of an array with */
/* the value v. */
void assignV(double *x, int n, double v)
{
/* Declare variables. */
int i;
/* */
/* This function fills the first n elements of an array with random */
/* values. The values are between 0 and 1 but this could modified. */
double rand_float(double a, double b)
{
return ((double)rand()/RAND_MAX)*(b-a) + a;
}
void assignRandom(double *x, int n)