Unit1c_CPP_Array_Functions.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

1c.1 CS103 Unit 1c – Arrays and Functions Mark Redekopp 1c.2 ARRAY BASICS 1c.3 Motivating Example Suppose I need to store the int main()...

1c.1 CS103 Unit 1c – Arrays and Functions Mark Redekopp 1c.2 ARRAY BASICS 1c.3 Motivating Example Suppose I need to store the int main() { grades for all students so I can int score1, score2, score3; cin >> score1 >> score2 >> score3; then compute statistics, sort them, print them, etc. // output scores in sorted order if(score1 < score2 && score1 < score3) I would need to store them in { } variables that I could access and use } – This is easy if I have 3 or 4 students int main() – This is painful if I have many { int score1, score2, score3, students score4, score5, score6, score7, score8, score9, Enter arrays score10, score11, score12, score13, score14, score15, – Collection of many variables referenced by one name score149, score150; cin >> score1 >> score2 >> score3 – Individual elements can be >> score4 >> score5 >> score6 accessed with an integer index 1c.4 Array Basics An array is a fixed size, named int main() { collection of ordered variables of int scores; // allocates 150 integers the same type that are accessed // with garbage values with an index and stored // Initialize the array contiguously in memory for(int i=0; i < 150; i++){ cin >> scores[i]; – Fixed size: Cannot grow or shrink // or scores[i] = 0; } – Named collection: One name to } refer to all variables in the array – Ordered / Accessed with an index: Addr: Index: 520 524 528 1116 Individual element (variable) is scores: 96 84 93 … 90 accessed with its position/index (using [] brackets) Computer Memory – Same Type: Variables in one array must all be the same type (one array can't store doubles and ints) 1c.5 Index vs. Value Value/data The expression in the square brackets is an index scores[2*i+1] Using array[index] yields the data/value in the array at that index index An index can be ANY EXPRESSION, even the value from an array or the return value from a function int main() For an array declared to be size n, { int scores; only indices 0 to n-1 are legal int i = 1; int x = scores[2*i + 1]; // x=8 Addr: 520 524 528 532 536 Index: int y = scores[ scores ]; // y=9 scores: int z = scores[max(4,2)]; // z=6 9 0 7 8 6 … (values) return 0; Computer Memory } 1c.6 Array Intro (1) #include #include #include using namespace std; int main() { // What are the initial values of the array? int data1; cout > num; and we'll learn more about it while( i < max_size && num != -1) { when we cover pointers) data[i] = num; i++; – Step 2: When you call the cin >> num; function, just provide the name } return i; of the array as the actual } parameter 1c.23 MORE FUNCTION DETAILS 1c.24 Function Prototypes int main() The compiler (g++/clang++) needs to { double area1,area2,area3; see a function's prototype or definition area3 = triangle_area(5.0,3.5); before it allows a call to that function X } The compiler will scan a file from top to double triangle_area(double b, double h) bottom { return 0.5 * b * h; If it encounters a call to a function } before the actual function definition it Compiler encounters a call to triangle_area() before it has seen its definition (Error!) will complain…[Compile error] double triangle_area(double, double); …Unless a prototype ("declaration") for int main() the function is provided earlier { double area1,area2,area3; A prototype only needs to include data } area3 = triangle_area(5.0,3.5); types for the parameters but not their names (ends with a ‘;’) double triangle_area(double b, double h) { – Prototype is used to check that you are return 0.5 * b * h; calling it with the correct syntax (i.e. } parameter data types & return type) Compiler sees a prototype and can check the (like a menu @ a restaurant) syntax of any following call and expects the definition later. 1c.25 The Need For Prototypes You might say: – "I don't like prototypes. I'll define each function before I call it" How would you order the functions in the program on the left if you did NOT want to use prototypes? – You can't! int f1(int x); int f2(int y); int f1(int x) { int main() return f2(x-1); { } cout

Use Quizgecko on...
Browser
Browser