Initializing entire 2D array with one value

Feb 23, 2016 at 12:32pm
Hello,
I want to initialize 2D array (matrix) with one value. I would like to say that for vector I can easily define as below
double default_credibility[50]={0.5};
but for 2D array if I use "for loop" as below , I will receive error "error: expected unqualified-id before ‘for’"
for ( int i=0; i< 50; i++)
{ for (int j=0; j< 50; j++)
{
double default_credibility[i][j]=0.5;
}
}
Moreover, I would like to say that I want that this value can change during programming and new value can be saved instead of 0.5.
how can I define this matrix.
any help will be appreciate
regards

Feb 23, 2016 at 12:38pm
double default_credibility[i][j]
declares a local array which doesn't make sense. Declare the array before the loops:
1
2
3
4
5
6
7
double default_credibility[50][50];
for ( int i=0; i< 50; i++)
{ for (int j=0; j< 50; j++)
{
double default_credibility[i][j]=0.5;
}
}
Feb 23, 2016 at 1:29pm
thank you for replying but after modifying code as below, still I have received the same error
int i;
int j;
double default_credibility[50][50];
for ( i=0; i< 50; i++)
{ for (j=0; j< 50; j++)
{
default_credibility[i][j]=0.5;}}
I would like to mention I defined this value out of main function after defining #include functions.
regards
Feb 23, 2016 at 1:39pm
thank you for replying but after modifying code as below, still I have received the same error
Would help to tell us what errors you are getting since we can't read minds.

There is nothing wrong with the code you presented, that's not where you are getting the errors from. Please present your full program using code tags - http://www.cplusplus.com/articles/jEywvCM9/

And please provide the a copy/paste of the full error messages.
Feb 23, 2016 at 1:56pm
since I modifying a source code I can not copy all because it is too long but I copy a part of it :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <boost/lambda/bind.hpp>
#include <boost/tuple/tuple.hpp>
#include <ns3/random-variable-stream.h>
#include <set>
#include <map>
//*******************2/12
int i;
int j;
double default_credibility[50][50];
double new_credibility[50][50];
int data_number[50][50];
int total_Interest=1;
int peer_Interest[50][50];
for ( i=0; i< 50; i++)
{ for (j=0; j< 50; j++)
{
 default_credibility[i][j]=0.5;
 new_credibility[i][j]=2;
data_number[i][j]=0;
peer_Interest[i][j]=0;
}
} 
//----------------
namespace ll = boost::lambda;

namespace ns3 {
namespace ndn {

NS_OBJECT_ENSURE_REGISTERED (ForwardingStrategy);

NS_LOG_COMPONENT_DEFINE (ForwardingStrategy::GetLogName ().c_str ());
std::string
ForwardingStrategy::GetLogName ()
{
  return "ndn.fw";
}


the part between star (***) and dash (----) I have added . and after adding I have received below error :

../src/ndnSIM/model/fw/ndn-forwarding-strategy.cc:58:1: error: expected unqualified-id before ‘for’
for ( i=0; i< 50; i++)
^
../src/ndnSIM/model/fw/ndn-forwarding-strategy.cc:58:12: error: ‘i’ does not name a type
for ( i=0; i< 50; i++)
^
../src/ndnSIM/model/fw/ndn-forwarding-strategy.cc:58:19: error: ‘i’ does not name a type
for ( i=0; i< 50; i++)


Feb 23, 2016 at 3:17pm
You need to put the code into a function.
Feb 24, 2016 at 5:18am
yes , thank you . I should define function for initialization.
however I can directly give value to matrix without defining "for loop", but this will be an inefficient way.
regards
Topic archived. No new replies allowed.