Parallel Arrays Parallel array Two or more arrays

![Parallel Arrays Example: int id[NUM_STUDENTS]; double gpa[NUM_STUDENTS]; id[0] if[1] id[2] id[3] id[4] id[5] 3423 Parallel Arrays Example: int id[NUM_STUDENTS]; double gpa[NUM_STUDENTS]; id[0] if[1] id[2] id[3] id[4] id[5] 3423](https://slidetodoc.com/presentation_image/ce6c8304e67e416ad1e6fb7e99236a43/image-2.jpg)





- Slides: 7

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.
![Parallel Arrays Example int idNUMSTUDENTS double gpaNUMSTUDENTS id0 if1 id2 id3 id4 id5 3423 Parallel Arrays Example: int id[NUM_STUDENTS]; double gpa[NUM_STUDENTS]; id[0] if[1] id[2] id[3] id[4] id[5] 3423](https://slidetodoc.com/presentation_image/ce6c8304e67e416ad1e6fb7e99236a43/image-2.jpg)
Parallel Arrays Example: int id[NUM_STUDENTS]; double gpa[NUM_STUDENTS]; id[0] if[1] id[2] id[3] id[4] id[5] 3423 1254 4532 2341 1242 5673 gpa[0] gpa[1] gpa[2] gpa[3] gpa[4] gpa[5] 3. 56 3. 65 2. 89 1. 99 2. 77 3. 45

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

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

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

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 in. File; ofstream out. File; in. File. open("c: input. dat");

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