CS 131- Data Structure and Algorithm (copy)

0.0(0)
studied byStudied by 2 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/74

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

75 Terms

1
New cards

-A powerful general-purpose programming language.

-an extension to the C language that can be used to develop operating systems, browsers, games, and so on.

-can support different ways of programming like procedural, object-oriented, functional, and so on,

C++

2
New cards

He developed C++

Bjarne Stroustrup

3
New cards

Why learn C++

-it is used to develop games, desktop apps,

operating systems, browsers, and so on because

of its performance.

-it is a good foundation for learning other

programming languages like Java, Python, etc.

-it will help you to understand the internal

architecture of a computer, how computer stores and retrieves information.

4
New cards

-it is a "library" in C++.

-a toolbox developed by the programmers of the C++ language which contains functions that we could use in our own program.

iostream

5
New cards

-the entrypoint of your program. If we were to imagine that our whole program is a room, the main is basically the "door" of our program.

-So when our code is executed by the computer, it'll find the ____ and will start executing all the code inside it (the ones

inside the curly braces).

main

6
New cards

-This line of code is the exit status of our application.

-If we ______ as the exit status, that means that our program worked fine until the end, without errors.

return 0;

7
New cards

special characters characterized by a backslash (\) and a letter or symbol beside it.

Escape sequences

8
New cards

std::cout << "I’m\tpretty";

I’m pretty

9
New cards

std::cout << "I’m\npretty";

I’m

pretty

10
New cards

std::cout << "\\";

\

11
New cards

std::cout << "\"";

"

12
New cards

to be able to use std::setprecision, we need to include the <_________> library

#include<iomanip>

13
New cards

this is needed to change the default formatting of the numbers that are printed to "fixed-point notation".

std::cout << std::fixed;

14
New cards

this is the line that sets the number of decimal places to be printed.

std::setprecision(3)

15
New cards

is used to place the text in the new line, but sometimes, itcan be confusing. Another way to add a new line is by using the std::endl statement.

\n

16
New cards

-objects that are used to store values.

-As an object, they act as containers or buckets

where the data will be put into and stored.

Variables

17
New cards

Variable declaration in C++ requires three parts:

● the data type of the variable;

● the variable name of your choice; and,

● a semicolon at the end

18
New cards

A letter, symbol, or white space). This should be enclosed in single quotes.

example: ‘A’;

Character (char)

19
New cards

Whole numbers ranging between ±32767.

example:

___ mynum = 6;

___ ynum = 16;

___ xnum = 1997;

Integer (int)

20
New cards

-Real numbers that can handle up to 7 decimals. To indicate that it is float, you should add an f at the end of the value.

example:

float myfloat = 1.2f;

float xfloat = 3.12f;

Floating Point Values (float)

21
New cards

Real numbers that can handle up to 15 decimals

Double is just the same as a Float but can hold larger numbers.

example:

double mynum = 100.23;

double xnum = 22.11;:

Double Floating Point Values (double)

22
New cards

Truth values (true/false).

bool myboolean = true;

bool xboolean = false;

Boolean (bool)

23
New cards

special type of variables

whose values can't be changed.

Constant variables

24
New cards

1. #include<iostream>

2. using namespace std;

3. int main(void) {

4. int letter_value = 'J';

5. cout << letter_value;

6. return 0;

7. }

What will the output be?

74

-As you can see in the example, we tried to assign the character 'J' to the variable letter_value which is an int. The output became 74 and not J, why is that?

-The character J was converted to its integer

equivalent, or the American Standard Code for

Information Interchange acronymed as ASCII value, which is 74.

- All the characters have their equivalent ASCII values. The uppercase A is 65, the uppercase B is 66. The lowercase a is 97, the lowercase b is 98.

25
New cards

ASCII

American Standard Code for Information Interchange

26
New cards

1. #include<iostream>

2. using namespace std;

3. int main(void) {

4.

5. // We create an int variable to represent sides and assign 3 to it.

6. int sides = 3;

7.

8. cout <<"A triangle has " <<sides <<" sides." <<endl;

9. cout <<"Isosceles triangle has " <<sides <<" too." <<endl;

10. return 0;

11. }

What will the output be?

A triangle has 3 sides.

Isosceles triangle has 3 sides too.

27
New cards

in your code this will not be executed because

they serve as guide to the programmers, which makes it very useful especially when making a project with lots of lines of codes.

Comments

28
New cards

There are two types of comments
//I am the 1st line of a C++ code. (What type of comment is this?)

and

1. /* I am the 1st line of a C++ code.

2. I am the 2nd line of this code

3. And I am the 3rd.*/

-Single line comments are preceded by double slash.

-Multiple line comments are enclosed in /* and */.

29
New cards

There are restrictions and guidelines to follow in

naming variables in C++.

-Variable names will only compose of letters, numbers, and/or a single symbol, underscore (_). That being said, all names must always start with a letter or an underscore (A-Z, a-z, _). But, Ma’am Fatima recommends that you should use names that are specific to what you are representing that variable for.

-Avoid using lengthy names for a variable. As much as possible, just make sure that the first three letters of its name makes sense when you read it so that it will be easy to remember and easy to type as well.

-Avoid using very short names for a variable that it won't make sense the first time you read them.

30
New cards

a number is basically increasing it by 1.

Increment

varname = varname + 1;

or
varname ++;

31
New cards

on the other hand is decreasing it by 1.

Decrement

varname = varname - 1;

or

varname --;

32
New cards

-allow the user to type in or inputa specific data that is asked by the program.
-gets a value from the user and stores it to thevariable

CIN »

33
New cards

-gets the value of the variable and prints/outputs it to the screen.

COUT «

34
New cards

-is a set of well-defined instructions to solve a particular problem

Algorithm

35
New cards

A good-quality algorithm:

-Precisely defined input and output

-clear and unambiguous steps

-most effective in solving problems

-not written in computer codes

-usable in any programming language

36
New cards

-is a storage that is used to store and organize data

-It is a way of arranging data on a computer so that it can be accessed and updated efficiently

Data structure

37
New cards

elements are arranged in sequence one after the other, which makes it easy to implement.

Linear data structures

38
New cards

elements are not in any sequence. They are arranged in a hierarchical manner where one element will be connected to one or more elements.

Nonlinear data structures

39
New cards

Why Learn DSA?

-DSA gives you an insight into how efficient it is to use each solutions.

-It also teaches you the science of evaluating the efficiency of an algorithm.

-This enables you to choose the best of various choices.

40
New cards

• It is an ordered collection of values of the same type.

• Elements can be accessed via its index.

• All arrays consist of contiguous memory locations.

ARRAY

41
New cards

It is made up of one row or one column of array members with the same name but different index values

ONE-DIMENSIONAL

42
New cards

ARRAY USE

-maintaining a large chunk of data using variable names.

-to easily sort elements

-for matrix operations

-CPU scheduling

-for implementing other data structures such as: stacks, queue, heaps,etc.

43
New cards

• Used for visiting, accessing, and printing each element of an array exactly once

• It is necessary to create a variable that will track the position of the element that is currently being accessed

ARRAY TRAVERSAL

44
New cards

• Most straightforward sorting method

• Repeatedly switches nearby components if they are in the wrong order.

• The name “bubble sort” comes from each item in the list “bubbles” up to where it should go, similar to water bubbles.

BUBBLE

45
New cards

• Works similarly to how one sorts playing cards in one’s hands.

• The array is virtually split into a sorted and an unsorted part.

• It uses in-place comparisons.

• A sub-list is kept here, which is always sorted.

• An element to be inserted in this sorted sub-list must first find its appropriate place and then insert it.

INSERTION

46
New cards

• In each iteration, the smallest element from an unsorted list is chosen and placed at the top of

the unsorted list.

• Based on in-place comparison and divides the list into two parts, one sorted and one unsorted.

SELECTION

47
New cards

• A container for a few lines of code that accomplish a specific task

• Contains the set of programming statements enclosed by { and }

• Can be called numerous times in a program for reusability and modularity

• It has its own parentheses and an opening (and closing) bracket , where variables, declarations, and parameters can be called

FUNCTION

48
New cards

Built into the language to perform operations. It is stored in Standard Function Library.

Pre-defined Functions.

49
New cards

Defined by the user to perform specific tasks.

User-defined Functions.

50
New cards

IMPORTANCE OF FUNCTION

-helps avoid repetition of the same logic

-can be called any number of times

-large programs can be easily tracked

-reusability is the key accomplishment

51
New cards

WHAT ARE THE ASPECTS OF A FUNCTION?

FUNCTION DECLARATION

FUNCTION DEFENITION

FUNCTION CALL

52
New cards

• A function must be declared globally to inform the compiler of the function name, parameters, and return type.

• This tells the compiler about a function name and how to call the function.

FUNCTION DECLARATION

53
New cards

THIS ASPECT OF FUNCTION CONTAINS THE TASKS OF THE FUNCTION WHEN CALLED. THEY ARE ENCLOSED { AND

• Contains the actual statements which are to be executed.

• The control comes to the most crucial feature when invoked.

• The function can only return one value.

FUNCTION DEFINITION

54
New cards

• When called, the function performs a defined task.

• To call a function, pass the following: required parameters and the function name.

• If the function returns a value, one can store the returned value.

• The parameter list must not differ in function calling and function declaration.

FUNCTION CALL

55
New cards

• Values which are passed in the function call.

• Can be constant, variables, expressions

ACTUAL PARAMETERS (ARGUMENTS)

56
New cards

• Variables local to that function and cannot be used in other parts of the program.

• All of the method’s required values are included in the list, along with their variable names and data types.

FORMAL PARAMETERS (REPLICA OF ACTUAL PARAMETER)

57
New cards

• Only statements that are inside that function or block of code can use them.

• They are not known to functions outside their own.

LOCAL VARIABLES (DEFINED WITHIN A FUNCTION OR BLOCK)

58
New cards

• Their values are maintained throughout the life of your program.

• It may be accessed from any of the program’s functions.

GLOBAL VARIABLES (DEFINED AT START OF THE PROGRAM)

59
New cards

THIS TYPE OF FUNCTION REFERS TO WHERE THEY CALL THEMSELVES REPEATEDLY, WHILE INPUT VALUE CHANGES EVERY TURN.
Functions that call themselves repeatedly.

RECURSIVE FUNCTIONS

60
New cards

• The function calls itself as the final statement of the function.

• When it is called, it must process or carry out some action.

• It does nothing after that call.

TAIL RECURSION

61
New cards

• The initial statement of a recursive function is also the first time it calls itself.

• At the time of calling, is not required to process anything.

• Everything is completed when the method returns.

HEAD RECURSION

62
New cards

In an array, the index starts at this value

0

63
New cards

this is used to access the elements of an array

index

64
New cards

this keyword is used if the function does not require to return a value

void

65
New cards

In order to use the different mathematical functions for complex operations, we have to include the library where those functions are contained into the code.

#include<cmath>.

66
New cards

Returns the value of the left operand (base) raised to the power of the right operand (exponent).

pow()
syntax = pow(base, exponent)

67
New cards

Returns the square root of a number.

sqrt()

syntax = sqrt(num)

68
New cards

The opposite of floor. Rounds up the number to the smallest integer greater than or equal to the number

ceil()

syntax = ceil(num)

69
New cards

Returns the absolute, positive value of num.

fabs()

syntax = fabs(num)

70
New cards

Rounds down a number to the largest

integer less than or equal to the number.

floor()

syntax = floor(num)

71
New cards

• collection of different data types under a single name

• used to express data about anything that is more complex than what can be represented by a single integer, character, or Boolean.

STRUCTURE

72
New cards

• compile multiple pieces of information on a single topic in one location

• collect data of the same data kinds and parameters

• incredibly simple to maintain

• aggregating together related data in one entity through the struct keyword allows programs to construct user-defined types.

IMPROTANCE OF STRUCTURES

73
New cards

Structures are declared by starting with the _____ keyword then the structure name (identifier).

struct

74
New cards

pk.pkname = "Pikachu";
• Use the ____ operator to access the inner variables of the structure

• One accesses the pkname field of the struct Pokemon pk and by using the _____ operator, assigning Pikachu as a string value to pkname.

. [ . ]

75
New cards

• A keyword used to specify alternative names for primitive types.

• With the application of typedef struct, variable declaration no longer needs the keyword struct.

TYPEDEF