Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:

Similar presentations


Presentation on theme: "Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:"— Presentation transcript:

1 Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example: int id[NUM_STUDENTS]; double gpa[NUM_STUDENTS]; The array id contains the id for each student. The array gpa contains the gpa for each student. id[0] represents the id for the first student. gpa[0] represents the gpa for the first student. The arrays are called parallel arrays because the data stored in id[i] and gpa[i] relate to the ith student.

2 Parallel Arrays Example: int id[NUM_STUDENTS];
double gpa[NUM_STUDENTS]; id[0] if[1] id[2] id[3] id[4] id[5] 3423 gpa[0] gpa[1] gpa[2] gpa[3] gpa[4] gpa[5] 3.56 1254 3.65 4532 2.89 2341 1.99 1242 2.77 5673 3.45

3 Parallel Arrays Assignment # 13: Ref. Election program posted on the Internet/ textbook. Parallel Arrays are: string name[100]; int age[100];

4 How to get the first character of a string
#include <iostream> #include <string> int main() { string inputStr; cin >> inputStr; // if you enter john, your program will display j cout << inputStr . substr(0,1) <<endl; return 0; }

5 How to get the first character of a string from an array of strings
#include <iostream> #include <string> int main() { string inputStr[]={“John", "Robin"};; cout << inputStr[0] . substr(0,1) <<endl; return 0; }

6 How to get input from file and write to a file
#include <iostream> #include <string> #include <fstream> #include <iomanip> int main() { string name[2]; ifstream inFile; ofstream outFile; inFile.open("c:\input.dat");

7 Continued if (!inFile) { cout << "Can't open it"; return 1; }
outFile.open("c:\output.dat"); inFile >> name[0] >> name[1]; //cout << name[0] <<endl; //cout << name[1] <<endl; outFile << name[0] << " "<< name[1]; return 0;


Download ppt "Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:"

Similar presentations


Ads by Google