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

CSE30 lab5.pdf ks Help 116.74 116.74 Anything that keeps your style consistent (

ID: 3671099 • Letter: C

Question

CSE30 lab5.pdf ks Help 116.74 116.74 Anything that keeps your style consistent (Exercise 1) Create -filelO.cpp in this part of the lab you will write a program that does the following operations: Reads words from a file (words_ in.txt), with one word per line. Creates an array, using dynamic memory allocation, large enough to contain those words (one word per array element). Output the words (array elements) to another file (words out.txt). Before storing the words from a file in an array, you will need to first find out how many lines there are in the file: one way to do it is-to check with a counter when you reach the end of the file, by using the function eof(). eof() returns true (1) if the end of file is reached, otherwise a false (0) is returned. You can review the use of eof() at: http://mathbits.com/MathBits/CompSci/Files/End.htm Once you have found out how many lines there are in the file, you can create a dynamically allocated array of strings, which is as large as the number of words contained in the file (as per the counter you have implemented). You can review the syntax to declare arrays with dynamic memory allocation (pointer/ new) in the lecture slides or at cplusplus.com. Finally, you will write each word (array element) into the output file, one word per line. Note: You do not need to declare an array with a pre-defined constant size nor ask the user to input the number of elements. Make sure that you create an input file with only one word per line. . RUE

Explanation / Answer

//Exercise-1
/**C++ program that reads input text file "words_in.txt
and checks whether file exists or not. If the file not exists,
then terminate the program. Then reads the number of words in the file.
And then open a output text file and "words_out.txt " and write
the file to the "words_out.txt"
"*/

//header files
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//starting point of the progrma execution
int main()
{


   //file name
   char fileName[40]="words_in.txt";

   //Create an ifstream object
   ifstream fin;
   fin.open(fileName);

   //Check if file exist
   if(!fin)
   {
       cout<<"File doesnot exist. Terminate the program..."<<endl;
       system("pause");
       return 0;
   }

   string word;
   int counter=0;

   //read number of words in the file
   while(!fin.eof())
   {
       fin>>word;
       counter++;
   }
   //close the fin
   fin.close();


   //Opem the file to read words again
   fin.open(fileName);


   //Create an ofstream object fout to write words to file "words_out.txt"
   ofstream fout;

   fout.open("words_out.txt");
  
   string *words=new string[counter];

   while(!fin.eof())
   {
       fin>>word;
       fout<<word<<endl;      
   }

   //close the ifstream and ofstream
   fin.close();
   fout.close();


   //pause the program output on console
   system("pause");
   return 0;
}

sample input file
words_in.txt

hi
hello
how
are
you

words_out.txt
hi
hello
how
are
you

---------------------------------------------------------------------------------------------------------------------------------------
//Exercise-2

/**C++ program that tests the method checkArraySort method
and print the results to console*/
//header files
#include<iostream>
#include<string>
using namespace std;
int checkArraySort(int *ptr,int);
//starting point of the progrma execution
int main()
{


   int arr[5]={1,8,3,4,5};

   int *ptr=arr;

   int result=checkArraySort(ptr,5);

   for(int index=0;index<5;index++)
       cout<<*(ptr+index)<<" ";

   if(result==1)
       cout<<"Ascending Order "<<endl;
   else if(result==-1)
       cout<<"Descending Order "<<endl;
   else
       cout<<"NO Order "<<endl;

   int arr2[]={1,2,3,4,5};

   ptr=arr2;

   for(int index=0;index<5;index++)
       cout<<*(ptr+index)<<" ";

   result=checkArraySort(ptr,5);

   if(result==1)
       cout<<"Ascending Order "<<endl;
   else if(result==-1)
       cout<<"Descending Order "<<endl;
   else
       cout<<"NO Order "<<endl;


   int arr3[]={5,4,3,2,1};

   ptr=arr3;

   for(int index=0;index<5;index++)
       cout<<*(ptr+index)<<" ";

   result=checkArraySort(ptr,5);

   if(result==1)
       cout<<"Ascending Order "<<endl;
   else if(result==-1)
       cout<<"Descending Order "<<endl;
   else
       cout<<"NO Order "<<endl;

   system("pause");
   return 0;

}


/**The method checkArraySort that takes integer pointer and size
as arguments and retunrs 1 for ascending, -1 for descending or 0 for not in order*/
int checkArraySort(int *ptr,int size)
{
   int ascending=0;
   int descending=0;
   int order=0;

   bool repeat=true;

   for(int index=0;index<size-1 && repeat;index++)
   {
       if(ptr[index]<ptr[index+1])
           ascending=1;
       else
       {
           ascending=0;
           repeat=false;
       }
   }

   repeat=true;
   if(ascending!=1)
   {
       for(int index=0;index<size-1 && repeat;index++)
       {
           if(ptr[index]>ptr[index+1])
               descending=-1;
           else
           {
               descending=0;
               repeat=false;
           }
       }
   }


   if(ascending)
       order=ascending;
   else if(descending==-1)
       order=descending;
   else
       order=0;

   return order;
}

sample output:
1 8 3 4 5 NO Order
1 2 3 4 5 Ascending Order
5 4 3 2 1 Descending Order