lotrsimp12345 37 Posting Pro in Training

I can write the recursion version correctly need help with writing a do loop in DrScheme.

After the do loop the variable x is sent to car L intially and then is updated to first element of everything but first element in list.

(define (sumiteration L)
  (do ((x (car L) (car(cdr L))))
    ((null? L))
    (set! sum (+ sum x)))
)
lotrsimp12345 37 Posting Pro in Training

yea my bad.

lotrsimp12345 37 Posting Pro in Training

I can write the recursion version correctly need help with writing a do loop in DrScheme.

After the do loop the variable x is sent to car L intially and then is updated to first element of everything but first element in list.

(define (sumiteration L)
  (do ((x (car L) (car(cdr L))))
    ((null? L))
    (set! sum (+ sum x)))
)
lotrsimp12345 37 Posting Pro in Training

thanks i will try that.

lotrsimp12345 37 Posting Pro in Training

none of these include directives exist at least in 2.0. I can't find what files to include.

lotrsimp12345 37 Posting Pro in Training

a search needs a for loop which keeps on moving the pointer forward and if it find it returns true.

similar thing for a general delete I would think. You would have to traverse entire list.

lotrsimp12345 37 Posting Pro in Training

i will take a look after this week. And try and help.

lotrsimp12345 37 Posting Pro in Training

the node contains 2 pointers in my case which are left and right and the root node in my bst class is used to scan the tree to do insert, delete, search.

template <class KT, class DT>
void my_bst<KT,DT>::insert(KT searchkey, const DT& newDataItem)
{
	my_bst_node<KT,DT>* temp=new my_bst_node<KT,DT>(searchkey, newDataItem);
	insert(temp, root);
}
 
