I have defined the struct chararray as follows.
struct chararray
{
char* lhs;
char* rhs;
};
Now, I have two arrays la[l] and ra[l](both have all l values defined). I assign that to the struct,and return the function.
chararray s;
s.lhs = new char[l];
s.rhs = new char[l];
s.lhs = &la[0];
s.rhs = &ra[0];
return s;
In main , I ouptut
for(int i = 1; i < l;i++){
cout << *(s.rhs + i);
}
Now,I get gibberish as output.
But if I output *(s.rhs + 0) and *(s.rhs + 1) and ... [ the values 0,1,.. are explicitly included ], I get correct output!
Why doesnt the for loop work?
PS - My entire code is -
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
struct chararray
{
char* lhs;
char* rhs;
};
chararray getcenter(char in[],int l){
int open_count = 0;
int open = 0;
int first = 0;
int last = 0;
int limit;
int close_count = 0;
char lhs[l],rhs[l];
for(int i = 0; i < l;i++){
lhs[i] = ' ';
rhs[i] = ' ';
}
for(int i = 0; in[i]!='\0';i++){
int flag = 0;
if(in[i] == '('){
if(flag == 0){first = i;flag = 1;}
open++;
}
if(in[i] == '('){
last = i;
}
limit = i;
}
for(int i = 0; in[i]!='\0';i++)
{
//cout << "entrt" << endl;
int temp;
if(in[i] == '(') { open_count++; }
if(in[i] == ')') { close_count++; }
if((open_count == close_count) && (open_count != 0) && (open_count != open))
{
//cout << open_count << endl;
for(int j = 0;j < i+1;j++)
{
lhs[j] = in[j];
}
lhs[i+1] = ' ';
lhs[i+2] = ' ';
for(int j = i+3; in[j]!='\0';j++)
{
lhs[j] = ' ';
rhs[j-i-3] = in[j];//Assuming no space between -> and rhs
temp = j;
}
for(int j = temp;rhs[j] != '\0';j++){
rhs[j] = ' ';
}
for(int j = temp;lhs[j] != '\0';j++){
lhs[j] = ' ';
}
break;
}
}
chararray s;
s.lhs = new char[l];
s.rhs = new char[l];
s.lhs = &lhs[0];
s.rhs = &rhs[0];
return s;
}
int main()
{
string input;
cin >> input;
int l=input.length()+1;
char in[l];
strcpy(in, input.c_str());
chararray s = getcenter(in,l);
for(int i = 1; i < l;i++){
//cout << *(s.rhs + i); - Doesnt work
}
cout << *(s.lhs + 5); // Works!
return 0;
}
Your for loop is starting at i=1, and when you explicitly set the values you are using 0 and 1. Decide where you want to start, and try it again.
Try this:
for(int = 0 ; i < l; i++)
cout << *(s.rhs + i);
Note that you are actually risking a memory leak with these lines of code:
s.lhs = new char[l];
s.rhs = new char[l];
s.lhs = &lhs[0];
s.rhs = &rhs[0];
You are allocating 1 char and storing its address in s.lhs, then resetting s.lhs to the address of lhs[0]. There is no need for the two first lines with the new in them. So replace that code with this: (also note that &lhs[0] is the same as lhs)
s.lhs = lhs;
s.rhs = rhs;
Related
I have an implementation of the Long Integer class, which should provide the ability to perform arithmetic operations with instances of the class.
It works in 95% of situations, but sometimes it shows wrong results.
The screen with wrong answers is located below the code.
HugeInt.h:
#ifndef _HUGEINGH
#define _HUGEINGH
#include <iostream>
#include <list>
#include <string>
#include <math.h>
using namespace std;
class HugeInt {
private:
char* listNumber;
int maxLength, length;
bool plus;
void expansion_size_length();
void updatingLength();
void set_listNumber(int position, char num);
char get_listNumber(int position);
char symbol_in_numeral(char symbol);
char numeral_in_symbol(char numeral);
public:
string toString();
void setNumber(string str);
HugeInt operator-(HugeInt HugeInt);
HugeInt operator+(HugeInt HugeInt);
HugeInt operator*(HugeInt HugeInt);
HugeInt(string str);
HugeInt();
HugeInt(const HugeInt &lg);
~HugeInt();
};
#endif
Hugeint.cpp:
#include "stdafx.h"
#include "HugeInt.h"
void HugeInt::expansion_size_length() {
char* tmp = new char[maxLength * 2];
for (int i = 0; i < maxLength; i++) {
tmp[i] = listNumber[i];
}
delete[] listNumber;
listNumber = tmp;
maxLength *= 2;
}
void HugeInt::updatingLength() {
while (length > 1) {
int tmp = listNumber[length - 1];
if (listNumber[length - 1] == 0) {
length--;
}
else {
return;
}
}
}
void HugeInt::set_listNumber(int position, char num) {
if (position < maxLength) {
listNumber[position] = num;
if (position >= length)
length = position + 1;
}
else {
while (position > maxLength)
expansion_size_length();
length = position + 1;
}
}
char HugeInt::get_listNumber(int position) {
if (position < length) {
return listNumber[position];
}
return 0;
}
char HugeInt::symbol_in_numeral(char symbol) {
return symbol - '0';
}
char HugeInt::numeral_in_symbol(char numeral) {
return numeral + '0';
}
string HugeInt::toString() {
string str = string();
if (!plus)
str.insert(str.size(), 1, '-');
for (int i = length - 1; i >= 0; i--) {
str.insert(str.size(), 1, numeral_in_symbol(listNumber[i] / 10));
str.insert(str.size(), 1, numeral_in_symbol(listNumber[i] % 10));
}
return str;
}
void HugeInt::setNumber(string str) {
//plus = true;
//maxLength = 10;
//listNumber = new char[maxLength];
length = 0; // дело в этом
delete[] listNumber;
listNumber = new char[str.length() + 1];
maxLength = str.length() + 1;
for (int i = length; i < maxLength; i++)
listNumber[i] = 0;
length = maxLength;
if (str.size() != 0) {
int k = 0;
if (str[0] == '-') {
plus = false;
for (int i = str.size() - 1; i > 0; i -= 2) {
if (i - 1 > 0)
listNumber[k] = symbol_in_numeral(str[i]) + symbol_in_numeral(str[i - 1]) * 10;
else listNumber[k] = symbol_in_numeral(str[i]);
k++;
}
}
else if (str[0] == '+') {
plus = true;
for (int i = str.size() - 1; i > 0; i -= 2) {
if (i - 1 > 0)
listNumber[k] = symbol_in_numeral(str[i]) + symbol_in_numeral(str[i - 1]) * 10;
else listNumber[k] = symbol_in_numeral(str[i]);
k++;
}
}
else {
plus = true;
for (int i = str.size() - 1; i >= 0; i -= 2) {
if (i - 1 >= 0)
listNumber[k] = symbol_in_numeral(str[i]) + symbol_in_numeral(str[i - 1]) * 10;
else listNumber[k] = symbol_in_numeral(str[i]);
k++;
}
}
}
updatingLength();
}
HugeInt HugeInt::operator-(HugeInt _HugeInt) {
HugeInt result = HugeInt();
if (_HugeInt.plus == this->plus) {
int length_tmp = _HugeInt.length > this->length ? _HugeInt.length : this->length;
char part_sub = 0;
int sub;
char tmp1, tmp2;
for (int i = 0; i < length_tmp; i++) {
tmp1 = listNumber[i];
tmp2 = _HugeInt.listNumber[i];
sub = ((int)listNumber[i] - (int)_HugeInt.listNumber[i] - (int)part_sub);
part_sub = 0;
if (sub < 0) {
sub += 100;
part_sub = 1;
}
//else if (sub == 0) //
//part_sub = 1; //
result.set_listNumber(i, sub);
}
if (part_sub != 0) {
part_sub = 0;
result.plus = !this->plus;
for (int i = 0; i < length_tmp; i++) {
sub = ((int)result.listNumber[i] + (int)part_sub - 100);//
part_sub = 0;
if (sub < 0) {
sub = abs(sub);
part_sub = 1;
}
//else if (sub == 0) //
//part_sub = 1; //
result.set_listNumber(i, sub % 100);
}
}
result.updatingLength();
return result;
}
else {
HugeInt lg1 = HugeInt(_HugeInt);
HugeInt lg2 = HugeInt(*this);
lg1.plus = !lg1.plus;
//HugeInt.plus = !HugeInt.plus;
return lg2 + lg1;
}
}
HugeInt HugeInt::operator+(HugeInt _HugeInt) {
HugeInt result = HugeInt();
if (_HugeInt.plus == this->plus) {
int length_tmp = _HugeInt.length > this->length ? _HugeInt.length : this->length;
char part_sum = 0;
char tmp1, tmp2;
for (int i = 0; i < length_tmp; i++) {
tmp1 = _HugeInt.listNumber[i];
tmp2 = listNumber[i];
result.set_listNumber(i, (_HugeInt.listNumber[i] + listNumber[i] + part_sum) % 100);
part_sum = (_HugeInt.listNumber[i] + listNumber[i] + part_sum) / 100;
}
if (part_sum != 0) {
result.set_listNumber(length_tmp, part_sum);
}
result.plus = plus;
result.updatingLength();
return result;
}
else {
if (_HugeInt.plus) {
//plus = !plus;
HugeInt lg1 = HugeInt(_HugeInt);
HugeInt lg2 = HugeInt(*this);
lg2.plus = !lg2.plus;
return lg1 - lg2;
}
else {
HugeInt lg1 = HugeInt(_HugeInt);
HugeInt lg2 = HugeInt(*this);
lg1.plus = !lg1.plus;
//HugeInt.plus != HugeInt.plus;
return lg2 - lg1;
}
}
}
HugeInt HugeInt::operator*(HugeInt _HugeInt) {
HugeInt result = HugeInt();
int part_mul;
int iter = 0;
HugeInt tmpHugeInt;
int tmp1, tmp2;
for (int i = 0; i < _HugeInt.length; i++) {
part_mul = 0;
tmpHugeInt = HugeInt();
for (int j = 0; j < length; j++) {
tmp1 = _HugeInt.listNumber[i];
tmp2 = listNumber[j];
iter = (int)_HugeInt.listNumber[i] * (int)listNumber[j] + (int)part_mul;
part_mul = iter / 100;
tmpHugeInt.set_listNumber(j + i, iter % 100);
}
if (part_mul != 0) {
tmpHugeInt.set_listNumber(tmpHugeInt.length, part_mul);
}
result = result + tmpHugeInt;
}
if (_HugeInt.plus != plus)
result.plus = false;
result.updatingLength();
return result;
}
HugeInt::HugeInt(string str) {
plus = true;
maxLength = 10;
listNumber = new char[maxLength];
length = 0;
setNumber(str);
}
HugeInt::HugeInt() {
plus = true;
maxLength = 10;
listNumber = new char[maxLength];
length = 0;
for (int i = length; i < maxLength; i++)
listNumber[i] = 0;
}
HugeInt::HugeInt(const HugeInt &lg) {
plus = lg.plus;
maxLength = lg.maxLength;
listNumber = new char[maxLength];
length = lg.length;
for (int i = 0; i < maxLength; i++)
if (i < lg.length){
listNumber[i] = lg.listNumber[i];
}
else{
listNumber[i] = 0;
}
}
HugeInt::~HugeInt() {
//delete[] listNumber;
}
main.cpp:
#include "stdafx.h"
#include "HugeInt.h"
int _tmain(int argc, _TCHAR* argv[])
{
/*
string num1, num2;
cout << "Enter the first number:" << endl;
cin >> num1;
cout << "Enter the second number:" << endl;
cin >> num2;
HugeInt hugeNum1 = HugeInt(num1);
HugeInt hugeNum2 = HugeInt(num2);
cout << "Entered the following numbers:" << endl;
cout << "First number: " << num1 << endl;
cout << "Second number: " << num2 << endl;
HugeInt sum = hugeNum1 + hugeNum2;
HugeInt sub = hugeNum1 - hugeNum2;
HugeInt mul = hugeNum1 * hugeNum2;
cout << "Summary = " << sum.toString() << endl;
cout << "Substruction = " << sub.toString() << endl;
cout << "Multiplacation = " << mul.toString() << endl;
*/
HugeInt longNumber1;
HugeInt longNumber2;
HugeInt longNumberRes;
/*
int firstTrue = 1837;
int secondTrue = -8108;
char buf[32];
itoa(firstTrue, buf, 10);
string firststr(buf);
itoa(secondTrue, buf, 10);
string secstr(buf);
longNumber1.setNumber(firststr);
longNumber2.setNumber(secstr);
longNumberRes = longNumber1 + longNumber2;
int res = atoi(longNumberRes.toString().c_str());
cout << "Good " << firstTrue << "+" << secondTrue << "=" << res << endl;
int firstFalse = 312;
int secondFalse = 712;
char buf2[32];
itoa(firstFalse, buf2, 10);
string firststr2(buf2);
itoa(secondFalse, buf2, 10);
string secstr2(buf2);
longNumber1.setNumber(firststr2);
longNumber2.setNumber(secstr2);
longNumberRes = longNumber1 - longNumber2;
int res2 = atoi(longNumberRes.toString().c_str());
cout << "Bad " << firstFalse << "-" << secondFalse << "=" << res2 << endl;
*/
for (int i = 0; i < 1000; i++)
{
int first = rand() % 20000 - 10000;
int second = rand() % 20000 - 10000;
char buf[32];
itoa(first, buf, 10);
string firststr(buf);
itoa(second, buf, 10);
string secstr(buf);
longNumber1.setNumber(firststr);
longNumber2.setNumber(secstr);
longNumberRes = longNumber1 - longNumber2;
int res = atoi(longNumberRes.toString().c_str());
if (res != first - second)
cout << i << " = "<< first << "+" << second << "=" << res << endl;
}
system("pause");
return 0;
}
It prints the following results when i am doing the test with 1000 random numbers (5 of 1000 results are incorrect):
48 = 6118+-7918=-1700
268 = -8887+4887=-3900
326 = 3169+-6169=-2900
474 = 212+-712=-400
492 = 1333+-7133=-5700
545 = 8352+-9552=-1100
617 = -4395+195=-4100
Looks like something wrong with minus operation, but i can not uderstand what exactly. Please, help, what is wrong in the implementation and how to fix it?
Well, I have to Create class to Insert String in another one at the specific position Without using class string(any pre-made classes except iostream.
When I ask for output it gets rubbish data..
#include "stdafx.h"
#include <iostream>
using namespace std;enter code here
//StrLen Calculates String Length
int StrLen(char *s) {
int len = 0;
while (s[len]!='\0'){
len++;
}
return len;
}
//The value of the string that is it's first argument is inserted into String that is it's second argument, pos is the beginning at position given by main.
char *InsertStr(char *s1, char *s2, int pos) {
int c = 0;
int len1 = StrLen(s1);
int len2 = StrLen(s2);
char s3[100];
int i = pos;
//This while move string from postion till the end of string to s3
while (s1[i] != '\0') {
s3[c] = s1[i];
c++;
i++;
}
c = 0;
i = pos;
//the string in s2[0 to the end of s2] moved to s1[from pos to len1]
while (s2[c] != '\0') {
s1[i] = s2[c];
c++;
i++;
}
int len3 = StrLen(s3);
len1 = StrLen(s1);
int x2 = len1 + len3;
c = 0;
len1 = StrLen(s1);
int x3 = pos + len2;
//this loop get the elements sent to s3 and get them back to
//s1[from pos + len2 till s3[i] reach the end]
for (int i = 0; i < len3; i++) {
s1[x3] = s3[i];
x3++;
}
return s1;
}
int main()
{
char s1[100];
char s2[100];
int pos=0;
cout << "Enter The First Argument String : \n";
cin.getline(s1, 100);
cout << "Enter The Second Argument String : \n";
cin.getline(s2, 100);
cout << "Enter The Position : \n";
cin >> pos;
cout << InsertStr(s1, s2, pos);
cout << "******************";
return 0;
}
I used In cin.getline(), to get all string till '\0'.
I traced the code all loops works fine except last one, Last one have garbage data.
I saw a video on youtube solved it like this but i couldn't trace it..
//This code is a working one, But i didn't use it since i couldn't trace it
#include "stdafx.h"
#include <iostream>
using namespace std;
int strlen(char *s)
{
int c = 0;
while (s[c] != '\0') c++;
return c;
}
char* my_strncat(char *s1, char *s2, int pos)
{
//k= pos
int len1, len2;
int i = 0, k = pos,l=0,j=0;
int x,x1, x2, x3;
len1 = strlen(s1);
len2 = strlen(s2);
char s3[100];
while (i <= len1) {
s3[i] = s1[i];
i++;
}
//x2=len2 j =len1
x1 = len1 + len2;
x3 = len2 + pos;
for (i = pos; i < x1; i++) {
x = s3[i];
if (l < len2) {
s1[i] = s2[l];
l++;
}
s1[x3] = x;
x3++;
}
return s1;
}
int main()
{
char s1[100] ;
char s2[100] ;
int pos = 0;
cout << "Enter The string of Source: \n";
cin.getline(s1,100);
cout << "Enter The string of Destination: \n";
cin.getline(s2, 100);
cout << "Enter The position of Destination: \n";
cin >> pos;
cout << my_strncat(s1, s2, pos) << endl;
return 0;
}
I cannot reproduce your garbage data but I see that you, in InsertStr() systematically forget, to add the zero at the end of strings. Your code goes in UB (Undefined Behaviour) so your garbage data a perfectly explained.
So, copying s1 in s3
while (s1[i] != '\0') {
s3[c] = s1[i];
c++;
i++;
}
s3[c] = '\0'; // add this line!
Copying s2 to s1+pos
while (s2[c] != '\0') {
s1[i] = s2[c];
c++;
i++;
}
s1[i] = '\0'; // add this line!
And copying the last part of s3 at the end of s1
for (int i = 0; i < len3; i++) {
s1[x3] = s3[i];
x3++;
}
s1[x3] = '\0'; // add this line!
Or, maybe, you can simply write your loops, using do/while, as follows
do
s3[c++] = s1[i];
while ( s1[i++] );
// ...
do
s1[i++] = s2[c];
while ( s2[c++] );
// ...
i = 0;
do
s1[x3++] = s3[i];
while ( s3[i++] );
i use strtok to split a string into different pieces and assemble them as a struct. This is my code:
object* parseJSON(size_t length, char* json, size_t jsonLen){
char *finalString = prepareJSON(json,jsonLen);
cout << finalString << "\n";
char delimiter[] = ",:";
char *token = strtok(finalString, delimiter);
object *retObj = (struct object *)malloc(length);
size_t returnObjectIndex = 0;
size_t index = 0;
bool firstTime = true;
struct object item;
while(token != NULL){
if((index % 2) != 0){
if(firstTime){
item.Type = token;
firstTime = false;
} else {
item.ID = token;
retObj[returnObjectIndex] = item;
returnObjectIndex++;
firstTime = true;
}
}
token = strtok(NULL,delimiter);
index++;
}
return retObj;
}
This is the finalString:
Type:temp,id:3,Type:temp,id:1
In my mind this should come out with this code:
for(int n = 0; n < 5; n++){
cout << returnObject[n].Type << " " << returnObject[n].ID << "\n";
}
as:
temp 3
temp 1
but instead i get this on the console:
temp 3
temp 1
h"° Ó*°
C
Is this because i loop 5 times even though there should be only 2 elements in the array? I loop 5 times because i can't get the number of elements of the array in the pointer (atleast i think that this is not possible).
Any help is very much appreciated
Best regards
Edit: This is my prepareJSON function:
char* prepareJSON(char* json, size_t jsonLen){
char forbiddenChars[] = {'[', '{','\"', '}', ']'};
bool isForbbidenChar = false;
char* finalString = (char*)malloc(jsonLen);
size_t finalStringIndex = 0;
for(size_t i = 0; i < jsonLen; i++){
char c = json[i];
isForbbidenChar = false;
for(int k = 0; k < 5; k++){
if(c == forbiddenChars[k]){
isForbbidenChar = true;
break;
}
}
if(isForbbidenChar){
continue;
}
else{
finalString[finalStringIndex] = c;
finalStringIndex++;
}
}
return finalString;
}
and this my main function:
int main(){
object *returnObject;
char json[] = "[{\"Type\":\"temp\",\"id\":\"3\"},{\"Type\":\"temp\",\"id\":\"1\"}]";
returnObject = parseJSON(5,json, strlen(json));
for(int n = 0; n < 5; n++){
cout << returnObject[n].Type << " " << returnObject[n].ID << "\n";
}
}
Solution:
I did indeed allocate too little memory, changed it:
object *retObj = (struct object *)malloc(sizeof(struct object) * length);
Now it works!
so I was writing a program which has to check if the input sting is a palindrome or not. And it's actually working but a problem with deleting the array I created during the course of it arouse.
#include <iostream>
using namespace std;
bool checkPalindrome(char* text);
char* clearString(char* src);
int main()
{
char buffer[1000];
cin.getline(buffer, 1000);
cout << boolalpha << checkPalindrome(buffer) << endl;
return 0;
}
bool checkPalindrome(char* text)
{
char* newStr = clearString(text);
if (!newStr)
return false;
int newLen = strlen(newStr);
for (int i = 0; i < newLen / 2; i++) {
if (newStr[i] == newStr[newLen - i - 1])
continue;
else
return false;
}
//delete[] newStr;
return true;
}
char* clearString(char* src)
{
unsigned len = strlen(src);
unsigned counter = 0;
for (int i = 0; i < len; i++) {
if (src[i] == ' '||src[i] == '.'||src[i] == ','||src[i] == '!')
counter++;
}
unsigned newSize = len - counter + 1;
char* dest = new(nothrow) char[newSize];
if (!dest) {
cout << "not enoough memory\n";
return NULL;
}
int i, j;
for(i = j = 0; j < newSize; ++i, ++j) {
if(src[i]==' '||src[i]=='.'||src[i]==','||src[i]=='!'||src[i]=='?')
i++;
else
dest[j] = src[i];
}
dest[j] = '\0';
return dest;
}
So the commentated delete in the checkPalindrome function causes a crash if executed and I get the "Heap corruption detected" error. I tried changing the function type to void and delete there and the same thing happened. Any ideas what causes it?
Your loop copies the '\0' at the end of the string, but then you add another '\0', using one more byte of memory than you allocated.
I've been working on a program in one of my college classes. I have been having trouble with the implementation of my LRU code as it is not displaying any errors or anything, but compiles. There are two parts. The main that we input the values into, which we then specify which algorithm we want to use to find page faults. I know the main works, along with the FIFO algorithm, but I'm not getting anything with my LRU code (It compiles and "runs" but displays nothing as if I did not click to use the algorithm). Can anyone help me figure out what is wrong?
main.cpp
#include <iostream>
#include <string>
//#include "fifo.cpp"
#include "lru.cpp"
//#include "optimal.cpp"
using namespace std;
int main() {
// List of different variables
string pagestring;
int fs,pn[50], n;
// Prompt for page references
cout<<"Virtual Memory Simulation\nBy blah\n----------\nEnter the number of pages : " << endl;
cin >> n;
cout<<"\n-------------\nPlease enter a list of page numbers separated by commas.\n"<< endl;
cin>>pagestring;
// algorithm to use
char algo;
while (true) {
// Prompt algorithm to use
cout<<"----------\nPlease select an algorithm to use.\n\n1: First-In-First-Out (FIFO)\n2: Least-Recently-Used (LRU)\n3: Optimal\n0: Quit\n"<<endl;
cin>>algo;
if (algo == '1') {
//fifo(pagestring);
}
else if (algo == '2'){
LRU_Execute(pagestring, n);
}
else if (algo == '3'){
cout<<"Optimal Not yet coded"<<endl;
}
else if (algo == '0'){
break;
}
else {
cout<<"Invalid choice. Please try again."<<endl;
}
}
cout<<"Goodbye!!"<<endl;
};
LRU.cpp
#include <iostream>
#include <string>
using namespace std;
class pra
{
int fs,z;
int frame[50], frame1[50][2], pn[50], n, cnt, p, x;
public:
pra();
void init(string pagestring);
void getdata(string pagestring, int n);
void lru(int* pn, int n, string pagestring);
};
pra::pra()
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::init(string pagestring)
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::getdata(string pagestring, int n)
{
fs=3;
// index to loop through input string
int i = 0;
// current input string character
char z = pagestring[i];
int x = 0;
//cout << "\nEnter the page numbers : ";
while (z != '\0'){
// skip over commas and spaces
if (!(z == ',')) {
pn[x] = z;
x++;
// cout<<pn[x]<<"-This is pn[x]\n";
}
z = pagestring[++i];
}
//cout<<pn[x]<<"-This is pn[x] AGAIN\n";
this->lru(pn, n, pagestring);
}
void pra::lru(int* pn, int n, string pagestring)
{
init(pagestring);
int ind = 0, fault = 0, pi = 0, j, fn;
char i, z;
p = 0;
cnt = 0;
int min;
cout<<n<<"---"<<i<<" - "<<j<<" - "<<" - "<<fn<<" - "<<z;
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
pi = 0;
for (i = 0; i < n; i++)
{
j = 0;
if (ind > fs - 1)
ind = 0;
fault = 1;
min = 999;
while (j < fs)
{
if (frame1[j][0] = pn[pi])
{
fault = 0;
p++;
frame1[j][1] = p;
goto l2;
}
if (frame1[j][1] < min)
{
min = frame1[j][1];
fn = j;
}
j++;
}
j = 0;
while (j < fs)
{
if (frame1[j][0] = -1)
{
fault = 1;
fn = j;
goto l2;
}
j++;
}
ind++;
l2:
if (fault == 1)
{
p++;
frame1[fn][0] = pn[pi];
frame1[fn][1] = p;
cnt++;
}
cout << "\nElement: " << pn[pi];
pi++;
for (z = 0; z < fs; z++)
{
cout << "\t" << frame1[z][0];
}
if (fault == 1)
cout << "\t**Page Fault**";
else
cout << "\t--No Page Fault--";
}
cout << "\nTotal number of page faults: " << cnt;
cout << "\n";
}
void LRU_Execute(string pagestring, int n)
{
pra p;
int j, fault = 0, i, pi, z, fn, ind = 0, ans, ch;
p.getdata(pagestring, n);
//p.lru();
while (ans == 1);
//return 1;
}