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...
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...
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.
. 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 ?[?] + ?[?]...
You are given an array of arrays a. Your task is to group the arrays a[i]...
You are given an array of arrays a. Your task is to group the arrays a[i] by their mean values, so that arrays with equal mean values are in the same group, and arrays with different mean values are in different groups. Each group should contain a set of indices (i, j, etc), such that the corresponding arrays (a[i], a[j], etc) all have the same mean. Return the set of groups as an array of arrays, where the indices within...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT