Error when compiling, error in array - c++

float premios[20]={500.00, 700.00, 800.00, 900.00, 1200.00, 1500.00, 1800.00, 2000.00, 2100.00, 2300.00, 2800.00, 3000.00, 3200.00, 3500.00, 4000.00, 10,000.00, 100,000.00, 200,000.00, 500,000.00, 1,000,000.00 };
Look at the code, when i try to compile it, it gives me the error "[Error] too many initializers for 'float [20]' ", it has exactly 20 values, tried to correct it by setting it to 21 values but it didnt work. Then i set the array to an empty array and it worked, can anybody explain me why did it happen?

The "," between each value count as a value. so I think 1,000,000.00 for example count as 3 values. eg. [1, 0, 0]
I believe you were trying to do 1000000.00 instead of 1,000,000.00

Your initializer contains 26 elements.
Adds: Using > float premios[] = ... does not mean it's an empty array -- it means the number of elements in the array is deduced from the initializer, so it will turn out a float[26].

Related

Addition Operation from ints in a String

I set up a string filled solely with numbers and using a for loop iterated through it in order to add them together mathematically (Wanted to see if the language would allow this), as a result I got some weird Numbers as the result. Can someone explain why this is happening?
int main()
{
std::string word = "2355412";
for (int i = 0; i<word.size(); i++){
int sum = word[i]+word[i+1];
std::cout << sum << std::endl;
}
return 0;
}
The code when run results in:
101
104
106
105
101
99
50
Due to the way I wrote my code I also believe that it should have resulted in an out of bounds error due word[i+1] on the final value resulting in the calling of a value that does not exist. Can someone explain why it did not throw an error?
The value you get is not what you expect because it is the sum of the ascii code corresponding to the characters you are summing, it's not converted into their value by default.
Also, as mentioned by other, string::operator[] doesn't check if you are trying to reach an out of bound value. In this case, you read 0 because you reached the string termination character \0 which happen to be 0.
it should have resulted in an out of bounds error
string::operator[] doesn't check bounds, it assumes you have. If you call it with an out of bounds index, the entire behaviour of your program is undefined, i.e. anything can happen.
It sounds like you want string::at, which does check bounds, and will throw std::out_of_range

SAS Array subscript out of range

I am getting the Array subscript out of range error:
ERROR: Array subscript out of range at line 408 column 169.
SYM_ROOT=FSV DATE=. TIME_M=. BID=. BIDSIZ=. ASK=. ASKSIZ=. EXN=.
FIRST.SYM_ROOT=1 LAST.SYM_ROOT=1 FIRST.DATE=1 LAST.DATE=1 FIRST.TIME_M=1
LAST.TIME_M=1 nexb1=. nexb2=. nexb3=. nexb4=. nexb5=. nexb6=. nexb7=. nexb8=.
nexb9=. nexb10=. nexb11=. nexb12=. nexb13=. nexb14=. nexb15=. nexb16=. nexb17=.
nexo1=. nexo2=. nexo3=. nexo4=. nexo5=. nexo6=. nexo7=. nexo8=. nexo9=. nexo10=.
nexo11=. nexo12=. nexo13=. nexo14=. nexo15=. nexo16=. nexo17=. sexb1=. sexb2=.
sexb3=. sexb4=. sexb5=. sexb6=. sexb7=. sexb8=. sexb9=. sexb10=. sexb11=.
sexb12=. sexb13=. sexb14=. sexb15=. sexb16=. sexb17=. sexo1=. sexo2=. sexo3=.
sexo4=. sexo5=. sexo6=. sexo7=. sexo8=. sexo9=. sexo10=. sexo11=. sexo12=.
sexo13=. sexo14=. sexo15=. sexo16=. sexo17=. _I_=. i=18 BB=. BO=. MIDPRICE=.
BBSize=. BOSize=. NUMEX=. _ERROR_=1 _N_=6417740
However, I am not sure what happened, because the code has previously worked on a different dataset.
The only thing that I can think of is that, because the dataset I am having problem with is the subset of the original one (which worked), it might not have the complete range of exn (I am using a variable named exn as the index of the array).
I defined the array as:
array nexb nexb:; array nexo nexo:; array sexb sexb:; array sexo sexo:;
The variable I am talking about is called exn, and it is used to reference the array:
nexb(exn)=bid;nexo(exn)=ofr;sexb(exn)=bidsiz;sexo(exn)=ofrsiz;
The arrays are initialized in the following way:
do i=1 to 17;
nexb(i)=.; nexo(i)=.; sexb(i)=.; sexo(i)=.;
end;
Originally exn spans from 1 to 17. Now I think some of the numbers in between might be missing in the dataset. But why is that a problem? They are initialized anyways.
You cannot use a missing value as the index into an array. Your log shows that EXN is missing.

error : Vector subscript out of range error

I have this code in c++ and I used vectors but I got this error:
error: Vector subscript out of range error.
Can some help me in this issue.
int const TN = 4;
vector <uint32_t> totalBytesReceived(TN);
void ReceivePacket(string context, Ptr <const Packet> p)
{
totalBytesReceived[context.at(10)] += p->GetSize();
}
void CalculateThroughput()
{
double mbs[TN];
for (int f = 0; f<TN; f++)
{
// mbs = ((totalBytesReceived*8.0)/100000);
mbs[f] = ((totalBytesReceived[f] * 8.0) / 100000);
//totalBytesReceived =0;
rdTrace << Simulator::Now().GetSeconds() << "\t" << mbs[f] << "\n";
Simulator::Schedule(Seconds(0.1), &CalculateThroughput);
}
}
It seems like
totalBytesReceived[context.at(10)] += p->GetSize();
throws the exception because the char at position 10 of context is out of range. Since you use it to index the vector, it has to be in the range 0 to 3.
Looking at the content of context you posted:
"/NodeList/" 1 "/DeviceList/*/$ns3::WifiNetDevice/Mac/MacRx"
^ ^ ^
0 10 12
If you want to extract the 1 and use it as an index, you need to use:
char c = context.at(12); // Extract the char.
int index = c - '0'; // Convert the character '1' to the integer 1.
This is because of the ASCII standard which determines how characters are stored as numbers.
Probably the real issue is that you get the character '1' and use its ASCII value as index to the vector instead of the intended integer value 1.
This out of bounds access is then undefined behaviour, which in your case leads to an exception.
The following is not the cause, leaving it for reference:
The exception is probably coming from this expression:
context.at(10)
This is the only operation (*) involved that is actually performing bounds checking. The vector operator[] isn't doing that, neither does a C array check it's bounds.
So: Are you sure the string context is never shorter than 11 characters?
(*) Accessing a vector out of bounds is undefined behaviour, and throwing an exception is within the possible outcomes of that. Thanks to Beta Carotin and Benjamin Lindley for that.
This is the real thing:
Also note that a vector isn't resized like map when accessing an out of bounds index using operator[], so unless you can guarantee that the characters in the string are between 0 and 3 inclusive this will be your next issue.
And this means (size_t)0 and (size_t)3, not the characters '0' and '3'.

Array index out of bounds error Pawn

I am using spawn points but when it compiles I'm getting this error:
Array index out of bounds
On this line is the error
for(new i =0 ; i < 5 ;i++) {
SetPlayerPos(playerid, spawnpoints[i][0], spawnpoints[i][1], spawnpoints[i][2]);
}
Hoping somebody knows the solution to the error.
Your array spawnpoints has either less than 5 entries or one of the arrays (spawnpoints[0], spawnpoints[1], spawnpoints[2], spawnpoints[3], spawnpoints[4]) has less than 3 entries. Try debugging your code.
Replace 5 with sizeof(spawnpoints). If you still get the error after this, then your spawnpoints array doesn't contain an x, y and z coordinate (and so is incorrectly structured.)
SetPlayerPos(playerid, Float:x, Float:y, Float:z);
Are spawnpoints defined with Float?
new Float:OldPos[MAX_PLAYERS][3];
Try with this example:
new Float:OldPos[MAX_PLAYERS][3];
GetPlayerPos(i, OldPos[i][0], OldPos[i][1], OldPos[i][2]);

Mathematica - Functions with Lists

I got this error in Mathematica today:
Set::shape: "Lists {0,0,0,0,0,0,0,0,0,0} and {0,0,0,0,0,0,0,0,0,0,{1}} are not the same shape" >>
And after 3 of those :
General::stop : Further output of Set::shape will be suppressed during this calculation. >>
I am confused as to why I cannot append a "1" to my list of zeros. Is this because I cannot edit the list that is passed into the function? If so, how could I edit that list and somehow return or print it?
Here is my full code:
notFunctioningFunction[list_] := (For[i = 1, i < 10, i++, list = Append[list, {1}]];
Print[list])
list = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
notFunctioningFunction[list]
The reason why I am appending a "{1}" is because in my function, I am solving an equation, and getting the value of the variable which outputs {1}. Here is my code for that :
varName / . Solve[ function1 == function2 ]
Obviously I am a beginner with Mathematica so please be patient :)
Thanks,
Bucco
Append needs to take one list and one element. Like so:
Append[{1,2,3,4},5]
If you have two lists, you can use Join. Like so:
Join[{1,2,3,4},{5}]
Both of these will yield the same result: {1,2,3,4,5}.
Dear Mathematica beginner.
First, when you use something like
{a,b} = {c,d,e};
in Mathematica, between two lists, the program has a difficulty because this is a construct used to assign values to variables, and it requires (among other things) the two lists to be equal.
If what you want is just to add a "1" to an existing and named list, one at a time, the best construct is:
AppendTo[list, 1];
(this construct will modify the variable 'list')
or
list = Join[list, {1}];
Second: about the error messages, they are printed 3 times by default in an evaluation, then muted so that a long list of identical error messages does not clutter your display.
Third, if what you need is adding 10 1s to a list, there is no need to construct that in a loop. You can do that in one pass:
list = Join[list, Table[1, {10}]]
or, more cryptic for beginners
list = Join[list, Array[1&, 10]]