C++ :: Linear Searching Through Array Of Class Objects
Jan 22, 2013
I have a linear search algorithm set up to search through an array of class objects it works but the output does not match, when i search for a particluar name in the array the 1st and third values int the array are found but the second value is not found..
below is my code:
int linsearch(string val) {
for (int j=0; j <= 3; j++) {
if (player[j].getLastName()==val)
return j ;
So I have a Class named Subject, wich I used linked list to keep the objects. Now I have another Class called Tests which I should register a test by the Subject class, so I made a search like this:
List obj; //it's the object to access the List class, which manipulates the //nodes from Subject.
obj.currentPtr=obj.firstPtr; while (obj.currentPtr!=NULL) { if(obj.currentPtr->nameSubject==value) //searching if there's a return obj.currentPtr; //name equal to provided obj.currentPtr=obj.currentPtr->nextPtr; } return 0;
I've made a similar process for Subject. But in this when I access the firstPtr, it's shown to be 0, once I have already the list of Subject and it should not be zero. What's wrong?
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code: class B { public: B():_y(1){} virtual ~B() { cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
#include <iostream> using namespace std; class superclass; class subclass1; class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
I need an array of class objects but am unsure of how one might accomplish this. I have so far...
//element class driver code Element Arsenic(lowCeiling, highCeiling); Element Cadmium(lowCeiling, highCeiling); Element Chromium(lowCeiling, highCeiling); Element Copper(lowCeiling, highCeiling); Element Lead(lowCeiling, highCeiling); Element Nickel(lowCeiling, highCeiling); Element Molybdenum(lowCeiling, highCeiling); Element Mercury(lowCeiling, highCeiling); Element Selenium(lowCeiling, highCeiling); Element Zinc(lowCeiling, highCeiling);
What I want to do is have an admin class which will hold all the employee objects, can add them, list and calculate salaries. I'm trying to make array of objects, not sure if it's right
here is the code
Code: #include <iostream> #include <string> using namespace std;
class Employee { public: Employee(string name, short type, int salary)
I have written a selection sort algorithm to go sort an array of class objects by age in ascending order, the problem is that the output being given does not match what i think the code should do. when the program runs the 3 records are added to the array and when they are sorted should be outputed in ascending order, the problem is that with my code the last 2 are sorted properly but the first element does not seem to move, it remains the same as the original unsorted value.
My code for the selection sort function and the display method are below:
void selectionSort() { int i, minIndex, minValue; for (i = 0; i < (arrlength - 1); i++) { minIndex = i ;
im tasked with creating a linear search script using functions on a 10 element array. the elements are to be supplied by the user as is the search target.
I understand how to create the array and gather that information from the user as well as howto set a variable for "target", this is what im calling it. Those two parts are simple enough.
I am not fully understanding the calling of an array in a function as a pointer. i semi understand the use of a pointer and howto call a normal pointer in a function. i also understand that an array is nothing more then a "special" pointer with a set of consecutive address blocks for the size of the array.
My first user defined function is simple enough
Code: ReadArray(int A[], int size){ int i; printf("Please enter %d integer numbers separated by spaces: ", size); for (i = 0; i < size; i++) scanf("%d", &A[i]); }
Sso nothing out of the ordinary there. that should be a standard for loop and use of scanf, sadly prof has not covered ssanf or any of the other options so i am stuck using scanf for now. maybe someday down the line in a other program or after this course ill get the chance to learn about better options for gathering data from the user.
I am confused as to my next function:
Code: void SearchArray(int A[], int target, int size);
I've not written any code here yet as im not sure if i should call the A[], or *A for the first type of the function?
If i call *A do i then use something like this for my search:
Code: for (*A = 0; *A < size; *A++) if (*A < target) or use A[] insteadA?
Code: for (i = 0; i < size; i++) if (A[i] = target)
Let's say I have a Car object , and it contains inner Engine object.
Code: struct Car{ Engine mEngine; };
In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:
Code: Car::Car: mEngin(arg1,arg2,...) { other stuff here }
Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?
Code: class Toyota: Car(...), mEngine(...), mGear(..) { ... };
The other options are: 1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive 2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
Basically I initialize an int array of size 10 with zeros. Then element 7 is changed to 1. Via cin I collect a user input (integer) which is the number to be searched.
Now the problem is that the search function from the book is somehow not working. I added a function to see how the array looks like and there is definitely one element with value 1 that should be found, but it simply isn't. The function always returns -1 (element not found).
The program compiles, but doesn't work as it is supposed to.
//******************************************************** // search algorithm for arrays //******************************************************** #include <iostream> using namespace std;
I have to search an array of struct students on the basis of their first names, last names and roll numbers .. so far i am done with the input of the student first , last names and roll numbers . I have problem in searching the array ..
I'm trying to open a text file from my computer and randomly output a random line of text (string) onto the console. How do I go about doing this?
So for example, if my text file, "StringTextFile.txt" had 50 rows of strings datas (not integers or chars), how would I randomly generate a line of word from the 50 rows of words in the StringTextFile.txt and output it onto the console?
This is how far I've gotten, which isn't much, but somewhere. I don't even know if my code even makes sense, at this point I'm just randomly shoving code together.
My program generates an array of random numbers. I want to then search a specific number within the array. If the number is in the array then a message apopears on the console saying that its there. I'm using the binary search algorithm to do this.
#include <iostream> #include <ctime> #include <cstdlib> #include <iomanip> #include <cstring> using namespace std; int size; int getSize() { return size; }
Set found to false. Set position to -1. Set index to 0. While found is false and index < number of elements If list[index] is equal to search value found = true. position = index. End If Add 1 to index. End While. Return position
I am having a problem printing out the results for my code, It is supposed to print out the largest and smallest value in the array, but keeps giving me a value not entered and I don't know why.
//This program lets the user enter 10 numbers and then display the highest and lowest number.
#include <iostream> #include<stdio.h> using namespace std; int main() { const int NUM_ENTERED = 9; int number[NUM_ENTERED], highestValue = number[0], lowestValue = number[0], count=0;
inputting a search array. I tried putting a binary search but I can't get it to work. everything else works up until I put the value I am searching for in the array, then it just crashes.
How it suppose to work: input 2 coordinates with a value each then it calculates the distance between them then it suppose to let user search the coordinates for a value and state if found which coordinate it is at.
I have been working on an assignment where I have to add three objects of a class Matrix. The class should have the flexibility to add more than two oprands without changing any operand on Left hand side of the '=' operator.
For a beginners C++ lab, I have a base class Employee and two derived classes HourlyEmployee and SalaryEmployee. In main, I have a vector defined as vector <Employee *> VEmp; It's then passed to a function to get the input, which works fine. But what I'm struggling with is another function with the header "printList(const vector <Employee *> & Ve)". It's supposed to loop through the vector and call the appropriate printPay function, which is a seperate print function inside each derived class. How do I loop through the vector and print it out? I was trying to do a for loop and something like "Ve[i].printPay();", but that doesn't work. So how would I do it?
Here's some snippets of the relevant code.
class Employee { .... virtual void printPay() = 0; }; class HourlyEmployee : public Employee {
i ran the following code in the latest version of code::blocks and it tells me that the objects cout and cin are not declared in this scope. what is the problem?
I used to use Turbo C++ 3.0 and i had no problem whatsoever with that compiler. But now i am trying to move to code::blocks but it is proving very very hard as all the standards have been changed.
I am a school student and thus, we had been told to practice on Turbo C++ 3.0 and now i am unable to unlearn it. Also, if i use printf in place of cout there is no error but i want to use cout as it is what i am comfortable working with.
#include<fstream> #include<conio.h> int main() { using namespace std; char name[20];
[Code] ....
Is there some document to which i can refer so as to get the latest C++ standards which is C++0x i believe?