C++ :: Quicksort Function To Work With Class Array
Jan 19, 2013
void Quicksort(int info[],int left,int right){
int pivot = left + (right - left)/2;//it is the middle (will change sometimes but will end up in the middle
int temp;
while(left<=right){
while(info[left] < pivot){
[Code] ....
This is my quicksort function. I have tried a lot of things but I am trying to get it to work to sort all the details I have stored in an array by there age. This is how I have entered the data.
void DataEntry(Details info[],int size){
int x;
int i;
i=0;
cout << "How Many Entries Would You Like to add";
[Code] ....
I am stuck on what to do with it
View 18 Replies
ADVERTISEMENT
Jun 9, 2014
I've implemented it a bit differently: I create 2 temporary arrays, one for the numbers lower from the pivot , an done for numbers greater then the pivot. in the end of each iteration the 2 arrays are copied to the original array:
Code: #include <iostream>
void QuickSort (int* A , int start, int end){
if (end-start<3){
return;
}
int mid=(start+end)/2;
int pivot=A[mid];
int lA[end-start] , rA[end-start], rCounter=0,lCounter=0;
int curr=0,i=start;
[code]....
View 3 Replies
View Related
Feb 16, 2014
I have a homework requiring me to use quicksort on a pointer array of CDs. They are sorted by the title. I have tried the general solution for integer arrays which sets the pivot at the middle integer but it didn't work/ stopped working when I tried to start sorting the already inputed CDs. The same is happening with the current version (an adaptation of pieces of code):
void swap(CD &a, CD &B)/>
{
CD temp;
temp = a;
[Code]....
View 2 Replies
View Related
Feb 11, 2013
i need a function that will work for both dynamic and static implementations of a function to get the transverse of a matrix. so far, i have this
Code:
matrix transpose(matrix m)
{
int row, col;
row = m.com_dim;
col= m.row_dim;
}
[code]....
this works well with my static implementation, but when i try it in dynamic it gives me errors. the function has to be the same for both dynamic and static implementation
View 4 Replies
View Related
Feb 5, 2013
I realized a Matrix class to practice and I have a problem I can not solve! Here my problematic code:
Mtrx.h:
Code:
template <class T>
Mtrx::Mtrx(dim m, dim n, const bool random_constructed = false, const T min = static_cast<T>(0), const T max = static_cast<T> (10))
Mtrx.C
[Code] ...
And here the relative main section:
Code:
Mtrx rand1 ( 5, 5, bool);// ok
cout<<rand1<<endl;
Mtrx rand2 ( 7, 3, bool, -5, 20);// ok
cout<<rand2<<endl;
Mtrx rand3 ( 7, 7, bool, 0., 15.);// compilation error: undefined reference to
// "Mtrx::Mtrx<double>(unsigned long, unsigned, bool, double, double)"
// collect2: error: ld returned 1 exit status
I compiled in a Linux OS with g++ -std=c++11
View 3 Replies
View Related
Feb 17, 2015
int sift(int a[], int b[], int n, int p) {
int i,k,x, tmp,index=0,count=0;
for(x=0; x<n; ++x)
b[x]=a[x];
[Code] .....
This is a function for a quicksort... Everything works; however, when I return index it returns the value for 'n'... I printed the value for index right before the function returns it and its 4, as it should be.
View 1 Replies
View Related
Mar 8, 2014
I have this:
string input;
unsigned short choice;
...
istringstream valid(input);
...
if(!(valid >> choice))
{
//some error
}
Ok. My code is almost 1000 lines, and I have splited some functions in headers. But the same function doesn't work:
template <typename T> bool valid_input(const string& input, T var)
{
istringstream valid(input);
return (valid >> var);
}
You can check it here: [URL] The output is correct, but in my machine with C++11, MinGW 4.8 (64 bit in a 64bit-Windows8), the output is incorrect. Why?
If you want more specific info, the problem is that I use input, I think. I use std::getline(std::cin, some_string).
View 4 Replies
View Related
Dec 2, 2013
I have been thinking about this all day and I am yet to come up with a good solution. So, I need to design an image class which should work with various data types (int, float, double etc.) and can also be multidimensional (2, 3, 4, 5). So, what I did was generate a template image class as follows:
Code:
template<typename T, int dimensions=3>
class Image {
private:
T * data;
};
Anyway, now I have various image formats that I want to support, so the easy thing to do is create a Factory sort of object which will call eventually generate the correct image.
So I want to create various image classes called ImageType1, ImageType2 etc. which will take the input image and generate the correct Image object. However, I do not want these objects to be templated because they need to be passed from functions and be used in a generic way.
So, at run time I will need to be able to do this…
Code:
class ImageType {
public:
ImageType() {
PolymorphicImage * image = new Image<float, 3>();
}
private:
PolymorphicImage * image;
};
So, I want my ImageType classes to contain the Image object and be able to generate it with the right template arguments at run time. Is there any way to do this without having multiple specialised definitions for ImageType?
View 2 Replies
View Related
Aug 31, 2013
I am trying to code a partition quicksort but the problem here is at the output is in an infinite loop:
The input : 4 3 5 7 2
The expected output : 2 3 4 5 7
But my output is infinite :
2 3 4 5 7
2 3 4 5 7
2 3 4 5 7
.
.
and so on
[Code:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/* Head ends here */
void partition(vector <int> ar) {
[Code] .....
View 5 Replies
View Related
Apr 28, 2014
So I have a small program which is supposed to write and read multiple objects from a file, but for some reason it doesn't write the information when I use "fstream" to create the object from the fstream class, but it does when I use "ofstream". Also, it doesn't read the information from the file no matter if I use "fstream" or "ifstream". I watched a video where this exact code worked just fine, but it just won't work when I run it. I'm using Dev C++ 4.9.9.2, I don't know if that has anything to do with it, I also tried it with Code::Blocks, didn't work either.
Here's the code.
#include <iostream>
#include <fstream>
using namespace std;
class Person
[Code].....
View 3 Replies
View Related
May 21, 2013
This is my code for submitting students but when i use search function the course member is empty
Code: #include "windows.h"
#include "iostream"
#include <io.h>
#include <sstream>
#include <conio.h>
#include <stdlib.h>
#include <iostream>
#define SIZE 5
using std::cout;
using std::cin;
using namespace std;
int menu();
[code].....
View 11 Replies
View Related
Mar 20, 2013
i am facing some problem with qsort() function it work well if the last element of array is larger then the 2nd last element. But in case if last element of array is smaller then the 2nd last it will sort the whole array and remains the last as it is. For example
Code:
int group_id_local[max_j]={2,1,4,5};// it work fine, output should be {1,2,4,5} but if i have this one
int group_id_local[max_j]={2,1,4,3};
// output should be {1,2,4,3}
/* COMPARE FUNCTION FOR USING QSORT()*/
int cmpfunc (const void* a, const void* b)
{
if (*(int *)a < *(int *)b) return -1;
if (*(int *)a > *(int *)b) return 1;
return 0;
[Code]....
why it will not sort the last element?
View 5 Replies
View Related
Mar 23, 2013
If yes then how?
View 6 Replies
View Related
Dec 2, 2013
I have to do a BST project for school and I am almost there. Here is the code:
BINARY_SEARCH_TREE.cpp
#include "stdafx.h"
#include "genBST.h"
#include <iostream>
using namespace std;
[Code].....
When I run the program, it compiles correctly but does not give any output. I'm not sure what else to do. I've tried changing up the code in nodeCount(), leafCount(), NodeCount(), and LeafCount(). I've tried adding a count variable to both nodeCount() and leafCount() but that didn't work. If I fiddle with the functions, I get a whole mess of errors. Currently, the code is stable but just won't output what I want it to.
View 3 Replies
View Related
May 21, 2014
I'm having trouble with getting a sine function to work. All variables are defined earlier in the same section. I have the code in a button (where I figured it would go) but I get the following error:
WindowsFormsApplication2.Math does not contain a definition for 'Sin'
For reference, I am using Microsoft Visual Studio Express 2013, and am coding a Windows Forms Application.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
[Code] ....
I've tried other functions as well (abs, sqrt, etc.) to no avail, as Math only seems to pop up with two options: Equals and ReferenceEquals.
View 2 Replies
View Related
Mar 10, 2013
We have to make a function integrate to work with the other functions given. I had it working before but I would only get all -4 as my answers but only the first one should be -4. what should more or less be put in my integrate function?
#include <iostream>
using namespace std;
typedef double (*FUNC)(double, double, double) = (line, square, cube);
double integrate(double FUNC, double a, double b){
for(int i=0; i<a && i<b; i++){
FUNC = a-b;
[code]......
View 2 Replies
View Related
Jan 18, 2014
Code:
struct lista* del(struct lista* p, char* path1) {
char model[MAX2];
int len;
char ch;
printf("Type model.
[Code] ..... t
This function should delete each element in the list which is the same as this one typed by user. There are no errors, but function doesn't work. It deletes something, but not this element which should.
View 5 Replies
View Related
Feb 15, 2012
I have the following function I would like to convert to work with std:string. I don't understand QT strings at all.
Code:
void FromHexString(const QString &hexText, void* pData, int dataSize) {
for (int i = 0; i < hexText.length(); ++i) {
bool ok = false;
((uint8_t*)pData)[ i ] = hexText.mid( 2*i, 2 ).toInt( &ok, 16 );
}
}
View 1 Replies
View Related
Feb 19, 2013
I wrote a program with function my own function which count the digits of entered number. The problem is whatever i type it shows 0 digits.Why is that?
Code:
#include <iostream>
using namespace std;
int cikCipari (int skaitlis, int cipars);
int main()
[Code] .....
View 7 Replies
View Related
Mar 1, 2013
I am trying to add a void function in Visual Studio 2012 but it doesn't work, meanwhile in Codeblocks it does:
#include <iostream>
using namespace std;
int welcome();
int main(){
welcome();
[Code] ....
View 5 Replies
View Related
Oct 22, 2013
I've created a function where you can choose any bounds for an array based list (positive or negative, as long as the first position is smaller than the last position). However for some reason when I call the print() function in my application program it doesn't do anything. My print function is technically correct (I still have work to do on the output) but I can't figure out why it wont show anything at all. Below is my header, implementation, and main program files, along with results from running the program.
Header File:
//safearrayType Header File
class safeArrayType {
public:
void print() const;
void insertAt(int location, const int& insertItem);
safeArrayType(int firstPlace=0, int maxPlace = 100);
[Code] ....
ResultsEnter the first bound of yourlist: -3(this was my input)
Enter the last bound of yourlist: 7(this was my input)
Press any key to continue...
View 1 Replies
View Related
Sep 18, 2013
How can a member function in my derived class call the same function from its base class?
View 1 Replies
View Related
Oct 13, 2014
We are making a program--but every time we input a value for scanf, the following for loop does not work and the program quits without displaying the for loop's function. We are not getting any errors.
View 11 Replies
View Related
Mar 30, 2013
Say I have 3 classes:
class Player {
public:
virtual func1();
[code]....
Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:
//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);
When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).
View 6 Replies
View Related
Feb 4, 2014
So I have a base class, lets call it base. In base I have a virtual function called update(), update just couts "base" then I have a class derived from base called derived;
it has a function called update(), update just couts "derived" then I create a vector called Vec it's initialised like this:
std::vector<base> Vec;
then I add an element into it like this
Derived DerElement;
Vec.push_back(DerElement);
then when I type:
for (int i=0; i<Vec.size(); i++) {
Vec.at(i).Update();
}
It outputs:
Derived DerElement2;
DerElement2.Update();
and it outputs this:
#include <iostream>
#include <vector>
class Base {
public:
virtual void Update() {
[Code] .....
and this is it's output:
Base
Derived
Press any key to continue . . .
View 1 Replies
View Related
Aug 21, 2013
I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.
I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.
I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.
Lets have it as a sample code:
class Timer {
private:
int x;
public:
Timer();
get_X();
start_X();
[Code] ....
View 4 Replies
View Related