You are on page 1of 11

2-Dimensional Arrays Data that is in rows and columns is usually stored in 2-dimensional arrays.

Declaring of 2-dimensional arrays Two-dimensional arrays are declared by specifying the number of rows then the number of columns. int a[30][10]; // declares an int array of 30 rows and 10 columns. char ticTacToeBoard[3][3]; // three rows and three columns of chars. Initializing 2-dimensional arrays Unless specified, all initial values of arrays are garbage. You can specify initial values by enclosing each row in curly braces like this: char ticTacToeBoard[3][3] = {{'x', 'x', 'o'}, {'o', 'o', 'x'}, {'x', 'o', ' '} }; If some elements are omitted in the initialization list, they are set to zero. Subscripting 2-dimensional arrays Write subscripts as x[row][col]. Passing over all elements of a two-dimensional array is usually done with two nested for loops. // clear the board for (int row=0; row<3; row++) { for (int col=0; col<3; col++) { ticTacToeBoard[row][col] = ' '; } } Passing 2-dimensional arrays as parameters C++ doesn't care about bounds, but it needs to compute the memory address given the subscripts (see below). To do this it needs to know the row width (number of columns). Therefore formal 2-dimensional array parameters must be declared with the row size, altho the number of rows may be omitted. For example, void clearBoard(ticTacToeBoard[][3]) { . . . } Arrays of Arrays The most common use of arrays of arrays in C/C++ is arrays of C-strings. Array of C-strings An array of C-strings is an array of arrays. Here is an example. char* days[] = {"Mon", "Tue", "Wed", "Thu", "Fri"}; In the above days array each element is a pointer to a string, and they don't have to be the same size. For example, char * greetings[] = {"Hello", "Goodbye", "See you later"}; Parameters to main When the main function is called, it is passed the name of the program that is being run and all the tokens (words) on the remainder of the line that was used to run the program. These are passed to the main as an array of C-strings and the number of elements in the array. Here is a main program that prints these parameters. #include <iostream> using namespace std; int main(int argc, char* argv[]) { for (int i=0; i<argc; i++) { cout << argv[i] << endl; } return 0; } Array of pointers or pointer to pointer Because arrays and pointers are so interchangeable, you can write the header of main like this also: int main(int argc, char* argv[]) { ... Introduction to Two-Dimensional Arrays Two-dimensional arrays, the most common multidimensional arrays, are used to store information that we normally represent in table

form. Two-dimensional arrays, like one-dimensional arrays, are homogeneous. This means that all of the data in a two-dimensional array is of the same type. Examples of applications involving two-dimensional arrays include: a seating plan for a room (organized by rows and columns), a monthly budget (organized by category and month), and a grade book where rows might correspond to individual students and columns to student scores. Declaration of Two-Dimensional Arrays Example: The following declarations set aside storage for a two-dimensional array called labScores which contains 40 rows and 14 columns. Rows correspond to a particular student and columns correspond to a particular lab score. const int MAX_STUDENTS=40; const int MAX_LABS=14; int labScores [MAX_STUDENTS][MAX_LABS]; Exercise 1: Show C++ statements to declare the following two-dimensional array: Declare a two-dimensional array which can be used to store a yearly budget. Each row of the array corresponds to a particular budgeted item like rent, electric, etc. There are at most 15 items to be budgeted. Each column of the array corresponds to a month, January, February, etc. Of course there are 12 columns corresponding to the 12 months of the year. All the data to be placed in the array consists of real numbers. Manipulation of a two-dimensional array requires the manipulation of two indices. When the two-dimensional array labScores is declared, enough storage is set aside to hold a table containing 40 rows and 14 columns for a total of 40 * 14 = 560 integer values. To access one particular value, we must specify the row and column. The row index ranges from 0 to MAX_STUDENTS1 (39) and the column index ranges from 0 toMAX_LABS-1 (13). Thus the table can be visualized as:

This two-dimensional array may also be visualized as a one-dimensional array of arrays. An alternative view of this array would be:

This two-dimensional array may be viewed as a one-dimensional array having 40 elements where each element is an array of 14 values. Accessing a Two-Dimensional Array Element In our labScores example, suppose we wish to indicate that the second student (corresponding to row 1) made a 90 on lab 10 (corresponding to column 9). We might use the statement: labScores [1][9] = 90; Array indices may be integer constants (as in the above example), variables, or expressions. They should be within the bounds of the array. Two-Dimensional Array Initialization We can declare and initialize an array A as follows: //declaration int A[3][4] = {{8, 2, 6, 5}, {6, 3, 1 ,0}, {8, 7, 9, 6}}; //row 0 //row 1 //row 2

Memory for the array may be visualized as:

Exercise 2: a. After the final answer sheet is printed, draw an alternative visualization of memory for the array A as we did above for our lab scores example. b. What value is stored in row index 2, column index 1? c. Give the name of the location where the value 0 is stored? We may also initialize a 2-D array by reading data. Suppose we wish to read entries into our labScores array. We should use two loops--one to control the student (row) and one to control the lab (column). To read in all labs corresponding to the first student, then read in all labs corresponding to the second student, and so on, we might use the following function: //Function: ReadScores() //Purpose: This function inputs lab scores for // students in a computing class. // void ReadScores(int labScores [MAX_STUDENTS][MAX_LABS], int& numStudents, int& numLabs, ifstream& myin) { //local variables int student, lab; //index for the student and //first read the number of students and labs //stored in the file. myin >> numStudents >> numLabs; //Outer loop controls which student (row) is being read for (student = 0; student < numStudents; student++) { //Inner loop controls which lab (column) is being read for (lab = 0; lab < numLabs; lab++) myin >> labS [student][lab]; } return; } //OUT: //OUT: //OUT: //IN: student labs actual # of students actual # of labs input file stream

lab being processed

Exercise 3: Suppose we wish to read in the data for our students' scores but the file is organized differently. Instead of all of one student's labs appearing first, the file has all grades on lab 1 first, then all grades on lab 2, etc. How must the code above be changed to accommodate this new arrangement of data? Explain the difference on the answer sheet. Two-Dimensional Arrays as Function Parameters As with one-dimensional arrays, a two-dimensional array is automatically passed as a pass-by-reference parameter. Consider the function heading above. The following function heading also could have been used: void ReadScores (int labScores[][MAX_LABS], int& numStudents, int& numLabs, ifstream& myin); //OUT: //OUT: //OUT: //IN: student labs Number of students Number of labs Input file stream

Notice the difference between these two function headings. For the labsScores array, the second heading does not include the number of rows required by the array but does include the number of columns. The explanation is simple -- when we pass an array the

compiler need only know the size of its elements, not how many elements it has. As explained above, a two-dimensional array can be thought of as a one-dimensional array of elements (which just happen to be arrays themselves). Thus the compiler needs to know the size of one of the elements. Each element is a row of values so the compiler needs to know the type of elements and how many values (the number of columns) are in each row. In the labScores array, the number of columns is MAX_LABS. If one wants to print the entire set of scores, a nested loop similar to that above can be used.

Exercise 4: Copy the program cla14a.cc and the data file labscores.dat to your account. This program contains declarations for the labScores array and contains a function which reads in data into this array from the filelabscores.dat. Make sure that you understand the code included in the program. Add a function to print the scores so that each student's labs appear on a separate line of output. Include a statement in your main program to call this function. Your output should be labeled as follows: Student 1: Student 2: Student 3: . . 80 98 85 90 70 100 85 100 99 60 25.... 60 89 90 90 85 72 78 0 93 78 80 70 98 100 98 65 89 0 94 56

Compile and run your program to make sure it is error free. Two-Dimensional Array Processing Processing of two-dimensional arrays might include finding an average of a row or a column. Suppose in our lab scores problem, we wish to determine the lab average for the third student (remember, this means row 2). The solution to this problem consists of finding the sum of all of the entries in row index 2 and dividing by the number of labs, NUM_OF_LABS. 15 16 17 18 19 20 21 sum = 0; student = 2; // 3rd student for (lab = 0; lab < NUM_OF_LABS; lab++) sum += labScores[student][lab]; average = float(sum)/NUM_OF_LABS; cout << "The average for student " << student+1 << " is " << average << endl;

In general, if we wished to determine the average for the Kth student, then K-1 would replace the value 2 in statement 16 above. If we wished to find an average for all 40 students, we can add an outer loop to control the student index.

Exercise 5: Add a function to your copy of cla14a.cc, say StudentAvg(), which finds and prints the lab average for each student in the class. A function prototype for this function is shown below. Activate this function from the main program. Compile and run your program. void StudentAvg(int labScores [][MAX_LABS], int numStudents, int numLabs) //IN: //IN: //IN: Lab scores # of students in the class # of labs recorded per student

