Menu

(Solved) : Write Program 1 Open File Projecttxt Read Data Integer Array Sample Code Open Read Data E Q37239165 . . .

You are to write aprogram to:

1). Open the file”project.txt” and read the data into an integer array. Below is thesample code to open and read data from an existing file. Note. Youcan also refer to pp 621-622 of the textbook for a sampleprogram.

#include<fstream>

… // other includesas needed

int main() {

ifstreaminFile(“project.txt”, ios::in);

// declare an integerarray of 100 elements here, I will name it the_array

// declare an integerto read data from file, I will name it the_input

int i{0};

if (!inFile) {

cerr << “Filecould not be opened” << endl;

exit(EXIT_FAILURE);

}

// to read data fromthe file to the array

while (inFile >>the_input) {

the_array[i] =the_input;

i++;

}

2). Once you have thedata stored in the array, use the bubble sort algorithm to sort thedata in ascending order. Here is the bubble sort algorithm.

int n, temp, i, j;

for (i = 0 ; i < n- 1; i++)

    {

      for (j = 0 ; j < n – i – 1;j++)

      {

           if(array[j] > array[j+1]) /* For decreasing order use < */

           {

             temp      =array[j];

             array[j]  =array[j+1];

             array[j+1]= temp;

           }

      }

}

where n is thedimension of the array, which is 100 in this case.

3). Print the inputarray to the screen (using cout <<) in nice and neat order of10 rows, 10 numbers per row.

4). Print the sortedarray in 10 rows of 10 numbers as well.

Expert Answer


Answer to You are to write a program to: 1). Open the file “project.txt” and read the data into an integer array. Below is the sam…

OR