C++ :: Implementing Template Operator - Numerical Array
Jun 19, 2012
I what to implement to my Template operator * . There is <Template> Array which purpose is container like vector for classes. There is class Point, each object of contain two coordinate x and y.
So,
1. I wanna fill Array with objects from Point class
2. Multiply each objects from this vector to a factor
3. And print all this bunch of objects ()...
And pop -up helper tell that : Error: no suitable user defined conversion from "Point " to Array<Point> exist
Code:
//array.h
#ifndef Array_H
#define Array_H
template <class Type> //Remove the "=double" default parameter.
class Array {
protected:
int m_size;
Type* m_data; //m_data should be a pointer, since you want to allocate data to it
I'm struggling a bit to combine templates and operator overloading.
I'm writing a program that will be able to perform various matrix arithmetic at the end. However I'm stuck at the overloaded << and >> functions. I've defined them as I'm used to, without working with templates, however they are incorrect as I have defined them, and I'm not sure how to fix it.
I want to overload the stream insertion operator >> to read matrix data from either the keyboard or a file stream. I want to use >> to input a 3 x 3 matrix stored in a text file in the following format:
2 4 3 5 2 3 7 1 0
And similarly I want to overload the stream extraction operator << to output a matrix to the screen or file in the following format:
3 2 -1 1 -1 2 2 3 -1
Here is my work so far:
matrix.h
#ifndef MATRIX_H #define MATRIX_H #include <cassert> #include <iostream> using namespace std; template <class T> class Matrix;
The following program is designed to demonstrate class templates. The program will evaluate postfix expressions input to the console by pushing them in a stack, and then popping them and evaluating them as such, (ex. entering 3 4 + would equal 3+4 = 7).
The code is below. We are not to modify it, but to fill in the blanks, the places filled in indicated with two asterisks for a line, and one on each side for a part of a line. If I didn't know what to enter (if anything), I put three ?s. If you want to copy and compile for yourself, look for all the *s and ?s.
1) I'm turning up all sorts of errors in the main program file (prog5.cpp) having to do with stacktype.cpp. It has been removed from the program, as it is included at the end of stackType.h. Most of them are "cannot convert 'this' pointer from StackType to StackType<stack> &'. How do I fix that?
2) The program supposedly lacks a default constructor, and it keeps turning up that 's' is an array of unknown size (do I call StackType or stack or what?).
stackType.h Code: #pragma once// Catherine Stringfellow and Trey Brumley // A Stack is a data type, which stores values in an order where values are added to the top of the stack when pushed, // and when popped, remove and return the value from the top of the stack. // Class specification for Stack ADT in file StackType.h using namespace std; static const int MAXITEMS = 50;
im trying to make a statistics program and i have an array holding all the users numbers. The array has 10 elements and i was wondering how i could put these elements in order from smallest to largest.
I need to find the Mean, median, mode, and make a histogram of a 99 value array. How ever my sorting function is not sorting the array at all how can I fix this.
I can't provide a generic implementation of the << operator, it depends on the specific types of T. So how do I specialize the operator for a specific type T ?
main3.cpp: In member function ‘FooB& FooB::operator=(const FooC&)’: main3.cpp:46:44: error: expected ‘(’ before ‘other’ main3.cpp:46:49: error: no matching function for call to ‘Foo<C>::Foo(const FooC&)’ main3.cpp:46:49: note: candidates are: main3.cpp:19:2: note: Foo<T>::Foo() [with T = C] main3.cpp:19:2: note: candidate expects 0 arguments, 1 provided main3.cpp:16:25: note: Foo<C>::Foo(const Foo<C>&) main3.cpp:16:25: note: no known conversion for argument 1 from ‘const FooC’ to ‘const Foo<C>&’
I designed a class template to create unique arrays. I was able to successfully input data to and output data from my array objects, irrespective of the datatype. However, I can't for the life of me fathom why my overloaded assignment operator worked perfectly well only for integer datatype and not for double/string datatypes.
Here is the class definition:
template <class dataType> class myArray { public: void setArrayData();
[code]....
And here is the definition of the overloaded assignment operator:
And here is my main function that tests the operations on objects of the class:
int main(){ //object declarations myArray<double> list(5); //a single-parameter object declaration of class myArray myArray<double> myList(2,13); //a two-parameter object declaration of class myArray
[code]....
The problem I'm having starts from where the assignment operator is being tested: for double and string datatypes, the upper input/output section works fine, but the assignment section freezes the display until the program execution is manually terminated!
I have to write a function called sortMe that sorts the elements of an array in numerical order from highest to lowest values (descending order) or vice versa (ascending order).
The assignment asks to: NOT re-arrange elements in the array; instead, it uses a second array, an array of indexes for the elements in the original array and then sortMe sorts the second array based on the values in the original array. A sorted version of the original array can then be produced with these sorted indexes.
Header of the function sortMe must be as shown below:
void sortMe(int array[],int sortedIndexes [], int size, char mode)
When mode is 'a', the function sorts the array in the ascending order, and when mode is 'd', the function sorts it in the descending order.
Declare and initialize the array array.
Declare the array sortedIndexes but do not initialize it. You are going to play with the array sortedIndexes in the function sortMe.
EXAMPLE:
int array[5]={3, 5,-1,10,0}; int sortedIndexes[5]; sortMe(array,sortedIndexes, 5, 'a');
After the function call, the elements of the array sortedIndexes should be: 2,4,0,1,3.
notice that the function does not e-arrange the elements in the array.
I'm trying to implement a tree from an array an first I used add_left_node() and add_right_node methods (as requested in the problem I have). But I'm a bit confused about the use of pointers in this. Is using &node the same as defining *pointer=node and then using "pointer" in place of &node?
I get only the value of the first node I created. For the node's left and right values I get long numbers displayed, so I think I have interfered with the addresses.
i can't implement trie with dynamic array .the problem is in this line i think :
Code:
childs_size = (node_p -> childs_value_p)[0] + 1; here is my code : Code: #include <stdio.h> #include <stdlib.h> #include <string.h> struct trie_node { unsigned char *childs_value_p; // pointer to an array of child nodes value struct trie_node **childs_ptp; // pointer to an array of child nodes pointer struct trie_node *failure_node_p; // pointer to failure node
[code]...
i could write this code with binary tree instead of dynamic array but need a large amount of memory for about 13000000 strings of length 16 . is there any better solution with lower memory usage to implement trie ?
I am studying/writing/ code for a future Advanced Data Structure class in C++; however I am not suppose to use STL, Templates and etc (the way I just code "kinda" of resembles what I have to use).
My application is suppose to read/analyze all integers contained in a text file of 5-digit integers (10000 - 99999).
For simplicity I am using the following input (or check the attached):
So far, my code is not displaying/printing the lists separated by the first digit of these 5-digits integers. I am expecting it to display/print logically/similar to the following:
Output:
Results for input file numbers.txt: 27 total integers read from file
The 3 unique integers beginning with digit 1 were 18399 17342 19948
The 6 unique integers beginning with digit 3 were 39485 34710 31298 38221 35893 32791
The 4 unique integers beginning with digit 4 were 43928 49238 45678 43210
The 6 unique integers beginning with digit 6 were 64545 62987 66221 61777 66666 65432
The 2 unique integers beginning with digit 8 were 88888 86861
The 1 unique integer beginning with digit 9 was 98765
There were 22 unique 5-digit integers in the file.
The highest unique count in one list was 6 integers.
My code that will follow soon displays/prints only the LAST 5-digits "group" of integers (in this case the 5-digits starting with 3). I am not sure what's wrong with my code; perhaps I am not designing it correctly. May be my calls in it are on the wrong place or I have to write all integers and then traverse it and output it (if that's the case, I am not sure how).
My code follows:
#include <iostream> #include <fstream> #include <iomanip> using namespace std; const int MAX_CELLS = 10; const int UNIQUE_FIVE_DIGIT = 5;
I am looking for C-libraries that would do numerical tasks with special emphasis on numerical differentiation of arbitrary order, numerical integration (Monte Carlo etc.) and interpolation. I would be grateful if you can provide me with some resources. By the way I already have Numerical Recipes but would like to have another resource at hand.
I have the problem of trying to find the smallest natural number that makes two consecutive terms in single precision floating point notation in the riemann zeta function equal. So basically the riemann function of 2 is given by:
sum of 1/(k^2) from k=1 until infinity, so : 1/(1^2) + 1/(2^2) + 1/(3^2) + ...... until infinity.
Now the question asks to stop at the smallest natural number n, at which the sum 1/1^2 + 1/2^2 + ......+ 1/(n^2) is equal to the sum 1/1^2 + 1/2^2 + ..... + 1/((n+1)^2) in single precision floating point notation.
Now well the obvious way to look for n would be on this way:
float i = 1.0; float n = 1/(i); float n1 = 1/(i+1.0); while ( n != n1){ i += 1.0; n = 1/i; n1 = 1/(i+1.0);}
But first of all this is obviously completely inefficient and I dont think it will yield a valid answer for any float variable, i.e. I dont think the sum until 1/n^2 and 1/(n+1)^2 will ever differ. I tried it out with the largest possible value of a variable of type float and the values were still seen as unequal in C++. How to do this? Will C++ find a value for n for which the condition holds? Is the compiler or my hardware important for this, i.e. would I get different results on a different pc?
I'm doing a refresher for C++ and have gotten to operator overloading. I'm trying to perform an operator overload with the insertion (<<) operator, but I have encountered a problem.
Here's my class [In a header file "Shinigami.h"]
#include<string> namespace K{ class Quincy; class Shinigami{ friend std::ostream& operator<<(std::ostream&, const Shinigami&);
[Code] .....
If the operator function is a friend of the 'Shinigami' class, why doesn't it recognize any of it's private members? I need it to be in this file because I'm doing a bit of association with the 'Quincy' class.
I thought it was the namespace, but I included that.
I am having a problem on how will I create a large matrix to be solved using numerical methods. Say I have a 500 x 500 matrix and want to find these 500 equations. How will I do it? To be exact, here is a sample problem
Solutions on finding would be gauss elimination, Successive overrelaxation, Jacobi Iteration, biconjugate gradient method and Thomas algorithm. I want to compare the accuracy, with tolerance of 10^-6
I have to code a simple program who determining the number of Characters (A character could be any alphabets, digits, punctuation marks, or special , Operators ( Operators are those symbols that are used in mathematica expression, such as,'+', '*', '/', '-', and so on.), Uppercase letters (Uppercase characters are those from A..Z) and Numerical digits ( A digit is any of the Hindu-Arabic numerals from 0..9). Why the output is wrong!
#include <iostream> #include <string> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std ; int main() { char text;
[Code] .....
This is my input file This is a possible factorial function in a programming language called LISP
(defun factorial (n) (if (< n 2) 1 (* n (factorial (1- n)))))
This is my output:
The number of characters = 113 The number of operators = 3 The number of numerical digits = 3 Uppercase letters = 5
I think that "characters" is wrong, but I do not know why !
We can't use the std::vector, std::map and std::lists implementations.
std::realloc doesn't call the destructor automatically.
If it is theoretically possible, we'll consider it and write lines similar to it, which should remain commented until something similar is possible.
int main(){ #define START 1000000000 #define END 10 unsigned int * array=new unsigned int[START]; delete[START-END]array;//Our suggestion to shrink the array delete[]array;//Delete the array return 0; }
Right well I've got this error and I don't know where to go from here because I can't find out how to get rid of it. I'm afraid I don't actually know where the error is coming from so I can't just paste a single function or such.
There are 2 files directly involved and I've had the error since before adding Registry.h but basically the Data namespace is the base of all data handling with my program and also provides a base class (Filed) for everything to derive from, it's an abstract class and hence forces definition of required functions for the child classes.
struct b { char fullname[100]; char title[100]; bopname };
and i declare a pointer to the struct as follows
b* bp;
and at runtime,
bp = new b[5];
In the end, is the statement delete[ ] bp enough to release the dynamically allocated memory. Or do i need to separately delete bp[0],bp[1]...
Does delete[ ] bp indicate that the array[ ] pointed by bp has to be deleted?? [I am confused as to how this happens, since the pointer only points at the 1st struct address]