Exercise 6: Add a function to cla14a.cc, say labAvg(), which finds and prints the average score made on each individual lab. Activate this function from the the main program. Compile and run your program. Turn in a script log of a listing, compile, and run.

cla14a.cc 1 2 3 //File: //Author: //Purpose: clab14a.cc This program reads data for a computer science

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

// //Input: // // // // // // // //Limitations: // //

closed lab section. Input for this program is from the external data file labscores.dat. The first two entries in the file are the number of students in the class and the number of closed labs the class has finished. For each student, their closed labs are input from the following lines of the file. It is assumed that there will be no more than MAX_STUDENTS students in the class and there will be no more than MAX_LABS labs.

//include files... #include <iostream> #include <fstream> using namespace std; //global constants... const int MAX_STUDENTS=40; const int MAX_LABS=14; //maximum number of students //maximum number of closed labs

//function prototypes... //function to read labs from the data file void ReadScores(int labScores[][MAX_LABS], int& numStudents, int& numLabs, ifstream& myin); //your function prototypes should go here!!! int main() { //local declarations... int numStudents; int numLabs; ifstream myin; int labScores[MAX_STUDENTS][MAX_LABS];

//how many students are in the class //how many closed labs //input file stream //holds lab scores

myin.open ("labscores.dat"); //open the file for input if (!myin) { cout << "Could not open labscores.dat\n"; return 1; } //read in the data for all students in the class ReadScores(labScores, numStudents, numLabs, myin); //print the data for all students in the class //your print function should be called here!!!!! //find each student's lab average -- use the StudentAvg() //function here. //find and print the average score made on each individual lab //use your LabAvg() function here //end of main... return 0; }

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 read 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

//Function: //Purpose: // // // // // //Assumption: //

ReadScores() This function reads data for students in a closed lab class. Data is read from the input file stream myin. The number of students in the lab and the number of closed labs finished by each student are read first. Next, for each student, their closed labs are read into the two-d array labscores. MAX_LABS is a global constant which has been defined previously. //OUT: Holds the lab scores //OUT: # of students //OUT: Number of Labs //IN: Input file stream

