C++ :: How To Set Pointer And Iterate Through A Matrix
Aug 8, 2012
How can a set a pointer , to a matrix example matrix[20][20] and iterate ( loop ) in each row first sort of X first and then Y through it using the pointer , how to setup dynamic allocation
through pointer = new matrix[20][20]
and finally call delete on pointer when done with the matrix.
View 4 Replies
ADVERTISEMENT
Jan 19, 2014
I am trying to iterate a matrix in order to exchange rows and columns element by element. Although the function that exchanges the rows has come out well, i don't seem to figure out how to do the same on columns.The columns switch for a matrix like
1 2 3
1 2 3
1 2 3
if i try to switch column 3 and 2,is:
1 3 2
1 3 2
1 2 3
Here are both of the functions:
void interchange_rows(int *p,int n,int r1,int r2){
int temp;
for(int i=0;i<n;i++){
temp=*(p+r1*n+i);
*(p+r1*n+i)=*(p+r2*n+i);
*(p+r2*n+i)=temp;
[Code] ......
View 1 Replies
View Related
Jun 20, 2014
i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):
#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)
[Code]...
View 6 Replies
View Related
Oct 29, 2014
I want to navigate a 2D Matrix (for example: int iMatrix[10][10]), with a pointer.
I'm not sure how to traverse this matrix in both demensions with a single pointer.
I want to know because I'm going to pass a pointer of this matrix into a function and do work with it.
View 4 Replies
View Related
Mar 27, 2013
i want to know how i can solve this question? do i need to create a class or write the program codes.
View 12 Replies
View Related
Jun 1, 2014
I just want to know the code of the program: Write code to accept matrix as aurgument and display its multiplication matrix which return its multiplication matrix.
View 1 Replies
View Related
Nov 19, 2013
I've come to a point in tuning my algorithm, where I need to sum the elements of two arrays, eg.:
array1: (1,1,1)
array2: (2,2,2)
arrayOut: (3,3,3)
Is there any way how to do that without need to iterate? Some memory hacks?
View 10 Replies
View Related
Apr 5, 2013
So I have a vector that I want to iterate through randomly, and by random I mean that each element is only accessed once but I don't want to shuffle the vector because the vector elements are large. So I want this functionality:
std::vector<SomeLargeObjectWithoutACopyConstructor> myvec;
// ...fill myvec
std::random_shuffle(myvec.begin(),myvec.end());
for (auto& value : myvec)
{
// do stuff
}
Only I don't want to do this because the object type has no copy constructor and is large, so I don't want to shuffle the vector, just the iteration path. Is there a simple and efficient way of iterating randomly through a vector while ensuring that each object is only accessed once?
View 3 Replies
View Related
Sep 6, 2013
How can I use one index to iterate between all the values
for example:
//I defined
vector<typeA> VA(XD,typeA());
vector<typeB> VB(XD,typeB());
//then the standard method to iterate is:
for(vector<typeA>::Size_type i=0; i<VA.size(); i++) {
VA[i].functionA();
VB[i].functionB();
}
the other way, use int,
//I defined
vector<typeA> VA(XD,typeA());
vector<typeB> VB(XD,typeB());
//then the standard method to iterate is:
for(int i=0; i<VA.size(); i++) {
VA[i].functionA();
VB[i].functionB();
}
none of above make me feel formal...
View 7 Replies
View Related
Apr 25, 2014
How do I iterate through a two dimensional vector? To iterate through and print the elements of a single dimensional vector I use:
vector<int> a;
for(vector<int>::iterator it=a.begin();it<a.end();it++)
cout<<*it;
How do I do the same for a two dimensional Vector?
View 2 Replies
View Related
Oct 31, 2013
so i have a container called Source that is of type std::map<std::string, std::vector<std::string>> but when I try to print it with this function:
void Lexer::PrintSource()
{
for(auto Iterator = this->Source.begin(); Iterator != this->Source.end(); Iterator++)
for(auto SubIterator = Iterator->begin(); SubIterator != Iterator->end(); SubIterator++)
cout<< *SubIterator << endl;
}
i get these errors:
Lexer.cpp: In member function 'void Lexer::PrintSource()':
Lexer.cpp:29:42: error: 'struct std::pair<const std::basic_string<char>, std::vector<std::basic_string<char> > >' has no member named 'begin'
for(auto SubIterator = Iterator->begin(); SubIterator != Iterator->end(); SubIterator++)
^
Lexer.cpp:29:76: error: 'struct std::pair<const std::basic_string<char>, std::vector<std::basic_string<char> > >' has no member named 'end'
for(auto SubIterator = Iterator->begin(); SubIterator != Iterator->end(); SubIterator++)
^
View 6 Replies
View Related
Feb 27, 2012
I want to assign a matrix to submatrix of a bigger matrix.
ublas::matrix<int> A(8,5);
ublas::matrix<int> B(2,5);
for(size_t i=0;i<A.size1();++i)
for(size_t j=0;j<A.size2();++j)
A(i,j)=i*A.size2()+j+1;
for(size_t i=0;i<B.size1();++i)
for(size_t j=0;j<B.size2();++j)
B(i,j)=i*B.size2()+j+5;
ublas::matrix_range<ublas::matrix<int> > a(A,ublas::range(0,2),ublas::range(0,5));
a=B;
and it works.
but if the matrix is compressed_matrix type, there's something with it. the error log as below:
Check failed in file boost_1_48_0/boost/numeric/ublas/detail/matrix_assign.hpp at line 1078:
detail::expression_type_check (m, cm)
terminate called after throwing an instance of 'boost::numeric::ublas::external_logic'
what(): external logic
Aborted
I'm not sure this is a bug or not.
View 2 Replies
View Related
Oct 5, 2014
I need to allow the user to input an integer of any length and print out the word of each number in the integer. So the user would enter "324562" and then the output would be "three two four five six two". I'm struggling to understand the best way to do it in C. I know I need a loop that goes through the int but I don't know how to do it
View 7 Replies
View Related
Mar 21, 2014
I need to iterate through a vector in a const function, and, as my function is called very often, to get more performances I need my iterator to be declared somewhere else than the function, so it doesn't have to get deleted and recreated over and over again. So here is my code:
class Shop {
public:
//methods
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const{
[Code] ....
Seems great? Well no. I actually get an error on the "for" line.
It tells me : "You can't use '=' here" and "You can't use the ++ operator here"
The thing is, if I actually declare my iterator in the loop, the compiler stops giving me warnings, but, as I said, I really want to avoid doing that.
View 4 Replies
View Related
Jan 14, 2014
A C++ container type called std::map is used to store elements that are formed by a combination of key value and mapped value. How do you iterate through this container and find the value mapped to a specific key?
View 3 Replies
View Related
Nov 26, 2012
The example enable a client to iterate the internal std::vector using being() and end().
Code:
class foo {
public:
typedef std::vector<std::string>const_iterator iter;
iter begin () const;
iter end () const;
[Code] .....
In the future I see the need for this class to be able to control sequence (sorting) and also show a subset of the complete list based on a search parameter.
Using std::sort appear to solve the ability to sort the collection.
How can I return an iterator to the client which only iterates a sub-set of all items in the std::vector?
An example would be, I add this method to the class;
Code:
void find(const std::string& st);
So if the client performs (below) only items in std::vector that contains the character "a" should be possible to iterate.
Code:
foo f;
f.search("a");
One option would be to operate with two collection inside the foo class. One more static containing all items and the other containing the sorted and filtered items. This would lead to some copying but should work. Far from perfect.
View 2 Replies
View Related
Mar 18, 2014
I'm having trouble getting my loop to work correctly. If I iterate the for loop once it works as expected and displays proper output. When I try to iterate two times or more the program gets stuck in an infinite loop.
testData8.dat:
Code:
12 9
13 756
View 3 Replies
View Related
Dec 21, 2014
I need to use dynamic memory allocation and use pointers to iterate through the arrays that I have already in this program. I am lost, nothing I do works and where to use the pointers. I am just looking for a push in the right direction so I can finish this project and how I can implement pointers in my program.
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <stdio.h>
using namespace std;
[Code]...
View 1 Replies
View Related
Nov 23, 2014
So I have an issue with a homework assignment that I am coding. I am attempting to get a function to iterate through an array and search for a number that was stored in an array by the user. So far I can take the number, get the numbers displayed but in my menuChoice2 function, for some reason the program is not confirming whether or not the number is entered, and is only telling me that the number has not been found, instead of confirming that the number is in the array.
Here is my code thusfar:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Variables that are Globally declared
int menuChoice = 0;
int usernum[1000] = { ' ' };
[Code] .....
To be clear I am not getting any errors but something is telling me that the error is in the formatting of menuChoice2.
View 14 Replies
View Related
Jan 20, 2014
So I have this problem with not being able to iterate over a vector in a nested for-loop. Here's the nested for-loop:
bool innerHit = false;
for (std::vector<Sprite*>::iterator outerIter = sprites.begin(); outerIter != sprites.end() && (!sprites.empty()); outerIter++) {
Sprite* spriteOne = *outerIter;
for (std::vector<Sprite*>::reverse_iterator innerIter = sprites.rbegin(); innerIter != sprites.rend() && (!sprites.empty()); innerIter++) {
Sprite* spriteTwo = *innerIter;
[Code] .....
What happens is, after having called the collisionDestroy-function and the program tries to execute the nest loop in the outer for-loop, it all crashes with the text "Expression: vector iterator not decrementable", which I understand is because the iterator will have already become useless. The question is: know this, how do I fix it? I can't seem to get a hang of it.
Here's the collisionDestroy-function (the collisionReaction does nothing but sets a few local variables):
void Enemy::collisionDestroy(std::vector<Sprite*>& sprites) {
for (std::vector<Sprite*>::iterator iter = sprites.begin(); iter != sprites.end(); iter++) {
Enemy* tmp = dynamic_cast<Enemy*>(*iter);
if (this == tmp && collisionType == 3 || collisionType == 1) {
sprites.erase(iter);
break;
}
}
}
View 14 Replies
View Related
Apr 8, 2013
I am trying to write a loop that will iterate through a char array in C and pull the IP addresses and test them to see if they respond. My ultimate goal is to have the first one that responds returned in the function, but the loop is not running correctly. It will run through right the first time, but then will start over again.
Code:
const char * getServer(char *svrList) {
char *srList;
srList = strtok(svrList, " ," );
printf("Contents of srList b4 loop: %s
", srList);
printf("Server List: %s
", svrList);
[Code] ....
Code output:
Contents of srList b4 loop: 1.1.1.1
Server List: 1.1.1.1
result is "1.1.1.1"
Hitting else loop
Contents of srList in else: 2.2.2.2
result is "2.2.2.2"
result is "2.2.2.2"
Contents of srList b4 loop: 1.1.1.1
Server List: 1.1.1.1
result is "1.1.1.1"
Hitting else loop
Contents of srList in else: (null)
View 14 Replies
View Related
May 25, 2014
I have to prepare a project. But I don t know how can I do this. Program will find a matrix P for a square matrix A such that P^-1 A P ....
View 15 Replies
View Related
Oct 4, 2014
3 3
2 1 3 2 3
1 0 2
1 2 6
First line shows row and column number
First index of the second line tells the number of non-zero entries of the first row and second index tell the column number where the non zero entry is placed
for 1st row of matrix:
non-zero entries=2
column number=1
non-zero entry=3
column number=2
non-zero entry=2
covert this in the given form
0 3 3
2 0 0
0 0 6
View 1 Replies
View Related
Feb 20, 2014
I am trying to iterate through a file path to extract the file name. since the . separating the name from the extension is a unique character, i thought i would reverse iterate from the . to the first separating directories. however, when trying to reference the memory location of the position of the . in the string, i am getting an i-value error:
for (std::string::reverse_iterator iter = &(songPath.find('.')); iter != songPath.rend(); ++iter) {
if (*iter == '')
break;
else
songName.push_back(*iter);
}
View 3 Replies
View Related
Jan 17, 2014
I was having problems changing the value of my head node I passed it as an argument as head which would be the address. The parameter was defined as struct node *head. like this
bool deleteNode(struct node *head, struct node *delptr)
I tried manipultaing pointer values to change head node value but it did not work. I saw some code online which used pointer to pointers(in code below) to change head node value it worked I dont fully understand why. Would like better understanding of why.
Would also like to know why the argument call needed &head instead of just head.
remove = deleteNode(&head,found); opposed to remove = deleteNode(head,found);
#include "stdafx.h"
#include<iostream>
struct node{
[Code].....
View 1 Replies
View Related
Aug 19, 2014
I am attempting to implement function pointers and I am having a bit of a problem.
See the code example below; what I want to be able to do is call a function pointer from another pointer.
I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.
class MainObject;
class SecondaryObject;
class SecondaryObject {
public:
[Code]....
View 10 Replies
View Related