Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name: True/False: Circle one Assignment 1 ID: 1. True / False 2. True /

Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name:

True/False: Circle one

Assignment 1

ID:

1. True / False 2. True / False 3. True / False 4. True / False 5. True / False 6. True / False 7. True / False 8. True / False 9. True / False 10. True / False

Variable and functions identifiers can only begin with alphabet and digit. Compile time array sizes can be non-constant variables. Compile time array sizes must be known at compile time. An array can have size of 0.

int m = 15.6; wont compile because the types do not match. int n = 11/5; will allocate memory for n and initialize with the value 2. Array indices begin with the number 1. You can define multiple functions with the same identifier. Variables declared local to a function are still accessible after the function completes execution. structs by default have private access specification.

11. Suppose youre tasked with fixing a function definition that does not work as intended. The function is supposed to compare two strings and set the count to the number of identical characters, two characters are identical if they are the same character and are in the same position in the cstring. This function will be case sensitive so the character a is not the same as A. Note that cstrings are just character arrays that have \0 as their last character, for example

char name[7] = "harry"; might looks like this in memory:

Usage of this function might look like (Note that the usage is correct and should not be modified):

h

a

r

r

y

\0

 int count = 0; compareCstrings("tacocat", "TACOCAT", count); compareCstrings("Harry", "Malfoy", count); compareCstrings("SMC","SBCC", count); 

Currently the function definition is:

// should set count to 0 // should set count to 1 // should set count to 2 

void compareCstrings(const char str1[], const char str2[], int count) { &count = 0;

 int index; while (str1 != '\0' || str2 != '\0') { 
 if (str1 == str2) &count++; 

index++; }

}

Rewrite the function so that it satisfies specification. Try to keep the general form of the original code, you should not have to add or remove any lines of code, just modify the existing ones.

Page 1 of 8

Santa Monica College CS 20A: Data Structures with C++ Assignment 1 Spring 2019 Name: ID:

12. Use the code below to answer the questions that follow. Assume that all proper libraries and name spaces are included and that the code will compile without error. Within the main function, for the variable int last_sid , write in the last digit of your SMC student ID.

}

}

}

int foo(int a, int b) { //First 

int c = a+b;

while(c>=3)

c-=3;

return c;

//------------------------------------

char foo(string a, int b) { //Second 

return a[b];

//------------------------------------ string foo(int b, string &a) { //Third

string sub = a.substr(3*b,3); 
a.replace(3*b,3,"..."); 

return sub;

//--------------------------------------

void main() {

int last_sid = ____; //<-Last digit of your SID

string letters("ggfiorkcboneat !!!ws adtarojot");

string output(""); 
int numbers[] = {0,8,3,7,4,6,9,1,2,5}; 
for(int i=0; i<10; i++) { 

}

int j = numbers[i]; 
numbers[i] = foo(last_sid,i); 
string s = foo(j, letters); 
output += foo(s, numbers[i]); 
cout << output; 

}

a.) What is it called when we use the same function identifier for multiple functions? What must we do to allow the compiler to differentiate between functions with the same identifier?

Page 2 of 8

Santa Monica College CS 20A: Data Structures with C++ Assignment 1 Spring 2019 Name: ID:

b.) What does the string letters look like at the end of second iteration? The blocks below are placed for your convenience, you may overwrite the values.

ggfiorkcboneat !!!ws adtarojot

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

c.) What does the numbers array look like at the end of the program? The blocks below are placed for your convenience, you may overwrite the values.

0837469125

d.) (3 points) What is printed with cout << output << endl; ?

*Extra blocks for work if needed:

ggfiorkcboneat !!!ws adtarojot

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

ggfiorkcboneat !!!ws adtarojot

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

0837469125 0837469125

Page 3 of 8

Santa Monica College CS 20A: Data Structures with C++ Assignment 1 Spring 2019 Name: ID:

13. Implement the class Tiktok discussed below. You may assume both iostream and string libraries are included, you may not use any other libraries. There are 60 seconds in a minute, 3600 seconds in an hour, 60 minutes in an hour. The 24-hour clock convention goes from 0:0:0 to 23:59:59. Read everything before starting that way you get a better sense on what member variables you need and how you want to implement the member functions. Note that what we expect to see in the console output is not necessarily what we may want to store as member variables.

It may be useful to recall that the modulus operator % can be used to obtain the remainder of integer division, also the + operator may be used to concatenate strings, and the function std::to_string(int) takes an integer and returns the string equivalent of that integer, presumably for concatenating a string with an integer.

  • Implement the default constructor which sets the initial state of Tiktok to correspond to time 0:00:00 in 24-hour clock convention (or 12:00:00 AM 12-hour clock convention).

  • addSeconds adds the number of seconds passed into the function to Tiktok. If the amount of seconds to add is negative do nothing. There is no upper limit to the amount of seconds that can be added.

  • addMinutes adds the number of minutes passed into the function to Tiktok. If the amount of minutes to add is negative do nothing. There is no upper limit to the amount of minutes that can be added.

  • addHours adds the number of hours passed into the function to Tiktok. If the amount of hours to add is negative do nothing. There is no upper limit to the amount of hours that can be added.

  • display24 prints to the console the time stored in Tiktok using 24-hour convention; with the following format: XX:XX:XX, followed by a newline.

  • display12 prints to the console the time stored in Tiktok using 12-hour convention. It will print AM or PM appropriately; with the following format XX:XX:XX XM, followed by a newline

    Below is the class definition for Tiktok, presumably in some header file. After planning out your design declare your member variables in the private field of the class.

     class Tiktok { public: 

    Tiktok();

     void addSeconds(int s); void addMinutes(int m); void addHours(int h); 
     void display24() const; void display12() const; 

    private: //TODO: Declare any member variables you think you will need.

    };

Page 4 of 8

Santa Monica College CS 20A: Data Structures with C++

Assignment 1

ID:

Spring 2019 Below is example usage for Tiktok :

 void main() { Tiktok tt; 

Name:

// Declare an instance of Tiktok

// 12:0:0 AM // 0:0:0 
// 12:59:0 AM // 0:59:0 
// 1:0:0 AM // 1:0:0 
// 11:59:59 AM // 11:59:59 
// 12:0:0 PM // 12:0:0 
tt.addHours(48); tt.display12(); tt.display24(); cout << endl; 
tt.addMinutes(59); tt.display12(); tt.display24(); cout << endl; 
tt.addSeconds(60); tt.display12(); tt.display24(); cout << endl; 
tt.addHours(10); tt.addMinutes(59); tt.addSeconds(59); tt.display12(); tt.display24(); cout << endl; 
tt.addSeconds(1); tt.display12(); tt.display24(); cout << endl; 

tt.addSeconds(11 * 3600 + 12 * 60 + 8);

//11hr*3600s/hr + 12m*60s/m + 8s = 40328s

 tt.display12(); tt.display24(); cout << endl; 
 tt.addMinutes(47); tt.display12(); tt.display24(); cout << endl; 
 tt.addSeconds(52); tt.display12(); tt.display24(); cout << endl; 

}

// 11:12:8 PM // 23:12:8 
// 11:59:8 PM // 23:59:8 
// 12:0:0 AM // 0:0:0 

Page 5 of 8

Santa Monica College CS 20A: Data Structures with C++ Assignment 1 Spring 2019 Name: ID:

Implement the member functions of Tiktok below, assume this being done in a separate source file. Be as neat and syntactically correct as possible.

a) Constructor:

b) addSeconds:

Page 6 of 8

Santa Monica College CS 20A: Data Structures with C++ Assignment 1 Spring 2019 Name: ID:

c) addMinutes:

d) addHours:

Page 7 of 8

Santa Monica College CS 20A: Data Structures with C++ Assignment 1 Spring 2019 Name: ID:

e) display24:

f) display12:

Page 8 of 8

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

Exude confidence, not arrogance.

Answered: 1 week ago

Question

All components of an array are of the same data type. True False

Answered: 1 week ago