template <class KT, class DT>
void my_bst<KT,DT>::insert(my_bst_node<KT,DT>*&	newnode ,my_bst_node<KT,DT>*& nodepointer)
{
   //if empty tree
   if(nodepointer==NULL)
   {
		nodepointer=newnode;
   }
   //key less than root nodes 
   else if((newnode->key)<=(nodepointer->key))
   {
	   insert(newnode,nodepointer->left);
   }
   //key greater than root nodes
   else
   {
	   insert(newnode,nodepointer->right);
   }
lotrsimp12345 37 Posting Pro in Training

use loops instead of so many cout statments and i don't think the variable in the while loop is defined. I am willing to help you come with the solution if i knew what you were doing. I can't read your mind. Based on your code your don't have a default condition.

lotrsimp12345 37 Posting Pro in Training

assuming your code is in one file for the class you should use inline in front like this:

inline void ChangeFood(string foodName, double foodPrice){
		mFoodName  = foodName;
		mFoodPrice = foodPrice;
	}
lotrsimp12345 37 Posting Pro in Training

Here is the code I am not sure what you mean.
my_bst_node.h

//create bst node

#ifndef MY_BST_NODE_H
#define MY_BST_NODE_H

#include <iostream>

using namespace std;

template<class KT,class DT>
class my_bst_node
{
public:
	my_bst_node();
	my_bst_node(KT tag, DT info, my_bst_node* l=0, my_bst_node* r=0);

	KT key;
	DT data;
	my_bst_node* left;
	my_bst_node* right;
};

template<class KT, class DT>
my_bst_node<KT,DT>::my_bst_node()
{
		left=right=0;
}

template<class KT, class DT>
my_bst_node<KT,DT>::my_bst_node(KT tag, DT info, my_bst_node* l, my_bst_node* r)
{
		key=tag;
		data=info;
		left=l; 
		right=r;
}

#endif

my_bst.h

//operations that a bst can perform

#ifndef MY_BST_H
#define MY_BST_H
#include <iostream>
#include "my_bst_node.h"

using  namespace std;

template<class KT,class DT>
class my_bst
{
public:
	//default constructor
	my_bst();

	//inserts a new node into binary tree
	void insert(KT searchkey, const DT &newDataItem);

	//search for the key and if found return true
	void search(KT searchkey);

	//prints out tree based on visiting the root node, then the left subtree and last the right subtree
	void preorder_print();

	//prints out tree based on visiting the left subtree, then the root node, and last the right subtree
	void inorder_print();

	//prints out tree based on visiting the left subtree, then the right subtree, and last the root node
	void postorder_print();

	//remove data item
	void remove(KT searchkey);

	//returns height of BST
	int height();

	//check if tree is balanced
	void balanced();

	//output tree structure
	void show(int tree);

private:
	my_bst_node<KT,DT>* root;

	//helper function of insert
	void insert(my_bst_node<KT,DT>*& newnode ,my_bst_node<KT,DT>*& nodepointer);

	//helper function of search
	void search(my_bst_node<KT,DT>*& d, const KT& searchkey);

	//helper function of preorder print
	void preorder_print(my_bst_node<KT,DT>*& f);

	//helper function of …
lotrsimp12345 37 Posting Pro in Training

I got the files to build but with this link

http://www.site.uottawa.ca/~laganier/tutorial/opencv+directshow/cvision.htm

the include files in step 2 don't show up anywhere.
Help appreciated.

lotrsimp12345 37 Posting Pro in Training

use getline till end of line using \n and then you can create substrings with delimeters.
maybe use getline() in conjunction with stream operators such as >> since you want to read in integers or numbers.

lotrsimp12345 37 Posting Pro in Training

if you are trying to have you data shown on console in your write to file method also have it printed out to console using cout.

lotrsimp12345 37 Posting Pro in Training

if it a pointer you need to dereference the variable otherwise you are getting the memory location. use *tokens to dereference pointer.

lotrsimp12345 37 Posting Pro in Training

First of all instead of writing out all those numbers on top i would put it in a loop by using a variable increment that variable and so forth Also to create a book of * use \n and . Make your life a lot easier. Don't have to type as much :). Easy to reuse you code.

Instead of using all those char arrays use string and getline() which should allow you to put in a delimiter to stop at.



setw() sets the field width of the next item only. So I don't think you should be having problems. Maybe you could explain the problem.

#include <iostream>

using namespace std;

int main()
{
	int j;
	for(int i=0; i<=5; i++)
	{
		for(j=0; j<=9; j++)
		{
			cout<<j;
		}
			j=0;
	}

	return 0;
}

Do something like this with the remainder of the *. Make your code look less clutter and easier to read :).

lotrsimp12345 37 Posting Pro in Training

please help.

lotrsimp12345 37 Posting Pro in Training

SO it says given the follwing relations

father (X,Y) says that X is the father of Y
mother (X,Y) says that X is the mother of Y
female(X) says that X is female
male(X) says that X is male

Define a sister relation so that sister(X,Y) says that X is a sister of Y

Here's what I have:

father(X,Y).
mother(X,Y).
female(X).
male(X).

sister(X,Y):-female(X),mother(M,Y), mother(M,X), father(F,X), father(F,Y).

So basically to be a sister, X must be a female, and both X and Y must have same mother and father.

Is this the correct approach?

Thanks.

lotrsimp12345 37 Posting Pro in Training

just keep counting every time it encounters a space

trying using isspace function I believe that calculates white spaces

while(isspace())
{
count++;
}

something like that.

lotrsimp12345 37 Posting Pro in Training
template <class KT, class DT>
void my_bst<KT,DT>::printLevelOrderAux(my_bst_node<KT,DT>* t, int level)
{
	if(t) 
	{
		if(level == 1) 
		{
			printf(" %d ", t->key);
		}
		else if (level > 1) 
		{
			printLevelOrderAux(t->left, level-1);
			printLevelOrderAux(t->right, level-1);
		}
	}
}

template <class KT, class DT>
void my_bst<KT,DT>::printLevelOrder(my_bst_node<KT,DT>* t,int height) 
{
	int i;

	for(i = 1; i <=height; i++) 
	{
		printLevelOrderAux(t, i);
	}
}

template <class KT, class DT>
void my_bst<KT,DT>::show(int height)
{
	printLevelOrder(root,height);
}
lotrsimp12345 37 Posting Pro in Training

use the "new" keyword and have a pointer point to this new data. That's what dynamic allocation is and make the new data an array which can have any size. So may use a variable.

lotrsimp12345 37 Posting Pro in Training

Here's what i have:

#include <iostream>
#include <fstream>
#include <iomanip>
#include "account.h"
#include "Lentext.h"
#include "Deltext.h"


using namespace std;
int main()
{
    LengthTextBuffer a;
    account b;

    strcpy(b.account_Number,"1111111111");
    strcpy(b.name,"Name");
    strcpy(b.address,"Address");
    strcpy(b.city, "City");
    strcpy(b.state, "IL");
    strcpy(b.zip_Code, "60192");
    strcpy(b.account_Balance, "111111");
    b.Print(cout);
    //pack data into a buffer not happening
    int c=b.Pack(a);
    cout<<endl<<c<<endl;
    return 0;
}
lotrsimp12345 37 Posting Pro in Training

its still won't work. I don't understand why.

lotrsimp12345 37 Posting Pro in Training

Here are my teachers directions

FILE PROCESSING - PROGRAMMING PROJECT 2
DUE: 10/28/09
This programming project is to modify the first project to be one that uses two of the buffers and the BufferFile classes outlined in the textbook in Chapter 4. The file is made of account information for a credit card company (class will be similar to project 1). To keep things simple we are only going to keep the following pieces of information about the account:

1. Account number - a 10 digit number
2. Card holder's name
3. Card holder's address which contains
* street address
* city
* state
* zip code
4. account balance

You are two write two separate programs using the buffer file class for each of the buffers chosen:
PROGRAM 1
This program is to create the file using the buffer class chosen. Your program is to be interactive with a user. You should first ask for the name of the file to be created, then create the file (in write mode). If the file cannot be created you should output an error message and quit. If the file is created then you should enter a loop that allows the user to input data into the file. Your program can use a sentinel value for the account number to exit the loop (an account number of -1 could be used).
PROGRAM2
This program is to …

lotrsimp12345 37 Posting Pro in Training

i don't think that is the intent of packing a buffer. IT is supposed to have a record which contains of fields and store it into a buffer everytime the all account objects assigned. The pack and send it to the buffer which does the reading and writing.

lotrsimp12345 37 Posting Pro in Training

but the code in my book shows that i should pack and my teacher said I should Pack the object, then write it to the file, that way the person class and buffer class don't know anything about each other.

lotrsimp12345 37 Posting Pro in Training

ahhhh it won't pack.. it returns 0 which means information isn't packed. This code in my book doesn't work.
main

#include <iostream>
#include <fstream>
#include <iomanip>
#include "account.h"
#include "Lentext.h"
#include "Deltext.h"


using namespace std;
int main()
{
    LengthTextBuffer a;
    account b;

    strcpy(b.account_Number,"1111111111");
    strcpy(b.name,"Name");
    strcpy(b.address,"Address");
    strcpy(b.city, "City");
    strcpy(b.state, "IL");
    strcpy(b.zip_Code, "60192");
    strcpy(b.account_Balance, "111111");
    b.Print(cout);
    //pack data into a buffer not happening
    int c=b.Pack(a);
    cout<<endl<<c<<endl;
    //ofstream out;
    //out.open("test.txt");
    //a.Write(out);
    return 0;
}
lotrsimp12345 37 Posting Pro in Training

Here's what i have. Not sure what it is doing exactly.

You pack a account object and then you write it to the buffer right? So shouldn't it print after i write it?

Main

#include <iostream>
#include <iomanip>
#include "account.h"
#include "Lentext.h"
#include "Deltext.h"


using namespace std;
int main()
{
    int result;
    account a;
    LengthTextBuffer b;

    strcpy(a.account_Number,"1111111111");
    strcpy(a.name, "Name");
    strcpy(a.address, "Address");
    strcpy(a.city, "City");
    strcpy(a.state, "IL");
    strcpy(a.zip_Code, "60192");
    cout<<"person object's data\n";
    a.Print(cout);
    result=a.Pack(b);
    cout<<"result after pack is"<<result;
    b.Print(cout);
    result=b.Write(cout);
    cout<<"result after write is"<<result;



    return 0;
}
[/CODE]

Account.h
[CODE]
#include <iostream>
#include "Lentext.h"
#include "Deltext.h"

#include <string>
using namespace std;
class account
{
public:
    char account_Number[11];
    char name[1000];
    char address[1000];
    char city[16];
    char state[3];
    char zip_Code[10];
    char account_Balance[1000];

    //default account constructor
    account();

    //set each field to a empty string
    void Clear();

    //methods for Lentext
    int Pack(LengthTextBuffer&) const;
    int UnPack(LengthTextBuffer&);

    //methods for Deltext
    int Pack(DelimTextBuffer&) const;
    int UnPack(DelimTextBuffer&);

    //print data
    void Print(ostream &);

    //intializes a DelimTextBuffer to be used for Persons
    int InitBuffer(DelimTextBuffer &);

    //intializes a LengthTextBuffer to be used for Persons
    int InitBuffer(LengthTextBuffer &);
};


[/CODE]

Account.cpp
[CODE]
#include "Account.h"
#include <iostream>
#include <iomanip>
using namespace std;


account::account()
{
    Clear();
}

void account::Clear()
{
    account_Number[0]=0;
    name[0]=0;
    address[0]=0;
    city[0]=0;
    state[0]=0;
    zip_Code[0]=0;
    account_Balance[0]=0;
}

//pack the fields into a FixedTextBuffer
//return true if all succeed
int account::Pack(LengthTextBuffer & Buffer) const
{
    int result;
    Buffer.Clear();
    result=Buffer.Pack(account_Number);
    result=result&&Buffer.Pack(name);
    result=result&&Buffer.Pack(address);
    result=result&&Buffer.Pack(city);
    result=result&&Buffer.Pack(state);
    result=result&&Buffer.Pack(zip_Code);
    result=result&&Buffer.Pack(account_Balance);
    return result;
}

int account::UnPack(LengthTextBuffer & Buffer)
{
    int result;
    result=Buffer.Unpack(account_Number);
    result=result&&Buffer.Unpack(name);
    result=result&&Buffer.Unpack(address);
    result=result&&Buffer.Unpack(city);
    result=result&&Buffer.Unpack(state);
    result=result&&Buffer.Unpack(zip_Code);
    result=result&&Buffer.Unpack(account_Balance);
    return result;
}

//pack …
lotrsimp12345 37 Posting Pro in Training

thanks!

lotrsimp12345 37 Posting Pro in Training

thanks.

lotrsimp12345 37 Posting Pro in Training

basically i have 4 files 2 contain the following data . Trying to find out data which is common and not common in both files and write it out to files common and not common. It almost works except for it doesn't compare the last item in the list before it ends. :(
Thanks.

Final1.txt
3
6
9
12
15
18
21

Final2.txt
9
12
15
18
21
24
27
30
33
36

--stores numbers which are common
Common
9
12
15
18
doesn't get this number: 21

Not common
3
6
24
27
30
33
36

#include <iostream>
#include <fstream>

using namespace std;
//open file to read  from
void intializeList(ifstream& List, char* input);

//open file to write to
void intializeoutput(ofstream& List, char* output);

//getitem from file
int getNextitem(ifstream& file);

//output item to correct file
void setnextitem(ofstream& file, int number);


int main()
{
	int moreItems;


	ifstream Final1,Final2;
	ofstream Common,Notcommon;

	//open input and output files
	intializeList(Final1,"Final1.txt");
	intializeList(Final2,"Final2.txt");
	intializeoutput(Common,"Common.txt");
	intializeoutput(Notcommon,"Notincommon.txt");
	
	//get first item in both lists
	int list1=getNextitem(Final1);
	int list2=getNextitem(Final2);
	moreItems=list1&&list2;
	
	//loop until no items in one list
	while(moreItems)
	{
		//item from list 1 smaller than item from list 2
		if(list1<list2)
		{
			cout<<"list1<list2\n";
			//write out to not common file
			setnextitem(Notcommon,list1);
			//get next value in list1
			list1=getNextitem(Final1);
		}
		//items are equal 
		else if(list1=list2)
		{
			cout<<"list1=list2\n";
			setnextitem(Common,list1);
			list1=getNextitem(Final1); …
lotrsimp12345 37 Posting Pro in Training

Please help really confused.

a multilevel index using a b-tree is to be made for a file with 600,000 records. A block contains 1500 bytes, a key is 10 bytes, a pointer is 5 bytes and a data record is 100 bytes.

a)what is the order of the tree?(assume tree node=a disk block)

600000recordsx100 bytes gives me total file size.
So a block would contain 15 records.

According to the definition in my book the order of a b-tree is the maximum number of descendants that a node in the b-tree can have.

b) how many blocks are required to hold the b-tree records at the lowest level.

2^n would give me the number of descendants at the lowest level where n is order of tree?


c)if at most 5000 bytes can be allocated in memory for the in memory portion of the index, what should the order be so that "sufficient" index blocks can be kept in memory

lotrsimp12345 37 Posting Pro in Training

i got it to work so its all good.

lotrsimp12345 37 Posting Pro in Training

a multilevel index using a B-tree is to be made for a file with 600,000 records. A block contains 1500 bytes, a key is 10 bytes, a pointer is 5 bytes and a data record is 100 bytes.

a)What is the order of the tree?(assume tree node=a disk block)

600000recordsX100 bytes gives me total file size.
So a block would contain 15 records.

According to the definition in my book the order of a B-tree is the maximum number of descendants that a node in the B-tree can have.

b) How many blocks are required to hold the B-tree records at the lowest level.

2^n would give me the number of descendants at the lowest level where n is order of tree?


c)if at most 5000 bytes can be allocated in memory for the in memory portion of the index, what should the order be so that "sufficient" index blocks can be kept in memory

lotrsimp12345 37 Posting Pro in Training

gives me error on 3rd coded line in search().

#ifndef MY_HT_CHAIN_H
#define MY_HT_CHAIN_H
#include <iostream>
#include <vector>
#include <list>

using namespace std;

class my_ht_chain
{
public:
	//constructor which creates a table of a certain size 
	my_ht_chain(int initTableSize);

	//destructor
	~my_ht_chain();

	//insert a value based on key
	void insert(int key);

	//search for a value based on key
	bool search(int key);

	//delete an item in the list
	void remove(int key);

	//prints out all items stored in hash table
	void show_ht_structure() const;

private: 
	//number of vectors to store in array
	int tablesize;

	//table to store key 
	vector<list<int>>* dataTable;

	//returns a int which corresponds to position in array
	int hashfunction(int key);
};



bool my_ht_chain::search(int key)
{
	list<int>::iterator list1;
	vector<list<int>>::iterator vector1;
	//1. find the array index
	vector1=dataTable->at(hashfunction(key));
	//2. search the list at that array index found above till key matches if not return false
	
}

int my_ht_chain::hashfunction(int key)
{
	return (key%tablesize);
}
lotrsimp12345 37 Posting Pro in Training

nvm i found my mistake it wasn't even in here. It was when i constructor my vector apparently the vector(# of values you wish to assign, what you wish to assign) not other way round.

lotrsimp12345 37 Posting Pro in Training

my insert function won't work. :( I got it to work before.

void my_ht_chain::insert(int key)
{
	//go through loop to check if hashing function matches vector index
	for(int i=0; i<tablesize; i++)
	{
		//match
		if(hashfunction(key)==i)
		{
			dataTable->at(i).push_front(key);
		}
	}
}

out of range invalid vector subscript.

lotrsimp12345 37 Posting Pro in Training

nvm i finally got it work. Thanks thought.

lotrsimp12345 37 Posting Pro in Training

kind of need help with syntax again.
in my insert method, i am supposed to expand an vector with a list. Not sure what to do within if statement.
Rather how I say at this index in my vector expand sideways a list which stores data values.

void my_ht_chain::insert(int key)
{
	//go through loop to check if hashing function matches vector index
	for(int i=0; i<tablesize; i++)
	{
		//match
		if(hashfunction(key)==i)
		{
			
		}
	}
}
lotrsimp12345 37 Posting Pro in Training

thanks! it makes a lot more sense its just like when you declare an array. we kind of have to for this hash table assignment. Have to create using seperate chaining method. Will ask for help again if i get stuck.

lotrsimp12345 37 Posting Pro in Training

so for instance say i did dataTable->at(key) would i be referring to the list or vector? or both?

lotrsimp12345 37 Posting Pro in Training

Is my understanding of a vector list correct. vector<list<int>>.

The vector holds a list of data type int?

class my_ht_chain
{
public:
	//constructor which creates a table of a certain size 
	my_ht_chain(int size);

	//destructor
	~my_ht_chain();

	//insert a value based on key
	void insert(int key);

	//search for a value based on key
	bool search(int key);

	//delete an item in the list
	void remove(int key);

	//prints out all items stored in hash table
	void show_ht_structure() const;

private:
	//number of vectors to store in array
	int tablesize;

	//table to store key 
	vector<list<int>>* dataTable;

	//used to map a key to a position
	int hashfunction(int key);
};
lotrsimp12345 37 Posting Pro in Training

actually it isn't too difficult to change my code. Thanks for help. I realized i was getting many errors because two variable are named the same thing.

lotrsimp12345 37 Posting Pro in Training

but my teacher wants a constructor and a destructor. So i guess i have to use pointers.

lotrsimp12345 37 Posting Pro in Training

