Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that uses looping structures and arrays to (1) Display your

ID: 3825153 • Letter: W

Question

Write a C++ program that uses looping structures and arrays to (1) Display your ID line on the console AND write it to the output file. (2) Read and display the contents of an input file containing ten (10) one- and two-digit numbers. Create your own input file with the name “Lab24.dat” (as demonstrated in class).   (3) Test for File Open errors. Exit the program with the message “Error opening input file” if an input file open error occurs. The program must terminate if a file open error occurs. (4) Use a function to sort the array using the “bubble sort” technique demonstrated in class.      (5) Display the sorted array AND write the contents of the sorted array to an output file named (Lab24.out).   (6) Use the same “cout” messages used in the sample below. (7) Formatting – line up the “ones” column in each set of output figures (as shown below).

do NOT create the input file as part of your program. Your program will be tested using a separate input file.
*IMPORTANT: Be sure to output an identifier line in your program to be displayed on the console AND written to the output file, include appropriate comments in your program, chose meaningful identifiers, and use indentation as the programs do in your textbook.

Explanation / Answer

An array lets you declare and work with a collection of values of the same type

To display the data in array we are using loop and using iteration we are reading the data.

When an array is declared, the values of each element are not set to zero automatically.

Sample ocde is given below

File reading of input file

For reading the data in file we need to provide the fil which we need to open using the function open().

Then we can read the file contents line by line using iteration

consider C++ program, and a file named data.txt with text data.

Bubble sort program for the data

The program has an input array . This returns the array in non decreasing order using Bubble Sort algorithm.

//bubble sort of data

#include <iostream>

using namespace std;

//use a method bubSort

void bubSort (int a[], int l)

{

for (int d = 0; d < l; ++d)

    for (int e = 0; e< l - d - 1; ++e)

      if (a[e] > a[e+ 1])

     {

        int dataTemp = a[e];

        a[e] = a[e + 1];

        a[e + 1] = dataTemp;

          }

}             

int main()

{

int arrayInput[] = {5,43,755,43,67,13};

int l = sizeof (arrayInput) / sizeof (arrayInput [0]);

bubsort (arrayInput, l);

cout << "this contains the sorted data : " << endl;

for (int d = 0; d < n; ++d)

    cout << arrayInput [d] << " ";

return 0;

An array lets you declare and work with a collection of values of the same type

To display the data in array we are using loop and using iteration we are reading the data.

When an array is declared, the values of each element are not set to zero automatically.

Sample ocde is given below

       
          #include<iostream>  
          using namespace std;  
       
          int main()  
          {  
                 int g[10];  
                 int k;  
       
                 for ( k = 0; k < 10; k++ )  
                         a[i] = 0;  
                 for ( k = 0; k < 10; k++ )  
                         cout << g[k] << ' ';  
                 return 0;  
          }  

File reading of input file

For reading the data in file we need to provide the fil which we need to open using the function open().

Then we can read the file contents line by line using iteration

consider C++ program, and a file named data.txt with text data.

  /* file reading program */  
                   
  #include<iostream.h>  
  #include<conio.h>  
  #include<string.h>  
  #include<fstream.h>  
  #include<stdlib.h>  
  void main()  
  {  
          clrscr();  
          ifstream ifile;  
          char s[100], fname[20];  
          cout<<"enter the file to be read ";  
          cin>>filename;  
          ifile.open(filename);  
          if(!ifile)  
          {  
                 cout<<" Error opening input file..!!";  
                 getch();  
                 exit(0);  
          }  
          while(ifile.eof()==0)  
      {  
          ifile>>s;  
          cout<<s<<" ";  
      }  
          cout<<" ";  
          ifile.close();  
          getch();  
  }  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote