1 / 25

Introduction to C++, Homework Assignments, Data Structures

This homework assignment covers various topics in C++ including short-circuit evaluation, control structures, object destruction, and coding style.

pmelvin
Download Presentation

Introduction to C++, Homework Assignments, Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 240: Data Structures Thursday, May 31st Intro to C++, Assignments, etc

  2. From the Homework • 1) Look at example p.3 on Page 12. What is wrong with this example? • Short-circuit evaluation prevents a divide-by-zero from occurring. • What I was looking for was the lack of parentheses. • (num != 0 && sum / num) may work in C++, but order of operations may vary in other languages. • ((num != 0) && (sum / num)) is a better answer as it is portable across languages.

  3. 2) Read "Selection and Repetition Control" on Page 12. Short-circuit evaluation has the benefit of saving computation time; however, it can cause problems to not be found. How can short-circuit evaluation make testing our code harder? • Short-circuit evaluation allows us to skip the evaluation of some parts of a test. • From example p.3: • ((num != 0) && (sum / num)) • When num is 0, we don’t evaluate sum / 0. • If a problem exists with the value in sum (but num is 0, we won’t notice it).

  4. 3) Look at Example P.5 on page 15. If the computer gets to the 3rd "else" statement, what are the possible values for operator? • We don’t actually know that operator is a “char”, but we’ll assume it is. • operator can be anything other than +, - or * • When using nested ifs, it is important to know what how the if statements interact. We will go over this in more detail as part of coding style.

  5. 4) Why are objects destroyed when they leave scope? • Once something is out of scope, we can no longer use it (technically, not true – an example is deleting files). • If we don’t destroy the object then we can’t reclaim the resources it uses. • In C++, dynamic objects are not destroyed when we leave scope. This requires the programmer to handle it.

  6. 5) What is wrong with Example P.10 on page 33? Hint: It means the programmer was lazy. • Declaring the array: • string names[] = {…..} • string names[] allows you to not declare how large the array is. But, it means that you don’t know how big the array is unless you count it. • Instead, use: • string names[4] = {…}

  7. Compilation • Remember our Hello.cpp file? • Let’s relate it to Figure P.1 (page 4)

  8. #include<iostream> using namespace std; int main() { cout << “Hello!” <<endl; return 0; } Hello.o Hello.exe Machine Code: add r1, r2, r3 mov r3, r4, r1 … We don’t have any in this file But, an example would be: #include”myfile.h”

  9. Compilation • To compile code: • g++ -c file.cpp • To link code: • g++ file1.o file2.o ….. –o file.exe • We will cover makefiles in a later class.

  10. For our code we will start using multiple files to organize our code. This example will have some basic math functions. Remember, filenames are case sensitive on linux. This may be a problem if you move files from one machine to another. mathexample.cpp #include<iostream> #include”mathstuff.h” using namespace std; int main() { int first_num,second_num; cout << “Enter first number: “ <<endl; cin >> first_num; cout << “Enter second number: “ <<endl; cin >> second_num; cout << mysum(first_num,second_num) <<endl; cout << mydiff(first_num,second_num) << endl; cout << myproduct(first_num,second_num) <<endl; return 0; } Coding

  11. mathstuff.h #ifndef MATHSTUFF_H #define MATHSTUFF_H //Basic math functions //Sum, difference and product of two numbers int mysum(int first,int second); int mydiff(int first,int second); int myproduct(int first,int second); #endif mathstuff.cpp int mysum(int first, int second) { return (first+second); } int mydiff(int first, int second) { return (first-second); } int myproduct(int first, int second) { return (first*second); }

  12. To compile • g++ -c mathstuff.cpp • g++ -c mathexample.cpp • g++ mathstuff.o mathexample.o –o math.exe • To run: • ./math.exe

  13. Why so many files? • Other than for organization…. • Multiple files makes it easier to use your code with a tester. • Remember, you can only have 1 main() in your code. • To test code, you want to be able to directly access the relevant code.

  14. A sample tester: #include<iostream> #include"mathstuff.h" using namespace std; int main() { for(int i=0;i<1000;i++) { int firstval=i; int secondval=i*i; if((firstval+secondval)!=(mysum(firstval,secondval))) { cout << "Failed addition case: " << firstval << ", " << secondval <<endl; return 1; } if((firstval-secondval)!=(mydiff(firstval,secondval))) { cout << "Failed subtraction case: " << firstval << ", " << secondval <<endl; return 1; } if((firstval*secondval)!=(myproduct(firstval,secondval))) { cout << "Failed multiplication case: " << firstval << ", " << secondval <<endl; return 1; } } cout << "Tests of mysum(i,i^2), mydiff(i,i^2) and myproduct(i,i^2) for i = [0,1000) is completed successfully." << endl; return 0; }

  15. The tester has its own main(), never needs to look at your main() and tests the functions directly. • The more we decompose effectively (effectively is a bit hard to qualify) the easier it is to test our code.

  16. Testing with assert • Assertion testing is commonly used to test for sanity. • If we had a variable called mysize: • mysize (because of its name) represents the size of something. • Therefore:

  17. if(mysize<0) { cout << “Sanity failed, mysize is negative!” <<endl; return 1; } • Therefore, whenever you change mysize you should make sure it didn’t become negative. If it became negative, something is wrong!

  18. #include<iostream> #include<cassert> using namespace std; int main() { int mysize = 5; Adjustsize(mysize,4); Adjustsize(mysize,-8); Adjustsize(mysize,-4); } Adjustsize modifies mysize. In this example, not much happens. In a more complex example we could be removing items from a list or container, etc.

  19. #include<iostream> #include<cassert> using namespace std; int main() { int mysize = 5; Adjustsize(mysize,4); Adjustsize(mysize,-8); Adjustsize(mysize,-4); } mysize = 5 Add 4 items mysize = 9 Remove 8 items mysize = 1 Remove 4 items mysize = ? If mysize is –3, we have a problem. A valid Adjustsize would either: 1) remove no items (not enough items, abort) 2) remove 1 item (removed all remaining)

  20. #include<iostream> #include<cassert> using namespace std; int main() { int mysize = 5; Adjustsize(mysize,4); Adjustsize(mysize,-8); Adjustsize(mysize,-4); } We don’t know if Adjustsize works correctly so we add asserts to verify. #include<iostream> #include<cassert> using namespace std; int main() { int mysize = 5; Adjustsize(mysize,4); assert(mysize>=0); Adjustsize(mysize,-8); assert(mysize>=0); Adjustsize(mysize,-4); assert(mysize>=0); }

  21. That may be a bit excessive. • Since we are worried about mysize becoming negative. We really only need those asserts after a remove. • However: • mysize could become negative due to overflow • mysize might become larger than we can handle • This should be handled separately.

  22. The only input we’ve shown so far is taking in ints. We will take in non-int user input using the string class. Later, once we’ve covered streams we will explain c_strings. #include<iostream> #include<string> using namespace std; int main() { string userinput; cout << “Input stuff and then press enter!” <<endl; cin >> userinput; cout << “You entered: “ << userinput <<endl; return 0; } User input

  23. Now that we have the string how do we use it? The methods are similar to that of Java and C. Once we have the variable “userinput” we can access it as follows: “userinput[n]” is the nth character of the string. “userinput.length()” is the size of the input “userinput.c_str()” is a cstring of the input – you will need this to use strings with various functions. We will go over other functions later. I just want you to have the basic understanding of a string at this point. Some basic string methods

  24. #include<iostream> #include<string> using namespace std; int main() { string input; cout << "Enter some text (one word): " <<endl; cin >> input; cout << "You entered: " << input <<endl; cout << "There are " << input.length() << " letters" << endl; for(int i=0;i<input.length();i++) { cout << "Character number: " << i+1 << " is " << input[i] << " and " << input.at(i) << endl; } char * mycstring; mycstring = new char[input.length()]; for(int i=0;i<input.length();i++) { mycstring[i] = input[i]; } mycstring[input.length()] = '\0'; cout << mycstring << endl; delete mycstring; return 0; }

  25. Logical operations: • Short circuit evaluation • bitand (&) - and (&&) • bitor (|) – or(||)

More Related