943,825 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 527
  • C++ RSS
Nov 8th, 2008
0

Flexie Programming Assignment

Expand Post »
i've wrote a program with a header file and a cpp file. when i try to compile i am getting errors that i can't fix, (i'm just a beginner). Code Below:

the .H file
C++ Syntax (Toggle Plain Text)
  1. //Flexie Muirhead #: 26075046
  2. //Assignment #2
  3.  
  4. #include <iostream>//allows program to output data to the screen
  5. using namespace std;
  6.  
  7. //searching class definition
  8. class Searching
  9. {
  10. public:
  11. //member funtions
  12. void insertionSort(int *list, int last);
  13. void hashSearching(int *list, int last);
  14. void sequentialSearch(int list[], int last);
  15. void binarySearch(int list[], int last);
  16.  
  17. private:
  18. };
  19.  
  20. //funtion sorts a list
  21. void Searching::insertionSort(int *list, int last)
  22. {
  23. int current;
  24. int hold;
  25. int walker;
  26.  
  27. for (current = 1; current <= last; current++)
  28. {
  29. hold=list[current];
  30.  
  31. for(walker = current -1; walker >= 0 && hold < list[walker]; walker--)
  32. {
  33. list[walker + 1 ] = list[walker];
  34. }
  35.  
  36. list[walker + 1] = hold;
  37. }
  38.  
  39. }//end of funtion insertion sort
  40.  
  41.  
  42. //searches a list using hash search
  43. void Searching::hashSearching(int *list, int last)
  44. {
  45. //declarations
  46. int hashList[last];
  47. int num;
  48. int address;
  49.  
  50. //rearrange list by determine location with the modulo-division
  51. //method
  52. for(int x=0; x<10; x++)
  53. {
  54. address = list[x]%last;
  55.  
  56. hashList[address]= list[x];
  57. }
  58.  
  59. //accept target
  60. cout<<"Enter The Number You Want To Find: ";
  61. cin>>num;
  62.  
  63. //finds loction by modulo-division method
  64. address = num%last;
  65.  
  66. //check if location correct
  67. if (num == hashList[address])
  68. cout<<"Loaction Found!!! "<<endl;
  69. else
  70. cout<<"Location Not Found!!! "<<endl;
  71. }
  72.  
  73.  
  74. //search list using sequential search
  75. void Searching::sequentialSearch(int *list, int last)
  76. {
  77. int target;
  78. int flag=0;
  79.  
  80. //accpet target
  81. cout<<"Enter The Number You Want To Find: ";
  82. cin>>target;
  83.  
  84. //check to see if target is in list
  85. for(int cntr = 0; cntr < last; cntr++)
  86. {
  87. if(list[cntr] == target)
  88. {
  89. cout << "Target Found!!" << cntr << "."<< endl;
  90. flag += 1;
  91. }
  92.  
  93. }
  94.  
  95. //if flag has not be incrimented them location not found
  96. if(flag < 1)
  97. {
  98. cout << "Target Not Found!!" << endl;
  99. }
  100.  
  101. }//end of sequential search
  102.  
  103.  
  104. //searches a lsit using binaery search
  105. void Searching::binarySearch(int list[], int last)
  106. {
  107.  
  108. //declarations
  109. int target;
  110. int first = 0;
  111. int mid;
  112.  
  113. //accept the target
  114. cout << "Enter a target to be found: ";
  115. cin >> target;
  116.  
  117. //loops until program reaches the end of the list
  118. while(first < last)
  119. {
  120. mid = (first + last)/2;
  121.  
  122. if(target > list[mid])
  123. {
  124. first = mid + 1;
  125. }
  126.  
  127. else
  128. if(target < list[mid])
  129. {
  130. last = mid + 1;
  131. }
  132. else
  133. {
  134. first = last + 1;
  135. }
  136. }
  137.  
  138. //check to see if location found
  139. if(target == list[mid])
  140. cout << "Target Found." << endl;
  141. else
  142. cout << "Target Not Found." << endl;
  143. }//end of funtion binary search

the .CPP file
C++ Syntax (Toggle Plain Text)
  1. //Flexie Muirhead #: 26075046
  2. //Assignment #2
  3.  
  4. //include directives
  5. #include <iostream>//allows program to output data to the screen
  6. #include <iomanip>
  7. #include "plpl.h"//include definition of class Search
  8.  
  9. using namespace std;
  10.  
  11. //funtion prototype, menu
  12. void menu(char *option);
  13.  
  14. //************INT MAIN*****************
  15. int main()
  16. {
  17. //array size. number of elements in the list
  18. const int arraySize = 10;
  19. //assign elements to list
  20. int array[arraySize] = {45, 67, 98, 12, 34, 53, 20, 01, 9, 56};
  21.  
  22. //create an object of the class
  23. Search searchOption;
  24.  
  25. char option;
  26. menuu:
  27.  
  28. //call menu funtion
  29. menu(option);
  30. //change opyion to caps
  31. option = toupper(option);
  32.  
  33. switch(option)
  34. {
  35. case 'A':
  36. system("cls");
  37. searchOption.sequentialSearch(array, arraySize);
  38. break;
  39.  
  40. case 'B':
  41. system("cls");
  42. searchOpyion.binarySearch(array, arraySize);
  43. break;
  44.  
  45. case 'C':
  46. system("cls");
  47. searchOption.hashSorting(array, arraySize);
  48. break;
  49.  
  50. case 'X':
  51. system("cls");
  52. cout<<"THANK YOU FOR USING THIS PROGRAM; SEE YOU NEXT TIME!\n\n";
  53. cout<<"\tThis Prgram was Created by:\n\n"<<"\t\tFlexie Muirhead\n\n";
  54. system("pause");
  55. exit(0);
  56. break;
  57.  
  58. default:
  59. system("cls");
  60. cout<< "Please Enter One of the Stated Options!"<<endl;
  61. }
  62.  
  63. system("pause");
  64. system("cls");
  65.  
  66. //goto statment acts a loop to loop back to the star of the program
  67. goto menuu;
  68.  
  69. system("pause");
  70. return 0;
  71. }//end of main funtions
  72.  
  73. //function header; this funtion accept option for the type of
  74. //search the user wants
  75. void menu(char *option)
  76. {
  77. cout<<"\n\t\tFlexie Muirhead Type Test Search Software\n";
  78. cout<<"\n\t\t"<<"_____________________________________________\n\n";
  79.  
  80. cout<<"\t\t\t"<<"MENU OPTIONS\n\n\n";
  81. cout<<"\t\t"<<"Press [A] For Sequential Searc Test\n";
  82. cout<<"\t\t"<<"Press [B] For Binary Search Test\n";
  83. cout<<"\t\t"<<"Press [C] For Hash Searche Tes\n";
  84. cout<<"\t\t"<<"Press [X] To Exit\n\n";
  85. cout<<"\t\t"<<"Select Option -->"<<endl;
  86.  
  87. cout<<"Enter Option ";
  88. cin>>option;
  89. }//end of menu funtion
Last edited by Ancient Dragon; Nov 8th, 2008 at 8:40 am. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flexy82rpq is offline Offline
4 posts
since Oct 2008
Nov 8th, 2008
0

Re: Flexie Programming Assignment

Can you please put your code with using the code tags.
[code= language ]
Used without the spaces.
[/ code ]

Also can you let us know what errors you are getting?
Reputation Points: 11
Solved Threads: 11
Junior Poster
kenji is offline Offline
145 posts
since May 2008
Nov 9th, 2008
0

Re: Flexie Programming Assignment

It has been completed, all errors has been resolve
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flexy82rpq is offline Offline
4 posts
since Oct 2008
Nov 10th, 2008
0

Re: Flexie Programming Assignment

ok, u solved your problem, but still there are some problems you should notice:

1. never put function definitions inside a header file, just the declarations. If you want to include that header in more compile units you will end up with a linker error because the compiler will generate the same code multiple times and the liker will not know what to link with.
ex:

Bla.h

class Bla { void nothing(); };

void Bla::nothing() {}; ----> THIS MUST BE PLACED IN A .CPP ( Bla.cpp )file if you want to include the header in multiple places.

2. if you have a class that has no data members(ie no state), why not make all functions static?
It makes no sense to have to instantiate an object that has no state, just to be able to call a function.
ex:
instead of
Bla obj();
obj.do_whatever();

or even worst
Bla obj = new Bla();
obj->do_whatever();
delete obj;

you can just
Bla::do_whatever();
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008

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: Algorithm in C++
Next Thread in C++ Forum Timeline: Database conectivity with c++





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


Follow us on Twitter


© 2011 DaniWeb® LLC