how do i do this without changing it to a pointer? then i need to change all the remainder of my code and would have to use a iterator.

lotrsimp12345 37 Posting Pro in Training

thanks i completely forgot about that.

lotrsimp12345 37 Posting Pro in Training

Do I need a copy constructor?

#ifndef MY_MIN_PQ_H
#define MY_MIN_PQ_H

#include <iostream>
#include <vector>
using namespace std;

class my_min_pq
{
public:
	//default constructor
	my_min_pq();

	//destructor 
	~my_min_pq();

	//insert element at end of sequence, and rearrange so minimum data at the front of array
	void enqueue(int data ,int priority);

	//removes data and priority from front of vector
	bool dequeue();

	//show structure based on priority where the smallest key is the first item outputted. 
	void show_pq_structure();

private: 
	//store priority
	vector<int> pq;

	//store data
	vector<int> data;

	//finds minimum priority value in array and swap positions of this priority index 
	//with the first index as well as data values
	void find_min();

};

my_min_pq::my_min_pq()
{
	pq;
	data;
}

my_min_pq::~my_min_pq()
{
	~pq();
	~data();
}
lotrsimp12345 37 Posting Pro in Training

we are supposed to implement using STL vector and base it on priority and have have a data value

So every time i enqueue i insert an in item into the end of the vector and if no space i modify. So do i reorder the list to place the maximum at front when i do this or do I create a find min function?

When i dequeue i would remove from fronso i would have to reorder the priority so minimum is at front?

lotrsimp12345 37 Posting Pro in Training

you could try using the &symbol before the variable but that might just put in memory location of those arrays.

lotrsimp12345 37 Posting Pro in Training

you pass in k to your function i believe.