Download presentation
Presentation is loading. Please wait.
Published bySpencer Harrell Modified over 8 years ago
1
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays Arrays can have more than one column Two dimensional arrays have two columns We specify the number of columns in a two dimensional array in the same way that we specify the number of rows int matrix[3][5];
2
23/10/2016CS150 Introduction to Computer Science 1 Initializing two dimensional arrays int matrix[3][5] = {{ 1, 2, 3, 4, 5}, { 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}; How could you read in the input for the array from the user?
3
33/10/2016CS150 Introduction to Computer Science 1 Problem Write the program segment to sum all the elements in a two-dimensional array Write the program segment to output all the elements of the array to the screen. Display the output as a matrix with the same number of rows and columns as the array
4
43/10/2016CS150 Introduction to Computer Science 1 Passing Two-Dimensional Arrays to Functions When passing two-dimensional arrays to functions you need to specify the number of columns in the formal argument list
5
53/10/2016CS150 Introduction to Computer Science 1 Problem Write the function definition of (sumRow) that is passed a two-dimensional array and returns the sum of all the rows placed in a one-dimensional array.
6
63/10/2016CS150 Introduction to Computer Science 1 Arrays and Data Types Useful for storing a collection of data elements of the same data type (float, int, string). char myName[5]; //All elements chars float salaries[NUM_EMP]; //All elements floats char vowels[]={ ‘ A ’, ’ E ’, ’ I ’, ’ O ’, ’ U ’ }; What about storing a collection of data elements of different data types?
7
73/10/2016CS150 Introduction to Computer Science 1 Data with Different Data Types For example, what if we wanted to keep the following information on a particular employee: employee id SS# number of children salary citizen The elements have different data types, so we can’t conveniently use array. Instead we will use a struct ( short for structure).
8
83/10/2016CS150 Introduction to Computer Science 1 Structure Definition To store this information: employee id SS# number of children salary citizen We would begin by defining a structure : struct employ { int id int ssnum; int numchild; float salary; bool citizen; };
9
93/10/2016CS150 Introduction to Computer Science 1 Struct Terminology For this struct: struct employ { int id int ssnum; int numchild; float salary; bool citizen; }; employ is the identifier name and a NEW data type. The individual components id, ssnum, etc. are called members.
10
103/10/2016CS150 Introduction to Computer Science 1 Struct Declaration As with all data types, in order to use our new data type employ we must allocate storage space by declaring variables of this data type: employ engineer,tech; This will allocate space for two variables called engineer and tech with the previously described members id, ssnum, etc.
11
113/10/2016CS150 Introduction to Computer Science 1 Member Access Operator To access a struct member, we use the member access operator (period between struct variable name and member name). In the variable engineer of data type employ we can make the assignments: engineer.id = 12345; engineer.ssnum = 534334343; engineer.numchild = 2; engineer.salary = 45443.34; engineer.citizen = true; How do we access the data in arrays?
12
123/10/2016CS150 Introduction to Computer Science 1 Example One Write a C++ struct data type realnum that will have members number, realpart, and intpart. Declare a variable numinfo of that type. Place the value 3.14159 in the field number.
13
133/10/2016CS150 Introduction to Computer Science 1 Example One Solution struct realnum { float number; int realpart; int intpart; }; realnum numinfo; numinfo.number=3.14159; Define struct realnum Declare variable numinfo Assign member number
14
143/10/2016CS150 Introduction to Computer Science 1 Structs as function arguments Structs can be passed to functions by reference or value in the same manner that other data types have been passed. Generally, passing structs by reference is preferred since passing by value requires a local copy of the struct be created within the function’s variables.
15
153/10/2016CS150 Introduction to Computer Science 1 Example Two Write a C++ function split that accepts a variable of type realnum. Assign the integer part of the number to the member variable intpart and the real part of the number to the member variable realpart.
16
163/10/2016CS150 Introduction to Computer Science 1 Example Two Solution Function prototype: void split(realnum &); Function call: split (numinfo); Function definition: void split(realnum & numberinfo) { // Use numberinfo.number,numberinfo.intpart, // and numberinfo.realpart. }
17
173/10/2016CS150 Introduction to Computer Science 1 Example Three Consider the following struct data type: struct info { int num; int divisors[10]; int howmany; }; Write a C++ function compute that accepts a variable of type info and returns all the divisors greater than 1 of the variable num in the array divisors and the number of divisors in the variable howmany.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.