|
Very true, I was hoping that the person would scrutinize that line and understand that it probably didn't accomplish what they were trying to do.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
I have a project and this is what the project is about :
Implement a template library handling matrices.
the library must offer operation like:
• submatrix: return a matrix that contains a contiguous subrange of rows and columns of the original matrix
• transpose: return a matrix with rows and columns inverted
The matrices returned by such operators must share the data with the original matrix, but direct copy of the matrices and vectors must result in a deep copy (sharing a matrix can be obtained by taking a submatrix extending the whole row and column ranges).
Create iterators for the matrix traversing the whole data in any order.
Provide matrix addition and (row-by-column) multiplication as overloaded operators
so i did everything and i am stuck in how to implment operator overloaded for multiplication and addition, i mean should i creat a class for this operation or should i implment them in the main and how to do this.
this is the matrix interface: <pre>#include <iostream>
#include <vector>
#include <memory>
using namespace std;
template < typename T>
class matrix_interface {
public:
virtual int getCol() const = 0;
virtual int getRow() const = 0;
virtual T get(int row, int col) const = 0;
virtual T& get_ref(int row, int col) = 0;
virtual ~matrix_interface() {};
};
template < typename T>
class standard_matrix : public matrix_interface <T>{
private:
int row,col;
vector <T> data;
public:
int getRow() const override {
return row;
}
int getCol() const override {
return col;
}
standard_matrix(const vector<T> &data, int row, int col) : row(row), col(col), data(data) {
if (data.size() < row*col)
throw ("the vector is invalid");
if (row < 1 || col < 1 ){
throw ("the dimensions are invalid");
}
}
T &getRef(int r, int c) override {
if (r < 1 || r > row) {
throw "Row is off limit";
} else if (c < 1 || c > col) {
throw "Column is off limits";
} else{
T& t= data.at(col * (r - 1) + c - 1);
return t;
}
}
T get(int r, int c) const override {
if (r < 1 || r > row) {
throw "Row is off limit";
} else if (c < 1 || c > col) {
throw "Column is off limit";
} else
return data[col * (r - 1) + c - 1];
}
};
template <typename T>
class transposed_matrix : public matrix_interface <T>{
private:
shared_ptr< matrix_interface<T> > mat_ptr;
public:
transposed_matrix (shared_ptr < matrix_interface <T> > x):mat_ptr(x){
}
int getRow() const override {
return mat_ptr->getCol();
}
int getCol() const override {
return mat_ptr->getRow();
}
T get(int row, int col) const override {
if (row < 1 || row > getRow()) {
throw "Row is off limit";
} else if (col < 1 || col > getCol()) {
throw "Col is off limit";
} else
return mat_ptr->get(col, row);
}
T &getRef(int row, int col) override {
if (row < 1 || row > getRow()) {
throw "Row is off limit";
} else if (col < 1 || col > getCol()) {
throw "Col is off limit";
} else
return mat_ptr->getRef(col, row);
}
};
template <typename T>
class submatrix_interface : public matrix_interface <T>{
private:
shared_ptr< matrix_interface<T> > mat_ptr;
int rowSet, colSet, row, col;
public:
submatrix_interface(shared_ptr < matrix_interface <T> > x,
int row_start, int col_start, int row_end, int col_end)
:mat_ptr(x){
if (row_start < 1 || col_start < 1 || row_end > mat_ptr->getRow() || col_end > mat_ptr->getCol()
|| row_start > row_end || col_start > col_end)
{
throw "the input of the submatrix entered is not valid.";
}
else {
rowSet = row_start - 1;
colSet = col_start - 1;
row = row_end - row_start + 1;
col = col_end - col_start + 1;
}
}
int getRow() const override {
return row;
}
int getCol() const override {
return col;
}
T get(int row, int col) const override {
if (row < 1 || row > getRow()) {
throw "Row is off limit";
} else if (col < 1 || col > getCol()) {
throw "Column is off limit";
} else {
return mat_ptr->get(row + rowSet, col + colSet);
}
}
T &getRef(int row, int col) override {
if (row < 1 || row > getRow()) {
throw "Row is off limit";
} else if (col < 1 || col > getCol()) {
throw "Column is off limit";
} else {
return mat_ptr->getRef(row + rowSet, col + colSet);
}
}
};
this is the matrix header:
<pre>#include "iterator.h"
#include<iostream>
using namespace std;
template < typename Type>
class matrix_base;
template < typename Type >
class matrix {
protected:
shared_ptr < matrix_interface <Type> > matrixIntPtr;
public:
matrix() {}
matrix (const matrix & m){
vector < Type > v;
for (auto i = m.begin(); i != m.end() ; i++) {
v.push_back(*i);
}
matrixIntPtr = make_shared <standard_matrix <Type> > (v,m.row(),m.col());
}
matrix(const vector<Type> &data, int r, int c) {
matrixIntPtr = make_shared <standard_matrix <Type> > (data,r,c);
}
Type row() const{
return matrixIntPtr->row();
}
Type col() const{
return matrixIntPtr->col();
}
Type operator ()(int r, int c) const{
return matrixIntPtr->get(r,c);
}
Type& operator ()(int r, int c) {
return matrixIntPtr->getRef(r,c);
}
row_iter <Type> begin() const{
return row_iter <Type> (matrixIntPtr, 1, 1);
}
column_iter <Type> col_begin() const{
return column_iter <Type> (matrixIntPtr, 1, 1);
}
row_iter <Type> end() const{
return row_iter <Type> (matrixIntPtr, matrixIntPtr->row()+1 , 1);
}
column_iter <Type> col_end() const{
return column_iter <Type> (matrixIntPtr, 1 , matrixIntPtr->col()+1);
}
matrix_base <Type> submatrix(int first_row, int first_col, int last_row, int last_col) const
{
return matrix_base <Type> (make_shared <submatrix_interface <Type> >(matrixIntPtr, first_row, first_col, last_row, last_col));
}
matrix_base <Type> transpose() const {
return matrix_base <Type> (make_shared <transposed_matrix <Type> >(matrixIntPtr));
}
and this is the iterators header:
<pre>#include "matrix_interface.h"
template < typename T>
class column_iter{
private:
shared_ptr < matrix_interface <T> > sharedPtr;
unsigned row, col;
public:
column_iter(shared_ptr<matrix_interface<T> > ptr, unsigned row, unsigned col)
: sharedPtr(ptr) , row(row) , col(col) {}
column_iter& operator ++(){
row++;
if (row == sharedPtr->col() + 1) {
row = 1;
col++;
}
return *this;
}
column_iter& operator ++(int){
column_iter tmp(*this);
operator++();
return tmp;
}
const T& operator *(){
return sharedPtr->getRef(row,col);
}
bool operator == (const column_iter& x){
return col == x.col && row == x.row;
}
bool operator != (const column_iter& x){
return ! operator == (x);
}
};
template < typename T>
class row_iter{
private:
shared_ptr < matrix_interface <T> > sharedPtr;
unsigned row;
unsigned col;
public:
row_iter(shared_ptr<matrix_interface<T> > ptr, unsigned row, unsigned col)
: sharedPtr(ptr) , row(row) , col(col) {}
row_iter& operator ++(){
col++;
if (col == sharedPtr->col() + 1) {
col = 1;
row++;
}
return *this;
}
row_iter& operator ++(int){
row_iter tmp(*this);
operator++();
return tmp;
}
const T& operator *(){
return sharedPtr->getRef(row,col);
}
bool operator == (const row_iter& x){
return col == x.col && row == x.row;
}
bool operator != (const row_iter& x){
return ! operator == (x);
}
};
|
|
|
|
|
|
How to find different between current system time and user given time in c (only C not in c++) program. Is there any method that I get only time (without date) as output (not as char as time_t type).
|
|
|
|
|
You could calculate the number of seconds from midnight for current time and user given time. Subtract the two and convert back to hours, minutes seconds:
time_t now;
time (&now); struct tm *systime = localtime(&now);
struct tm user_time = {56, 34, 12}; int sys_sec = systime->tm_hour*3600 + systime->tm_min*60 + stystime->tm_sec;
int user_sec = user_time.tm_hour*3600 + user_time.tm_min*60 + user_time.tm_sec;
int diff = sys_sec - user_sec;
Of course you have to figure out what to do when time wraps around at midnight but that's your problem . I'm just showing how you can shoot yourself in the foot
Mircea
modified 31-Oct-20 14:15pm.
|
|
|
|
|
Mircea Neacsu wrote: struct tm *systime = localtime(&now); I've never understood declaring a variable as struct* .
struct is a keyword used to define a UDF. It's not a type in itself.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
tm is a regular structure and localtime (as well as gmtime) return a pointer to an internal buffer.
Quoting from cplusplus.com:[^]
Quote: A pointer to a tm structure with its members filled with the values that correspond to the local time representation of timer.
The returned value points to an internal object whose validity or value may be altered by any subsequent call to gmtime or localtime.
Now that I look again at the code I see that I missed the call to time(&now) . Oops!
Mircea
|
|
|
|
|
It is simply a pointer to a structure that gets filled in by the system call. What is wrong with that?
|
|
|
|
|
It bothers me because it would be akin to saying:
class *Foo = new Foo(); Is that legal C++?
EDIT:
Oh wait, I didn't notice that there is a struct name followed by a variable name. I see now.
The difficult we do right away...
...the impossible takes slightly longer.
modified 31-Oct-20 16:37pm.
|
|
|
|
|
Richard Andrew x64 wrote: Is that legal C++? Yes, and necessary. Pointers must actually point to something, or they are just empty space.
|
|
|
|
|
You misunderstood what I was demonstrating. I was demonstrating declaring a variable of type "class*". Is it possible to declare a variable of type "class"? Is there such a type?
I don't think that's legal in C++. I haven't tried it though.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: Is it possible to declare a variable of type "class"? No, because class is an abstract concept, not a type in the same sense that int is. The actual type will be the name used in the class definition. For example consider the following:
class Foo
{
public:
Foo() { std::cout << "Constructing a Foo object" << std::endl; }
};
int main(int argc, char** argv)
{
Foo aFoo;
Foo* pFoo = new Foo();
}
A new object aFoo is created and stored on the local stack. A second new object is instantiated on the heap and its address passed back to be stored in the pointer pFoo . The type of both objects is Foo not class .
Does that make sense?
modified 2-Nov-20 6:39am.
|
|
|
|
|
You can use the difftime() function to subtract two time_t values. If you have the time in a struct tm, you can convert it to time_t using the mktime() function. The strftime() function formats a struct tm to text format.
These APIs are all available in C90, but some options were introduced as of C99.
difftime() should incorporate leap-seconds for past dates, but I don't know if actual implementations do so.
Note that issues such as daylight-saving time, time zones, etc. are left to the programmer. Ignoring these is a common way for programmers to shoot themselves in the foot.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Hi all,
PLease help me for Searching and displaying a record in Array of Structure in c. I think i am meesing up with pointer and array index. PLease help
struct employee{
int emp_id;
char emp_name[30];
}emp[MAX];
void findAllDetails(int *e){
printf("\n Employee Details...\n");
printf("\n Emp No : %d", emp.emp_id);
printf("\n Emp Name : %s", emp.emp_name);
}
void PrintAllDetails(int &K){
for(i=0;i<MAX;i++){
if(K==&emp[i].emp_id)
break;
if(i<MAX)
{
findAllDetails(&emp[i]);
}
}
modified 29-Oct-20 22:51pm.
|
|
|
|
|
I would have written something similar to
#include <stdio.h>
#define EMPLOYEE_ARRAY_SIZE 10
#define NAME_MAX_LENGTH 30
#define NOT_FOUND -1
struct employee
{
int id;
char name[NAME_MAX_LENGTH];
};
int find_employee_by_id( const struct employee emp_array[], int emp_array_size, int id);
void print_employee( const struct employee * pemp );
int main()
{
struct employee emp_array[EMPLOYEE_ARRAY_SIZE] =
{
{ 1, "foo"}, {2, "boo"}, {3, "goo"}, };
int id = 2;
int index = find_employee_by_id( emp_array, EMPLOYEE_ARRAY_SIZE, id );
if ( index != NOT_FOUND )
{
printf("found emplyee details:\n");
print_employee( & emp_array[index] );
}
else
{
printf("employee with id = %d not found\n", id);
}
return 0;
}
int find_employee_by_id( const struct employee emp_array[], int emp_array_size, int id)
{
int index;
for (index = 0; index < emp_array_size; ++ index)
if ( emp_array[index].id == id)
return index;
return -1;
}
void print_employee( const struct employee * pemp )
{
printf("id = %d\n", pemp->id);
printf("name = %s\n", pemp->name);
}
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
I need my existing MDI application in MFC to be transformed with the below requirement.
Every window should look like a completely separate instance of the application – with its own button in the Windows Taskbar. However the windows should be still part of the single application.
If there are multiple monitors used, each window of my application can be viewed in the various montitors
Please give your suggestions how to convert
|
|
|
|
|
Make a client-server architecture. Each window (running as a separate program) is nothing but a display facilty, just like a web browser, with no application logic, no (application) data structures. Everything is done in your server, i.e. your old application deprived of display facilities.
The architecture would be like a database accessed across the web.
|
|
|
|
|
My Application takes very less data (configuration) from text files, rest are all calculations done every second for around 20000 tags or values. If I do it using TCP/IP Sockets that my application may be overloaded for each window. Is there any other option in MFC?
|
|
|
|
|
I do not work with MFC myself, so this is just results from a little documentation searching.
TCP/IP is certainly not a lightweight mechanism, but pipes are. In the original Unix design (from the early 1970s), the processes in either end had to have a common ancestor, creating the pipe, but later we got named pipes, CreateNamedPipeA function[^] so that arbitrary processes can communicate across them. This has far less overhead than an TCP/IP socket.
You also have the CSharedFile Class[^] and CMemFile Class[^], making RAM look like a file; you use file operations to access RAM.
There is the opposite function: To make a disk file look like RAM, a "memory mapped file". In the old days, you couldn't do that from C#, you had to use C/C++. Now I find only the dotNet version (at MemoryMappedFile Class[^]), but of course it is still is provided in C/C++; I just can't find it (I would think that CSharedFile and CMemFile are thin layers on top of that mechanism.) You may be more clever than I am in finding the C++ equivalent. It is there, somewhere...
A memory mapped file does not require an underlaying disk file; you use the same facility for creating a data segment accessible from multiple processes. Using a shared segment, a.k.a. a memory mapped file, is certainly the fastest way to exchange data between processes, comparable to threads accessing shared data within a process: The memory management system handles it. The greatest advantage, compared to a file/pipe solution: You don't have to serialize (or marshal, or whatever your call it) your data structures, but can access them "as is". For pointer based structures, all processes accessing the shared data segment obviously must map it to the same logical address. Equally obvious: You are responsible for all synchronization yourself, which must be done with semaphores (or whatever higher level mechanism) at system level, available to all processes. It will be somewhat more costly than a single-process implementation, but it doesn't have to be much, especially if there a single process (the server) updating the data, other processes only reading/displaying them.
|
|
|
|
|
|
Still, IPC may be one way to solve is problem.
|
|
|
|
|
I cannot disagree...
however, it depends.
|
|
|
|
|
Thanks that you got my requirement correctly. All I need... is a single application in which all the opened documents looks like in MS Office (Word, Excel).
There is already an option in MFC to create application type as "multiple top level documents" which meets my requirements.
Can you please suggest how to convert my existing MDI application from "Multiple Document Interface" type to "multiple top level documents" type.
Please help.
|
|
|
|
|
|
There is an option in MFC to create application type as "multiple top level documents" which meets my requirements.
Can you please suggest how to convert my application from "Multiple Document Interface" type to "multiple top level documents" type.
Please advise.
|
|
|
|
|