0% found this document useful (0 votes)
30 views3 pages

Lab 21

Uploaded by

brandon jackson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

Lab 21

Uploaded by

brandon jackson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 21 Due Date: See Blackboard

Source File: ~/2336/21/lab21.cpp


Input: under control of main function
Output: under control of main function
Value: 1
Write a recursive function template whose prototype is given by
1 template<typename T>
2 const T *linearSearch(const T *array, int n, T itemToFind);
The function performs a linear search of the range [array, array + n) for itemToFind. The function
returns the smallest address i in the range [array, array + n) such that *i == itemToFind. The function
returns nullptr if no such address exists. A main function for testing your function is shown in Figure 1.
The expected output from executing this code is shown in Figure 2. To use the Makefile as distributed in
class, add a target of lab21main to targets1srcfile.

1 #include <iostream>
2 #include <string>
3

4 using namespace std;


5

6 // function template prototype


7 template<typename T>
8 const T *linearSearch(const T *array, int n, T itemToFind);
9

10 #include "lab21.cpp"
11

12 template<typename T>
13 void printArray(const T *array, int count)
14 {
15 if (count > 0)
16 {
17 cout << *array << " ";
18 printArray(array + 1, count - 1);
19 }
20 else
21 cout << endl;
22 }
23

24 template<typename T>
25 void printAndSearch(const T *array, int n, T itemToFind, string nameOfArray)
26 {
27 const T *ptr;
28

29 cout << "Array " << nameOfArray << " contains:" << endl;
30 printArray(array, n);
31 ptr = linearSearch(array, n, itemToFind);
32 cout << itemToFind;
33 if (ptr)
34 cout << " is in array " << nameOfArray << " and is located at index "
35 << ptr - array << endl << endl;
36 else
37 cout << " is not in array " << nameOfArray << endl << endl;
38 }
39

Figure 1. /usr/local/2336/src/lab21main.C (Part 1 of 2)

CS 2336 — Data Structures and Algorithms Page 1


Lab 21 Due Date: See Blackboard

40 int main()
41 {
42 const int aCount = 5, bCount = 7, cCount = 7, dCount = 12;
43 int a[aCount] = {5, 5, 5, 5, 5};
44 double b[bCount] = {7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1};
45 char c[cCount] = {’r’, ’a’, ’c’, ’e’, ’c’, ’a’, ’r’};
46 string d[dCount] = {"Cadillac", "Oldsmobile", "Chevrolet",
47 "Toyota", "Lexus", "Dodge", "GMC", "BMW",
48 "BMW", "GMC", "Dodge", "Lexus"};
49

50 printAndSearch(a, aCount, 5, "a");


51 printAndSearch(a, aCount, 2, "a");
52 printAndSearch(b, bCount, 1.1, "b");
53 printAndSearch(b, bCount, 11.11, "b");
54 printAndSearch(c, cCount, ’e’, "c");
55 printAndSearch(c, cCount, ’E’, "c");
56 printAndSearch(d, dCount, static_cast<string>("Lexus"), "d");
57 printAndSearch(d, dCount, static_cast<string>("Mercedes"), "d");
58

59 return 0;
60 }

Figure 1. /usr/local/2336/src/lab21main.C (Part 2 of 2)

1 newuser@csunix ~> cd 2336


2 newuser@csunix ~/2336> ./getlab.ksh 21
3 * Checking to see if a folder exists for Lab 21. . .No
4 * Creating a folder for Lab 21
5 * Checking to see if Lab 21 has sample input and output files. . .Yes
6 * Copying input and output files for Lab 21
7 from folder /usr/local/2336/data/21 to folder ./21
8 * Checking to see if /usr/local/2336/src/lab21main.C exists. . .Yes
9 * Copying file /usr/local/2336/src/lab21main.C to folder ./21
10 * Checking to see if /usr/local/2336/include/lab21.h exists. . .No
11 * Copying file /usr/local/2336/src/Makefile to folder ./21
12 * Adding a target of lab21main to targets1srcfile
13 * Touching file ./21/lab21.cpp
14 * Edit file ./21/lab21.cpp in Notepad++
15 newuser@csunix ~/2336> cd 21
16 newuser@csunix ~/2336/21> ls
17 01.out Makefile lab21.cpp lab21main.C
18 newuser@csunix ~/2336/21> make lab21main
19 g++ -g -Wall -std=c++11 -c lab21main.C -I/usr/local/2336/include -I.
20 g++ -o lab21main lab21main.o -L/usr/local/2336/lib -lm -lbits

Figure 2. Commands to Compile, Link, & Run Lab 21 (Part 1 of 2)

Page 2 CS 2336 — Data Structures and Algorithms


Lab 21 Due Date: See Blackboard

21 newuser@csunix ~/2336/21> ./lab21main


22 Array a contains:
23 5 5 5 5 5
24 5 is in array a and is located at index 0
25

26 Array a contains:
27 5 5 5 5 5
28 2 is not in array a
29

30 Array b contains:
31 7.7 6.6 5.5 4.4 3.3 2.2 1.1
32 1.1 is in array b and is located at index 6
33

34 Array b contains:
35 7.7 6.6 5.5 4.4 3.3 2.2 1.1
36 11.11 is not in array b
37

38 Array c contains:
39 r a c e c a r
40 e is in array c and is located at index 3
41

42 Array c contains:
43 r a c e c a r
44 E is not in array c
45

46 Array d contains:
47 Cadillac Oldsmobile Chevrolet Toyota Lexus Dodge GMC BMW BMW GMC Dodge Lexus
48 Lexus is in array d and is located at index 4
49

50 Array d contains:
51 Cadillac Oldsmobile Chevrolet Toyota Lexus Dodge GMC BMW BMW GMC Dodge Lexus
52 Mercedes is not in array d
53

54 newuser@csunix ~/2336/21> ./lab21main > my.out


55 newuser@csunix ~/2336/21> diff 01.out my.out
56 newuser@csunix ~/2336/21>

Figure 2. Commands to Compile, Link, & Run Lab 21 (Part 2 of 2)

CS 2336 — Data Structures and Algorithms Page 3

You might also like