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

PROGRAMMING LANGUAGE C++ b) Create a function with the declaration void readSequ

ID: 3890131 • Letter: P

Question

PROGRAMMING LANGUAGE C++

b) Create a function with the declaration

void readSequenceFromFile(std::string filename, int** sequencePointer, int *length)

that performs the following three steps:

Step 1. The function reads the file filename and extracts the first integer. This first integer is to be stored in *length and represents the length of the sequence to be read.

Step 2. The function allocates a sequence of integers of size *length and assigns it to *sequencePointer.

Step 3. The function reads the next *length integers from the

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
using namespace std;

std::ifstream ifn;

void readSequenceFromFile(std::string filename, int* sequencePointer, int *length)
{
     char name[256];
     int i;
     for(i=0;filename[i]!='';i++)
           name[i]=filename[i];
     name[i]='';

     ifn.open(name);
     int x;
     while(!ifn.eof())
     {
       ifn >> *length;
       sequencePointer = new int[*length];
       cout << " sequence length : " << *length << endl;
       for(i=0;i<*length&&!ifn.eof();i++)
       {
          ifn >> x ;
          sequencePointer[i]=x;
          cout << sequencePointer[i] << " " ;
       }
     }
}

int main()
{
   string fname;
   int len;
   int a[100];
   cout << "Enter input filename : ";
   cin>> fname;
   readSequenceFromFile(fname, a, &len);
   return 0;
}

/*
Sample Executed output
----------------------


lenovo@lenovo-Vbox:~/chegg$ g++ readseq.cpp -Wall
lenovo@lenovo-Vbox:~/chegg$ cat numbers.txt
8 34 45 6 2 9 23 8 23 4 81 52 12 7 21 88 66 55 44lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter input filename : numbers.txt

sequence length : 8
34 45 6 2 9 23 8 23
sequence length : 4
81 52 12 7
sequence length : 21
88 66 55 44 lenovo@lenovo-Vbox:~/chegg$


*/

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