pointer and dynamic array

0 comments

I have a program for counting the number of times each word appears in a text file. but the program is built using c++ libraries that I am not allowed to use, therefore, i would like the program to be modified in a way that only these libraries are used:

<string>: for using strings

<algorithm> only for changing everything to lower case

<fstream> for files

<iostream>for input/output

the modified must keep using pointers and dynamic memory arrays

detailed comments for every line must be provided

.

.

a program with a similar logic to this one must be implemented for checking for alphabetical numbers INSTEAD OF THE ALREADY MADE REGEX FUNCTIONS :

bool alpha(char ch){ // this function checks whether char is alphabet or not

if((ch>=’a’ && ch<=’z’) || (ch>=’A’ && ch<=’Z’)) //if yes then return true
return true;
else //if not then return false
return false;
}

CALLING FUNCTION:

for(int i=0;i<n;i++){
for(int j=0;j<s[i].size();j++){
if(!alpha(s[i][j])){ //check if char is alphabet or not
s[i][j] = ‘ ‘; // if not then replace with space
}
}
}

.

.

.

.

also, for the size of the array (using dynamic memory), please use this example we did in class to modify the code to something similar: FOCUS ON HOW THEY USED SIZE AS A PARAMETER

#include <iostream>

#include <fstream>

using namespace std;

void loadData (int *HR, int *time, string filename, int *size);

void calculateMax (int *HR, int *time, int *maxHR, int *timeMax, int size);

#define SIZE 1440 // to save maximum size possible (24 hrs * 60 mins)

int main()

{

int time[SIZE], HR[SIZE];

int size;

int max_HR = 0, time_max = 0;

string filename;

cout << “Enter file name n”;

cin >> filename;

loadData(HR, time, filename, &size);

calculateMax (HR, time, &max_HR, &time_max, size);

cout << “Maximum heart rate is: ” << max_HR << endl;

cout << “Maximum heart rate occurred at time: ” << time_max;

return(0);

}

void loadData (int *HR, int *time, string filename, int *size)

{

ifstream inFile;

inFile.open (filename, ios::in);

if (inFile.fail())

{

cerr << “Error opening the file n”;

exit (-1);

}

inFile >> *size;

for (int i=0; i< *size; i++)

inFile >> *(time + i) >> *(HR + i);

inFile.close();

}

void calculateMax (int *HR, int *time, int *maxHR, int *timeMax, int size)

{

*maxHR = 0;

*timeMax = 0;

for (int i=0; i<size; i++)

{

if(*(HR+i) > *maxHR)

{

*maxHR = *(HR+i);

*timeMax = *(time+i);

}

}

}

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}