944,126 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3163
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 7th, 2005
0

can't proceed from here.

Expand Post »
As a part of a larger problem I have to solve the following::


*input - number of variables (say n=4)
*input - 'n' numbers in decimal (say 1,3,5,7)
*I need to check for all the grey code neighbours
i.e, to say, 2=00000001(a fixed length string) and
3=00000011(a fixed length string) differ EXACTLY IN ONE BIT.
so they form gery code neighbours and form a duet 000000x1.

* for the above example we get 4 gery code neighbours (so 4 duets)
which are 0000001x (from 1 and 3),
00000x01 (from 1 and 5),
00000x11 (from 3 and 7),
000001x1 (from 5 and 7)


*now I need to combine all the duets. for eg (1 and 5) and (3 and 7) combine to give a QUAD 000000xx1.()
for the above example we get 2 quads--which turn our to be the same.

*now I need to combine the quads to get 8's

* I need to go on and create lists of 1)all singletons
2)all duets
3)all quads
and so on.

so far I've managed to read the inputs and save them in a list of strings ,each string representing one input in binary

i.e, list of 00000001, 00000011, 00000101, 00000111 as strings.

Iam not able to decide how to proceed.
This is the first time Iam writing anything so complex.(I haven't written codes for anything more than sorting numbers before.)This one is for an assignment.
Iam not able to decide what data structures to use

Plz suggest any good way of approaching the problem.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
arikeri is offline Offline
25 posts
since Mar 2005
Mar 7th, 2005
0

Re: can't proceed from here.

anyone who can help Plz send me an yahoo msg and I'll show u the code I've written so far. Hope U unaderstand .I don't want to post the code.some of my friends too are using this forum.

Arjun.

YAHOO ID: arjunarikeri
Reputation Points: 10
Solved Threads: 0
Light Poster
arikeri is offline Offline
25 posts
since Mar 2005
Mar 7th, 2005
0

Re: can't proceed from here.

Basically you're looking at a fancy table lookup. Build a table of Gray codes for however many bits you're comparing and then look for two adjacent numbers in the list that match the codes in your table. Repeat until your computer chokes and dies.

One easy way to build a Gray code table is to start with a hard coded table for two bits:

00, 01, 11, 10

Copy and reverse the table:

10, 11, 01, 00

Prefix the codes in the first table with 0:

000, 001, 011, 010

Prefix the codes in the second table with 1:

110, 111, 101, 100

Append the second table to the first:

000, 001, 011, 010, 110, 111, 101, 100

This can be used for a table up to as many bits as you want, but keep in mind that there are multiple permutations for Gray codes. Your program specification is vague enough that keeping to it will be very difficult. This in particular is disturbing:
Quote ...
1)all singletons
2)all duets
3)all quads
and so on.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 7th, 2005
0

Re: can't proceed from here.

lsiten I already have a 100 line code.
Just that Iam not able to proceed further.But I don't want to post it for everyone to view.

Plz tell me how I can show u my code.

Arjun.
Reputation Points: 10
Solved Threads: 0
Light Poster
arikeri is offline Offline
25 posts
since Mar 2005
Mar 7th, 2005
0

Re: can't proceed from here.

>I already have a 100 line code.
Search google and you may find a clever algorithm to do what you want. Otherwise you're looking at brute force, and that's going to be sloooooooow.

>Plz tell me how I can show u my code.
Here, or nowhere. Daniweb is a public forum. If you want private help that may or may not be good quality (because it isn't publicly checked by the helper's peers), try www.allexperts.com.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 7th, 2005
0

Re: can't proceed from here.

Quote originally posted by Narue ...
>I already have a 100 line code.
Search google and you may find a clever algorithm to do what you want. Otherwise you're looking at brute force, and that's going to be sloooooooow.

>Plz tell me how I can show u my code.
Here, or nowhere. Daniweb is a public forum. If you want private help that may or may not be good quality (because it isn't publicly checked by the helper's peers), try www.allexperts.com.

OK. I'll risk it!
maybe this looks a brute force way.But ,you see, Ican't change it now.
I have an error in the function 'compare'(atleast I feel that the error there)

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3. #include<stdlib.h>
  4. #include<list>
  5. #include <sstream>
  6. using namespace std;
  7. // all variables used are global
  8. int n;//number of variables
  9. int m;//number of minterms
  10. int d;//number of don't cares
  11. int i;//used for indexing again and again
  12. int pos;//the functions 'compare' and 'hypergen' use this
  13. list<string>::iterator j;//I've used this in various functions
  14. list<string> minterms;// Each string in the all the lists in this program is of length 15 ....
  15. list<string> doncares;// and it represents(in binary) a positive integer less than ((2^15)-1)
  16. list<string> *slists=new list<string>[20];//I've put some big number
  17. //couldn't find a better solution:-(
  18. list<int> Minterm; //I needed something golbal (and dynamic!) for the minterms
  19. list<int> Doncare;
  20.  
  21. //The following are all the functions used.
  22. void hypergen(int i);//This function generates the hypercubes
  23. string dtob(int integer);
  24. string itoa(int num);
  25. bool compare(string one,string two);
  26. int main(){
  27. // Getting all the inputs from the user
  28. cout<<"enter the number of variables\n";
  29. cin>>n;
  30. for(i=0;i<=n;i++){list<string> LISTi;}
  31. cout<<"\nEnter the number of minterms\n";
  32. cin>>m;
  33. cout<<"\nenter the minterms\n";
  34. int minterm[m];
  35. for (i=0;i<m;i++){cin>>minterm[i];Minterm.push_back(minterm[i]);}
  36. cout<<"\nminterms read-OK!\n";
  37. cout<<"\nEnter the number of don't cares\n";
  38. cin>>d;
  39. int doncare[d];
  40. cout<<"\nenter the don't cares\n";
  41. for (i=0;i<d;i++){cin>>doncare[i];Doncare.push_back(doncare[i]);}
  42. cout<<"\ndon't cares read-OK!\n";
  43. // pushing the minterms and don't cares int their lists
  44. for (i=0;i<m;i++){minterms.push_back(dtob(minterm[i]));slists[0].push_back(*(--minterms.end()));}
  45. cout<<"\nBinary representation of Minterms pushed to the list 'minterms' and to slists[0]\n";
  46. for (i=0;i<d;i++){doncares.push_back(dtob(doncare[i]));slists[0].push_back(*(--doncares.end()));}
  47. cout<<"\nBinary representation of Don't cares pushed to the list 'doncares' and to slists[0]\n";
  48. cout<<"\n\n";
  49. //Printing the list to check for corrrectness
  50. for(j=minterms.begin(); j != minterms.end(); ++j) cout << *j << " ";
  51. cout <<"\nAren't these minterms ?\n"<< endl;
  52. for(j=doncares.begin(); j != doncares.end(); ++j) cout << *j << " ";
  53. cout <<"\nAren't these don't cares ?\n"<<endl;
  54. for(j=slists[0].begin(); j != slists[0].end(); ++j) cout << *j << " ";
  55. cout <<"\nAren't these (minterms)+(don't cares) ?\n\n"<<endl;
  56.  
  57. //Now the 'REAL' thing
  58. for(i=0;i<=n;i++){hypergen(i);}
  59.  
  60. return 1;
  61.  
  62. }
  63.  
  64. /*------------------------------------------------------------------- ---- ---*/
  65. /* The fuction takes in a decinmal integer and returns it binary representation as a atring */
  66. string dtob(int integer){
  67. int index,temp,bin;
  68. int array[15]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384};
  69. string BIN;
  70. if(integer<0 || integer>32767){
  71. cout<<"\nThe number must be between 0-32767.\nEverything has a limit!";
  72. cout<<"\nThis program may fail now";
  73. }
  74. else{
  75. index=14;
  76. while(index>=0){
  77. temp=0;
  78. if(integer>=array[index]){
  79. temp=array[index];
  80. integer=integer-temp;
  81. bin=1;
  82. index--;
  83. }
  84. else{
  85. bin=0;
  86. index--;
  87. }
  88. BIN+=itoa(bin);
  89. }
  90. }
  91.  
  92. return BIN;
  93.  
  94. }
  95.  
  96. /*-------------------------------------------------------------------------------*/
  97. /* input is an integer output is the char of that integer
  98.   for eg if input is integer 5 output is char 5 */
  99. string itoa(int num){ //Don't know much about how this function works!
  100. stringstream converter;
  101. converter << num;
  102. return converter.str();
  103. }
  104. /*---------------------------------------------------------------------------------*/
  105. /*This function takes in the index of a list and generates all 'next' order hypercubes
  106.   that ara possible by combining exactly two elements of this list ---------------*/
  107. /*All 'next' order hypercubes generated from 'slists[i]' are stored in 'slists[i+1] */
  108. /*The two terms which combine can be(and are) safely deleted -----------------------*/
  109. void hypergen(int i){ //I don't want 'i' to be changed in this function
  110. bool arjun;
  111. string temp;//temporary string whiched is pushed into slists[i+1]
  112. list<string>::iterator k;
  113. for(j=slists[i].begin(); j != slists[i].end(); ++j){
  114. for(k=++j; k!=slists[i].end(); ++k){
  115. arjun = compare(*j,*k);
  116. if (arjun){// need to add an element add a hypercube to slist[i+1]
  117.  
  118. }
  119. }
  120. }
  121. for(j=slists[i].begin(); j != slists[i].end(); ++j) cout << *j << " ";
  122. cout <<"\n This is hypercube list no "<<i<<endl;
  123. }
  124.  
  125. /*-------------Iam fed up -------------------------------------------------*/
  126. /*funtion returns a valuue 'TRUE' if the input strings differ exactly by one bit */
  127. bool compare(string one,string two){
  128. int count;
  129. count=0;//this stores the number of bits in 'one' and 'two' differ
  130. pos=0;//pos give the position where the strings 'one' and 'two' differ.
  131. for(i=0; i<=14; i++){
  132. if(one[i] != two[i]){++count; pos = i;}
  133. }
  134. if(count == 1) {return (0<5);} //returning TRUE
  135. else {return (0>5); //returning FALSE
  136.  
  137. }




plz help me rectify the error.

Arjun
Reputation Points: 10
Solved Threads: 0
Light Poster
arikeri is offline Offline
25 posts
since Mar 2005
Mar 7th, 2005
0

Oh my god !

can't believe I posted this.

Plz neglect my previous posting. I had just missed a ' } '.
the error actually got me goin nuts!

Iam sorry,

Arjun
Reputation Points: 10
Solved Threads: 0
Light Poster
arikeri is offline Offline
25 posts
since Mar 2005
Mar 7th, 2005
0

Re: can't proceed from here.

Hi,

suppose 'a' , 'b' and 'c' are three strings which are already storing something.
how can I 1)erase c and
2)combine parts of 'a' and 'b' and store it ic c.


eg: initially a = "helloarjun"
b= "hisam"
c= "how r u"

finally I want c= "hellosam"


thanks in advance

Arjun
Reputation Points: 10
Solved Threads: 0
Light Poster
arikeri is offline Offline
25 posts
since Mar 2005
Mar 7th, 2005
0

code needs a small correction

Iam screwing up in the function 'hypergen' .Iam not able to use the increment operator properly. i.e, Iam not able to apply ++j /j++ properly.Iam totally confused

Plz read the first posting of this thread for the problem statement.

here is my code
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3. #include<stdlib.h>
  4. #include<list>
  5. #include <sstream>
  6. using namespace std;
  7. // all variables used are global
  8. int n;//number of variables
  9. int m;//number of minterms
  10. int d;//number of don't cares
  11. int i;//used for indexing again and again
  12. int pos;//the functions 'compare' and 'hypergen' use this
  13. list<string>::iterator j;//I've used this in various functions
  14. list<string> minterms;// Each string in the all the lists in this program is of length 15 ....
  15. list<string> doncares;// and it represents(in binary) a positive integer less than ((2^15)-1)
  16. list<string> *slists=new list<string>[20];//I've put some big number
  17. //couldn't find a better solution:-(
  18. list<int> Minterm; //I needed something golbal (and dynamic!) for the minterms
  19. list<int> Doncare;
  20.  
  21. //The following are all the functions used.
  22. void hypergen(int i);//This function generates the hypercubes
  23. string dtob(int integer);
  24. string itoa(int num);
  25. bool compare(string one,string two);
  26. int main(){
  27. // Getting all the inputs from the user
  28. cout<<"enter the number of variables\n";
  29. cin>>n;
  30. for(i=0;i<=n;i++){list<string> LISTi;}
  31. cout<<"\nEnter the number of minterms\n";
  32. cin>>m;
  33. cout<<"\nenter the minterms\n";
  34. int minterm[m];
  35. for (i=0;i<m;i++){cin>>minterm[i];Minterm.push_back(minterm[i]);}
  36. cout<<"\nminterms read-OK!\n";
  37. cout<<"\nEnter the number of don't cares\n";
  38. cin>>d;
  39. int doncare[d];
  40. cout<<"\nenter the don't cares\n";
  41. for (i=0;i<d;i++){cin>>doncare[i];Doncare.push_back(doncare[i]);}
  42. cout<<"\ndon't cares read-OK!\n";
  43. // pushing the minterms and don't cares int their lists
  44. for (i=0;i<m;i++){minterms.push_back(dtob(minterm[i]));slists[0].push_back(*(--minterms.end()));}
  45. cout<<"\nBinary representation of Minterms pushed to the list 'minterms' and to slists[0]\n";
  46. for (i=0;i<d;i++){doncares.push_back(dtob(doncare[i]));slists[0].push_back(*(--doncares.end()));}
  47. cout<<"\nBinary representation of Don't cares pushed to the list 'doncares' and to slists[0]\n";
  48. cout<<"\n\n";
  49. //Printing the list to check for corrrectness
  50. for(j=minterms.begin(); j != minterms.end(); ++j) cout << *j << " ";
  51. cout <<"\nAren't these minterms ?\n"<< endl;
  52. for(j=doncares.begin(); j != doncares.end(); ++j) cout << *j << " ";
  53. cout <<"\nAren't these don't cares ?\n"<<endl;
  54. for(j=slists[0].begin(); j != slists[0].end(); ++j) cout << *j << " ";
  55. cout <<"\nAren't these (minterms)+(don't cares) ?\n\n"<<endl;
  56.  
  57. //Now the 'REAL' thing
  58. for(i=0;i<=n;i++){hypergen(i);}
  59.  
  60. return 1;
  61.  
  62. }
  63.  
  64. /*------------------------------------------------------------------- ---- ---*/
  65. /* The fuction takes in a decinmal integer and returns it binary representation as a atring */
  66. string dtob(int integer){
  67. int index,temp,bin;
  68. int array[15]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384};
  69. string BIN;
  70. if(integer<0 || integer>32767){
  71. cout<<"\nThe number must be between 0-32767.\nEverything has a limit!";
  72. cout<<"\nThis program may fail now";
  73. }
  74. else{
  75. index=14;
  76. while(index>=0){
  77. temp=0;
  78. if(integer>=array[index]){
  79. temp=array[index];
  80. integer=integer-temp;
  81. bin=1;
  82. index--;
  83. }
  84. else{
  85. bin=0;
  86. index--;
  87. }
  88. BIN+=itoa(bin);
  89. }
  90. }
  91.  
  92. return BIN;
  93.  
  94. }
  95.  
  96. /*-------------------------------------------------------------------------------*/
  97. /* input is an integer output is the char of that integer
  98.   for eg if input is integer 5 output is char 5 */
  99. string itoa(int num){ //Don't know much about how this function works!
  100. stringstream converter;
  101. converter << num;
  102. return converter.str();
  103. }
  104.  
  105. /*---------------------------------------------------------------------------------*/
  106. /*This function takes in the index of a list and generates all 'next' order hypercubes
  107.   that ara possible by combining exactly two elements of this list ---------------*/
  108. /*All 'next' order hypercubes generated from 'slists[i]' are stored in 'slists[i+1] */
  109. /*The two terms which combine can be(and are) safely deleted -----------------------*/
  110. void hypergen(int i){ //I don't want 'i' to be changed in this function
  111. bool arjun;
  112. string x ("x");
  113. string temp;//temporary string whiched is pushed into slists[i+1]
  114. list<string>::iterator k;
  115. for(j=slists[i].begin(); j != slists[i].end(); j++){ //THE ERROR IS SOMEWHERE HERE.
  116. for(k=j++; k!=slists[i].end(); k++) { //A double loop to compare each pair of strings.
  117. arjun = compare(*j,*k);// 'arjun' is true only when *j and *k differ exactly by 1 bit
  118. if (arjun){// need to add an element add a hypercube to slist[i+1]
  119. temp = *j;
  120. temp[pos] = x[0];
  121. slists[i+1].push_back(temp);
  122. }
  123. }
  124. }
  125. for(j=slists[i+1].begin(); j != slists[i+1].end(); j++) cout << *j << " ";
  126. cout <<"\n This is hypercube list no "<<i+1<<endl;
  127. }
  128.  
  129. /*-------------Iam fed up -------------------------------------------------*/
  130. /*funtion returns a valuue 'TRUE' if the input strings differ exactly by one bit */
  131. bool compare(string one,string two){
  132. int count;
  133. count=0;//this stores the number of bits in 'one' and 'two' differ
  134. pos=0;//pos keeps track of position where 'one' and 'two' differ
  135. cout<<"\n one = "<< one ; // this is to check weather strings are passeed properly or not
  136. cout<<"\n two = "<< two <<"\n" ;
  137. for(i=0; i<=14; i++){
  138. if(one[i] != two[i]){++count; pos = i;} //for every disimilarity,increase the count by 1
  139.  
  140. }
  141. if(count == 1) {return (0<5);} //returning TRUE
  142. else {return (0>5);} //returning FALSE
  143.  
  144. }
Reputation Points: 10
Solved Threads: 0
Light Poster
arikeri is offline Offline
25 posts
since Mar 2005
Mar 7th, 2005
0
Re: can't proceed from here.
have a small correction to make in my previous posting.

line no.7 and line no.109 of the code should not have been there. Both are comments which are actually false.I forgot to delete them

very sorry for this


Arjun
Edit/Delete Message
Reputation Points: 10
Solved Threads: 0
Light Poster
arikeri is offline Offline
25 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: cell bill help again
Next Thread in C++ Forum Timeline: traversing a list in a for loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC