LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2011, 03:08 PM   #1
parekhharsh_j
LQ Newbie
 
Registered: Jun 2011
Posts: 20

Rep: Reputation: Disabled
How to do this in C++?


In MATLAB or FORTRAN we do as
Code:
A=[
1 1 1 
1 1 1]
A(1,:)+A(2,:) = [2 2 2]
While in C++ I have to use a loop,something like
Code:
for (i=1;i<4;i++)
B(i)=A(1,i)+A(2,i)
Now, is it possible in C++ without using loop?
Somethink like shorthand operator which I am not aware of
 
Old 10-06-2011, 03:19 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Your snippet has an implicit loop. C/C++ (and most other languages) simply require you to make an *explicit* loop
 
Old 10-06-2011, 03:36 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
You might need to learn a little more C or C++ before you try to do harder thing in C++

Quote:
Originally Posted by parekhharsh_j View Post
in C++ I have to use a loop,something like
Code:
for (i=1;i<4;i++)
B(i)=A(1,i)+A(2,i)
No, in C++ you have to use a loop something like:
Code:
for (i=0;i<3;i++)
B[i]=A[0][i]+A[1][i];
Array indexes start with 0, rather than 1.
Array indexes use [] rather than ().
Multi dimensional arrays use [][] rather than (,).

Quote:
Now, is it possible in C++ without using loop?
Somethink like shorthand operator which I am not aware of
In C++, it is possible to define a class to use instead of array to make a similar shorthand work without needing the loop in each place you use the class. Instead the loop would be buried inside a method of that class.

There would be a lot of effort to set up such a class. To set it up really well would take even more effort and advanced knowledge of C++.

If you want to then use it to avoid the explicit loop in just a few dozen places in your main algorithm, it probably wouldn't be worth the trouble. But if your main algorithm is large and complicated enough, letting that algorithm focus on its core purpose without a lot of distracting inner loops might be worth the trouble.

I'm sure someone has already written a C++ class for people who want to work with vectors while thinking in Fortran but coding in C++. I've seen that for C, but in C you can't do nearly as good a job at hiding the details from the top level code. I don't know what you might search for to find that in C++.

In C++, it is pretty easy (for an experienced programmer) to make a kind of vector in which indexes start at 1 instead of 0. It is pretty easy to make a kind of vector in which (n) is used instead of [n] for selecting an element. It is pretty easy to make a kind of array in which (i,j) is used instead of [i][j]. In such an array, it is pretty easy to make a special symbol meaning ALL such that (i,ALL) or (ALL,i) could select a vector instead of an element. But you are limited in the character set of that symbol. It couldn't be : as I believe it is in Matlab. For your main question, it is pretty easy to define + for such vectors such that having declared vector B and array A, you could code:
B = A(1,ALL) + A(2,ALL);
It would be more in the style of C++ if the syntax for selecting a vector from an array were just A(1) rather than A(1,ALL). That would be a bit easier to define and to use. But that doesn't give a good alternative for A(ALL,1). You can do an extraordinary amount in a C++ class definition to make objects behave exactly the way you want.

I understand that : isn't just for ALL, it can also be used to concisely specify a sub range. An array class in C++ could easily support sub ranges with a fairly concise syntax, but not as concise as : in Matlab.

Last edited by johnsfine; 10-06-2011 at 04:03 PM.
 
2 members found this post helpful.
Old 10-06-2011, 04:10 PM   #4
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by parekhharsh_j View Post
Now, is it possible in C++ without using loop?
Code:
#include <vector>
#include <algorithm>
#include <iostream>

int add(int i, int j){
	return i + j;
};

void print(int i ){
	std::cout << i << std::endl;
}

int main(int argc, char** argv){
	std::vector<int> a(6), b(6);
	std::fill(a.begin(), a.end(), 1);
	std::fill(b.begin(), b.end(), 2);
	std::transform(a.begin(), a.end(), b.begin(), a.begin(), add);
	std::for_each(a.begin(), a.end(), print);
	return 0;
}
Code:
#include <vector>
#include <algorithm>
#include <iostream>

int add(int i, int j){
	return i + j;
};

void print(int i ){
	std::cout << i << std::endl;
}

int main(int argc, char** argv){
	const int size = 6;
	int a[size], b[size];
	std::fill(a, a + size, 1);
	std::fill(b, b + size, 2);
	std::transform(a, a + size, b, a, add);
	std::for_each(a, a + size, print);
	return 0;
}
 
1 members found this post helpful.
Old 10-06-2011, 04:51 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by parekhharsh_j View Post
In MATLAB or FORTRAN we do as
Code:
A=[
1 1 1 
1 1 1]
A(1,:)+A(2,:) = [2 2 2]
While in C++ I have to use a loop,something like
Code:
for (i=1;i<4;i++)
B(i)=A(1,i)+A(2,i)
Now, is it possible in C++ without using loop?
Somethink like shorthand operator which I am not aware of
Start from http://en.wikipedia.org/wiki/List_of...ical_libraries and from there have a close look at http://en.wikipedia.org/wiki/Armadillo_(C++_library) , and there you have other libraries of similar functionality listed.
 
1 members found this post helpful.
Old 10-07-2011, 07:34 AM   #6
parekhharsh_j
LQ Newbie
 
Registered: Jun 2011
Posts: 20

Original Poster
Rep: Reputation: Disabled
Smile

Thanks Sergei Steshenko....You solved my problem
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration