Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.

Similar presentations


Presentation on theme: "Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual."— Presentation transcript:

1 Array

2 Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual elements of the array are referenced by sub_scripting the group name. – The index type is integer –In C++ the index range must be 0... n-1 where n is a programmer-defined constant expression. –Parameter passing style Always call by reference (no indication necessary)

3 Arrays Hold Multiple Values Unlike regular variables, arrays can hold multiple values. Arrays is a collection of homogeneous data type.

4 18 ShowDiff.cpp #include using namespace std; int main() { const int MAX_ITEMS = 8; float x[MAX_ITEMS]; float average,sum; int i; cout << "Enter " << MAX_ITEMS << " numbers: "; for (i = 0; i < MAX_ITEMS; i++) cin >> x[i]; // Compute the average value. sum = 0.0; for (i = 0; i < MAX_ITEMS; i++) sum += x[i]; average = sum / MAX_ITEMS; cout << "The average value is " << average <<endl <<endl; // Display the difference between each item and the average. cout << "Table of differences between x[i] and the average.\n"; cout << setw (4) << "i" << setw (8) << "x[i]" << setw (14) << "difference" << endl; for (i = 0; i < MAX_ITEMS; i++) cout << setw (4) << i << setw (8) << x[i] << setw (14) << (x[i] - average) << endl; return 0; }

5 18 ShowDiff.cpp #include void input_array ( int x[], int ); float sum_array( int x[], int ); using namespace std; int main() { const int MAX_ITEMS = 8; float x[MAX_ITEMS],average, sum = 0.0; int i; input_array(x, MAX_ITEMS); // Compute the average value. sum = sum_array(x, MAX_ITEMS); average = sum / MAX_ITEMS; cout << "The average value is " << average <<endl <<endl; // Display the difference between each item and the average. cout << "Table of differences between x[i] and the average.\n"; cout << setw (4) << "i" << setw (8) << "x[i]" << setw (14) << "difference" << endl; for (i = 0; i < MAX_ITEMS; i++) cout << setw (4) << i << setw (8) << x[i] << setw (14) << (x[i] - average) << endl; return 0; }

6 // // input_array: input data into array // void input_array ( int x[], int size ) { cout << "Enter " << MAX_ITEMS << " numbers: "; for (i = 0; i < MAX_ITEMS; i++) cin >> x[i]; } // // sum_array: sum of array // float sum_array( int x[], int size ) { float sum = 0.0; for (i = 0; i < MAX_ITEMS; i++) sum += x[i]; return sum; }

7 22 ShowDiff.cpp Program Output Enter 8 numbers: 16 12 6 8 2.5 12 14 -54.5 The average value is 2.0 Table of differences between x[i] and the average Ix[I]difference 016 14 112 10 2 6 4 3 8 6 etc

8 Parallel Arrays Parallel Arrays can be used when array variables are related to each other by position, or subscript. Tip: It is necessary that the data in each array be loaded in the correct order. If we have arrays ‘NumberGrade’ and ‘LetterGrade’ below, each element of NumberGrade is related to an element in LetterGrade by the position in the array. –NumberGrade: 90 80 70 60 50 –LetterGrade: “A” ”B” ”C” ”D” ”F”

9 String and Character Array C++ does not have a data type specifically for strings. Many C++ programmers work with strings by manipulating groups of character called character arrays. Then, string class was developed for most updated compiler package which allow you to create string object.

10 // stringex.cpp #include #include // library for string class using namespace std; int main() { string MyString;// MyString is a string object MyString = "Hello World!"; cout << MyString << '\n'; return 0; }

11 // char.cpp #include using namespace std; int main() { char myStr[30] = "Hello World!"; // myString is a character array cout << myStr << '\n'; return 0; }

12 String object and its associated function 1.You can assign the contents of one string object to another string object. 2.You can assign a string literal to a string object. 3.You can assign a character literal to a string object. string MyString1, MyString2 = “ABCDEF”; MyString1 = MyString2; MyString1 = “string literal”; MyString1 = ‘A’; If ( MyString1 == MyString2 ) // string comparison cout << MyString1;// display the content of MyString1 cout << MyString.length();// return the size of the string MyString1 = MyString1 + ‘ ‘;// string concatenation MyString1 += MyString2; getline(cin,MyString1);

13 Character array and its associated function 1.You can assign the contents of one string object to another string object unless you use strcpy function. 2.You cannot assign a character literal to a string object. 3.You cannot compare the two string with ==, instead you use strcmp or stricmp. 4.You cannot use + to perform string concatenation, instead you use strcat function. char myString1[30], myString2[30] = “ABCDEF”; strcpy(myString1,myString2);// #include myString1 = myString2; Strcpy(mString1. “string literal”); myString1[0] = ‘A’; If ( strcmp(myString1,myString2) )//string.h or cstring //return 0 if equal, //return > 0 if myString1 > myString2, //return < 0 if myString1 < myString2 cout << myString1;// display the content of MyString1 cout << myString.length();// return the size of the string strcat(myString1,” “); // #include strcat(myString1,myString2); cin.getline(myString1,31);

14 // str_end.cpp #include using namespace std; int main() { string instring; ifstream infile; // Declare file stream named infile. infile.open("strings.txt"); // Open file for input. if (infile) // If no error occurred while opening file input the data from the file. { cout << "The strings in the data file are:\n\n"; while(getline(infile,instring)) { cout << instring << endl; } else // If error occurred, display message. { cout << "An error occurred while opening the file.\n"; } infile.close(); // Close the input file. return 0; }


Download ppt "Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual."

Similar presentations


Ads by Google