void ReadScores(int labScores[][MAX_LABS], int& numStudents, int& numLabs, ifstream& myin) { //local declarations... int student; int lab;

//controls which student's labs are //controls which lab is being read

//get the number of students in the class myin >> numStudents >> numLabs; //outer loop controls which student(row) is being read for (student = 0; student < numStudents; student++) { //inner loop controls which lab(column) is being read for (lab = 0; lab < numLabs; lab++) myin >> labScores[student][lab]; } return; } //Place the definition of your function to print the scores here //Place the definition of the StudentAvg() function here //Place the definition of the LabAvg() function here labscores.dat

1 2 3 4 5 6

5 14 10 9 8 8 6 6 8 8 9 9

7 8 0 8 9

8 10 9 4 5 7 7 7 9 9

9 8 9 7 9

9 9 4 9 9

9 10 8 8 10 9 7 8 3 10 10 7 7 8 5 7 1 5 9 9 9 8 9 10 9 9 10 9 9 9

9 9 9 8 9

TWO DIMENSIONAL ARRAY


It is a collection of data elements of same data type arranged in rows and columns (that is, in two dimensions).

Declaration of Two-Dimensional Array

Type arrayName[numberOfRows][numberOfColumn]; For example,int Sales[3][5];

Initialization of Two-Dimensional Array


An two-dimensional array can be initialized along with declaration. For two-dimensional array initialization, elements of each row are enclosed within curly braces and separated by commas. All rows are enclosed within curly braces.int A[4][3] = {{22, 23, 10}, {15, 25, 13}, {20, 74, 67}, {11, 18, 14}};

Referring to Array Elements

To access the elements of a two-dimensional array, we need a pair of indices: one for the row position and one for the column position. The format is as simple as: name[rowIndex][columnIndex] Examples:cout<<A[1][2]; //print an array element A[1][2]=13; // assign value to an array element cin>>A[1][2]; //input element int mat[3][5], row, col ; for (row = 0; row < 3; row++) for (col = 0; col < 5; col++) cin >> mat[row][col];

Using Loop to input an Two-Dimensional Array from user

Arrays as Parameters

Two-dimensional arrays can be passed as parameters to a function, and they are passed by reference. When declaring a two-dimensional array as a formal parameter, we can omit the size of the first dimension, but not the second; that is, we must specify the number of columns. For example: void print(int A[][3],int N, int M)In order to pass to this function an array declared as: int arr[4][3];we need to write a call like this: print(arr); Here is a complete example: #include <iostream.h> void print(int A[][3],int N, int M) { for (R = 0; R < N; R++) for (C = 0; C < M; C++) cin >> A[R][C]; } int main () { int arr[4][3] ={{12, 29, 11}, {25, 25, 13}, {24, 64, 67}, {11, 18, 14}}; print(arr,4,3); return 0; }

Function to read the array A

Function to display content of a two dimensional array A

void Read(int A[][20], int N, int M) { for(int R=0;R<N;R++) for(int C=0;C<M;C++) { cout<<"(R<<','<<")?"; cin>>A[R][C]; } }

void Display(int A[][20],int N, int M) { for(int R=0;R<N;R++) { for(int C=0;C<M;C++) cout<<setw(10)<<A[R][C]; cout<<endl; } }

Function to find the sum of two dimensional arrays A and B


void Addition(int A[][20], int B[][20],int N, int M) { for(int R=0;R<N;R++) for(int C=0;C<M;C++) C[R][C]=A[R][C]+B[R][C]; }

Function to multiply two dimensional arrays A and B of order NxL and LxM
void Multiply(int A[][20], int B[][20], int C[][20],int N, int L, int M) { for(int R=0;R<N;R++) for(int C=0;C<M;C++) { C[R][C]=0; for(int T=0;T<L;T++) C[R][C]+=A[R][T]*B[T][C]; } }

Function to find & display sum of rows & sum of cols. of a 2 dim. array A
void SumRowCol(int A[][20], int N, int M) { for(int R=0;R<N;R++) { int SumR=0; for(int C=0;C<M;C++) SumR+=A[R][C]; cout<<"Row("<<R<<")="<<SumR<<endl; } for(int R=0;R<N;R++) { int SumR=0; for(int C=0;C<M;C++) SumR+=A[R][C]; cout<<"Row("<<R<<")="<<SumR<<endl; } }

Function to find sum of diagonal elements of a square matrix A


void Diagonal(int A[][20], int N, int &Rdiag, int &LDiag) { for(int I=0,Rdiag=0;I<N;I++) Rdiag+=A[I][I]; for(int I=0,Ldiag=0;I<N;I++) Ldiag+=A[N-I-1][I]; } void Transpose(int A[][20], int B[][20],int N, int M) { for(int R=0;R<N;R++) for(int C=0;C<M;C++) B[R][C]=A[C][R]; }

Function to find out transpose of a two dimensional array A

C-STRINGS (Character Arrays)


STRING: It is an array of type char.

Syntax for declaration

char <array/string name> [max. number of characters to be stored +1];

The number of elements that can be stored in a string is always n-1, if the size of the array specified is n. This is because 1 byte is reserved for the NULL character '\0' i.e. backslash zero. A string is always terminated with the NULL character. Example:char str[80];In the above example, str can be used to store a string with 79 characters.

Initializing a string

A string can be initialized to a constant value when it is declared. char str[ ] = "Good"; Orchar str[]={'G','o','o','d','\0'}; Here. 'G' will be stored in str[0], 'o' in str[1] and so on. Note: When the value is assigned to the complete string at once, the computer automatically inserts the NULL character at the end of the string. But, if it is done character by character, then we have to insert it at the end of the string.

Reading strings with/without embedded blanks Printing strings

To read a string without blanks cin can be usedcin>>str;To read a string with blanks cin.getline() or gets() can be used.cin.getline(str,80); gets(str);

-Or-

cout and puts() can be used to print a string.cout<<str: Or puts(str); Note: For gets( ) and puts(), the header file stdio.h has to be included. puts() can be used to display only strings. It takes a line feed after printing the string. cin gets()

It can be used to take input of a value of any data type.

It can be used to take input of a string.

It takes the white space i.e. a blank, a tab, or a new line character as a string terminator.

It does not take the white space i.e. a blank, a tab, or a new line character, as a string terminator.

It requires header file iostream.h Example: char S[80]; cout<<"Enter a string:; cin>>S;

It requires the header file stdio.h Example: char S[80]; cout<<"Enter a string:"; gets(S);

cout

puts()

It can be used to display the value of any data type.

It can be used to display the value of a string.

It does not take a line feed after displaying the string.

It takes a line feed after displaying the string.

It requires the header file iostream.h Example: char S[80]="Computers"; cout<<S<<S; Output: ComputersComputers

It requires the header file stdio.h Example: char S[80]="Computers"; puts(S); puts(S); Output: Computers Computers

Counting the number of characters in a string and printing it backwards


#include<iostream.h> #include<stdio.h> int main( ) { char str[80]; cout<<"Enter a string:"; gets(str); for(int l=0; str[l]!='\0';l++); //Loop to find length cout<<"The length of the string is : "<<l<<endl ; for(int i=l-1;i>=0;i--) //Loop to display the string backwards cout<<str[i]; return 0; } void count(char S[]) { int words=0; for(int i=0;S[i]!='\0';i++)

Function to count the number of words in a string

{ if (S[i]==' ') words++; //Checking for spaces

} cout<<"The number of words="<<words+1<<endl;

Function to find the length of a string


int length(char S[ ]) { for(int i=0;S[i]!='\0';i++); return i; }

Function to copy the contents of string S2 to S1


void copy(char S1[ ], char S2[ ]) { for(int i=0;S2[i]!='\0';i++) S1[i]=S2[i]; S1[i]='\0';

Function to concatenate the contents of string S2 to S1


void concat(char S1[ ], char S2[ ]) { for(int l=0;S1[l]!='\0';l++); for(int i=0;S2[i]!='\0';i++) S1[l++]=S2[i]; S1[l]='\0';

Function to compare strings STR1 to STR2. The function returns a value>0 if //STR1>STR2, a value<0 if STR1<STR2, and value 0 if STR1=STR2
int compare(char STR1[ ],char STR2[]) { for(int I=0;STR1[I]==STR2[I] && STR1[I]!='\0'&&STR2[I]!='\0'; I++); return STR1[I]-STR2[I];

To reverse the contents of string S and store it in string Rev


void Reverse(char S[], char Rev[]) { for(int C1=0; S[C1]!='\0'; C1++); C1--; for(int C2=0;C1>=0;C2++,C1--) Rev[C2]=S[C1]; Rev[C2]='\0'; }

Function to check whether a string S is a palindrome or not


int Palin(char S[]) { for(int L=0;S[L]!='\0';L++); //To find length for(int C=0;(C<L/2) && (S[C]==S[L-C-1]);C++); return (C==L/2)?1:0; //Returns 1 if Palindrome else 0 } void Upper(char S[]) { for(int i=0;S[i]!='\0';i++) S[i] = (S[i]>='a' && S[i]<='z')?(S[i]-32):S[i]; } void Lower(char S[]) { for(int i=0;S[i]!='\0';i++) S[i] = (S[i]>='A' && S[i]<='Z')?(S[i]+32):S[i]; }

Function to change the case of string S to uppercase

Function to change the case of string S to lower case

Function to extract n characters from left side of the string and store it in a different string. Example: 4 characters from ENVIRONMENT=ENVI
int SLeft(char S[ ], int n, char result[ ]) { for(int l=0;S[l]!='\0';l++); if(n<=I) //characters extracted should be <=length { for(int i=0;i<n;i++)

result[i]=S[i]; result[i]='\0'; return 1; } else return 0;

Function to extract n characters from right side of the string and store it in a different string. Example: 4 characters from ENVIRONMENT=MENT
int SRight(char S[ ], int n, char result[ ]) { for(int l=0;S[l]!='\0';l++); if(n<=I) //characters extracted should be <=length { for(int j=0;i=l-n;S[i]!=/0;i++,j++) result[j]=S[i]; result[j]='\0'; return 1; } else return 0;

Function to extract n characters from specified location loc of the string and store it in a different string. Example: 4 characters from third location in string ENVIRONMENT= VIRO
int substring(char S[ ], int n, int loc, char result[ ]) { for(int l=0;S[l]!='\0';l++); if(n<=I) //characters extracted should be <=length { for(int j=0;i=l-n;S[i]!=/0;i++,j++) result[j]=S[i]; result[j]='\0'; return 1; } else return 0; }

You might also like