Question

In: Computer Science

Read and print parallel array - How can this be made to read parallel arrays and...

Read and print parallel array - How can this be made to read parallel arrays and then print them?

The program presented here is intended to read from the text file and build parallel arrays. Then it will print as shown. The first line will not be stored in the array. The second line will be stored in firstArray[] and the third line will then be stored in secondArray[] and the arrays will repeat until the file is read.

begin external text file:

lineone

linetwo

line three

linefour

line five

begin program code:

ifstream infile;
string filname;
string line;
int i = 0;
cout << "Enter file name: ";
cin >> filname;
infile.open(filname.c_str());
getline(infile, line);

infile >> line; // first line
infile.ignore(20, '\n');
  
while (infile && i < 5)
{
i++;
getline(infile, firstArray[i]);
getline(infile, secondArray[i]);
}

string w;
string v;
w = firstArray[0];
v = secondArray[0];

cout << "*" << w << v << "*" << endl;
cout << "*" << firstArray[0] << "*" << endl;
cout << line << endl;

Solutions

Expert Solution

Here is the updated code, you didn't declared the arrays at the beginning also, I have assumed the size of the array to be5.

==========================================================================

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>

using namespace std;

int main(){
   string firstArray[5];
   string secondArray[5];
   ifstream infile;
   string filname;
   string line;
   int i = 0 , j = 0;
   cout << "Enter file name: ";
   cin >> filname;
  
   infile.open(filname.c_str());
   getline(infile, line);
  
   while (getline(infile,line,'\n') &&i<5 &&j<5){
   if((i+j)%2==0)  
       firstArray[i++]=line;  
   else
       secondArray[j++]=line;
  
   }
   infile.close();
   cout<<"First Array elements: \n";
   for(int k= 0; k<i; k++) cout<<firstArray[k]<<endl;
  
   cout<<"Second Array elements: \n";
   for(int k= 0; k<j; k++) cout<<secondArray[k]<<endl;
  
   return 0;
}

=================================================================

Output screenshot


Related Solutions

Define parallel arrays, tell how the data in each array is accessed and give an example...
Define parallel arrays, tell how the data in each array is accessed and give an example of parallel arrays.
1) a) Give short code example of parallel arrays. b) Explain how parallel arrays work. 2)...
1) a) Give short code example of parallel arrays. b) Explain how parallel arrays work. 2) a) How would you compare two arrays to check if they have the same values? b) Assume array1 and array2 are int arrays with 10 elements in each. if(array1 == array2) What is this comparing? 3 a) Can you encounter memory violation using an array? b) If yes explain. If no, explain why not.
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
. As input you are given two arrays: an array of numbers ? and an array...
. As input you are given two arrays: an array of numbers ? and an array ? of queries where each query is a target number. The array ? is unsorted and may contain duplicates. Your goal is, for each query ? in the array ?, count the number of pairs in the array ? that sums up to ?; that is, the number of distinct pairs of indices [?, ?], with ? < ?, such that ?[?] + ?[?]...
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to hold 5 items in each In the main(), load the arrays with data Then output the entire contents of the arrays Create a method called find1() and pass the 2 arrays to that method. problem 2 In the find1() method, ask the user to enter a value to search for Write logic to search the first array and then output the related data from...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT