same address is allocated to different nodes - memory

I have to create an array of pointers for rows and columns initially all pointing to null.
The address of the newly created row/column headers are to be inserted into the arrays.
In the result I see that the addresses are repeated in the row and column arrays.
Problem: the addresses are repeated in the row and column arrays.
I have to mention that I did not use delete (for deallocating the memory yet, because i am confused where to include it whether inside the function or outside).
I also see that no rowheader is created for the token 2x3y3.
First I created an array of pointers
//allocating row and column pointers, m is number of rows and n is number of columns
- node **rArr = new node*[m+1];
- node **cArr = new node*[n+1];
void create_array_with_nullp(){
for(int i=0; i<=m;i++){
rArr[i]=NULL;
std::cout<<"row array contents"<<rArr[i]<<'\n';
}
for(int i=0; i<=n;i++){
cArr[i]=NULL;
std::cout<<"col array contents"<<cArr[i]<<'\n';
}
}
Then I am creating rowheaders or colheaders for the tokens if not already created through the following function:
void create_n_link_new_node(int a, int b){
if(a >m || b>n || a<0 || b<0){
return;
}
node * colptr = cArr[b];
node * rowptr = rArr[a];
if (rowptr==NULL){
node * new_rowheader = new node;
new_rowheader->coefficient = NULL;
new_rowheader->row = a;
new_rowheader->column = -1;
new_rowheader->rowLink = new_rowheader;
new_rowheader->colLink = new_rowheader;
rArr[a] = new_rowheader;
std::cout<<"new row header created"<<'\n';
std::cout<< "coefficient = "<<new_rowheader->coefficient<<'\n';
std::cout<< "row = "<<new_rowheader->row<<'\n';
std::cout<< "column = "<<new_rowheader->column<<'\n';
std::cout<< "rowLink = "<<new_rowheader->rowLink<<'\n';
std::cout<< "colLink = "<<new_rowheader->colLink<<'\n';
}
if(colptr == NULL){
node * new_colheader = new node;
new_colheader->coefficient = NULL;
new_colheader->row = -1;
new_colheader->column = b;
new_colheader->rowLink = new_colheader;
new_colheader->colLink = new_colheader;
cArr[b] = new_colheader;
std::cout<<"new column header created"<<'\n';
std::cout<< "coefficient = "<<new_colheader->coefficient<<'\n';
std::cout<< "row = "<<new_colheader->row<<'\n';
std::cout<< "column = "<<new_colheader->column<<'\n';
std::cout<< "rowLink = "<<new_colheader->rowLink<<'\n';
std::cout<< "colLink = "<<new_colheader->colLink<<'\n';
}
}
The Result is:
THE RESULT:
token==5x4y2
coefficient=5
row= 4
col = 2
new row header created
coefficient = 0
row = 4
column = -1
rowLink = 0x100103c10
colLink = 0x100103c10
new column header created
coefficient = 0
row = -1
column = 2
rowLink = 0x100103c30
colLink = 0x100103c30
token==8x4y
coefficient=8
row= 4
col = 1
new column header created
coefficient = 0
row = -1
column = 1
rowLink = 0x100103c50
colLink = 0x100103c50
token==2x3y3
coefficient=2
row= 3
col = 3
new column header created
coefficient = 0
row = -1
column = 3
rowLink = 0x100103c70
colLink = 0x100103c70
token==4xy2
coefficient=4
row= 1
col = 2
new row header created
coefficient = 0
row = 1
column = -1
rowLink = 0x100103c90
colLink = 0x100103c90
token==y3
coefficient=-1
row= 0
col = 3
new row header created
coefficient = 0
row = 0
column = -1
rowLink = 0x100103cb0
colLink = 0x100103cb0
token==5y
coefficient=5
row= 0
col = 1
token==5
coefficient=5
row= 0
col = 0
new column header created
coefficient = 0
row = -1
column = 0
rowLink = 0x100103cd0
colLink = 0x100103cd0
why no rowheader is created for token=2x3y3 ?
Finally the addresses stored in Array of pointers are:
#Array of row pointers#
- row 0 = 0x100103cb0
- row 1 = 0x100103c90
- row 2 = 0x100103cd0
- row 3 = 0x100103c50
- row 4 = 0x100103c30
#Array of column pointers#
- column 0 = 0x100103cd0
- column 1 = 0x100103c50
- column 2 = 0x100103c30
- column 3 = 0x100103c70
* row 2 is having the same address of column 0,
* row 3 is having the same address of column 1,
* row 4 is having the same address of column 2

It's really hard to figure out what you're trying to do here. Couple of things that stick out:
Remember that for an array of length m, you can only index from 0 to m-1 (not m). Your loop in create_array_with_nullp is going past the end of each of the arrays, corrupting memory.
node **rArr = new node*[m];
//...
for(int i=0; i<=m;i++){ // ouch! should only go to i**<**m
In create_n_link_new_node, did you mean to set the column entry like so:
cArr[b] = new_colheader->rowLink;
or did you actually mean:
cArr[b] = new_colheader;
Are you intending to have each of colheader and rowheader cross-referenced? If so, you would need:
new_colheader->rowLink = new_rowheader;
new_rowheader->colLink = new_colheader;
at the end of create_n_link_new_node.
Again, not really sure what is trying to be accomplished but those things seem suspect at least.

Related

Get all repeated itens from a table in Lua

I need a little help here
Let's suppose I have a table with numbers.
tbl = {'item1' = 6, 'item2' = 1, 'item3' = 6, 'item4' = 3, 'item5' = 2, 'item5' = 3}
I wanna put all repeated numbers in the same table (with key and value) like this:
repeated = {'item1' = 6, 'item3' = 6, 'item4' = 3, 'item5' = 3}
and creat a new one with the "not repeated" numbers:
notrepeated = {'item2' = 1, 'item5' = 2}
Can someone help? Thank you so much.
-- Count the items for each number
local itemsByNum = {}
for item, num in pairs(tbl) do
itemsByNum[num] = (itemsByNum[num] or 0) + 1
end
-- Now move objects to the respective tables
local rep, noRep = {}, {} -- can't use "repeat" as that's a Lua keyword
for item, num in pairs(tbl) do
if itemsByNum[num] > 1 then -- repeated at least once
rep[item] = num
else -- unique number
norep[item] = num
end
end

add value afterwards to key in table lua

Does someone know how to add an value to an key which already has an value ?
for example:
x = {}
x[1] = {string = "hallo"}
x[1] = {number = 10}
print(x[1].string) --nil
print(x[1].number) --10
It should be possible to print both things out. The same way how it is here possible:
x[1] = { string = "hallo" ; number = 10}
I just need to add some informations afterwards to the table and especially to the same key.
Thanks!
x = {} -- create an empty table
x[1] = {string = "hallo"} -- assign a table with 1 element to x[1]
x[1] = {number = 10} -- assign another table to x[1]
The second assignment overwrites the first assignment.
x[1]["number"] = 10 or short x[1].number = 10 will add a field number with value 10 to the table x[1]
Notice that your x[1] = { string = "hallo" ; number = 10} is acutally equivalent to
x[1] = {}
x[1]["string"] = "hallo"
x[1]["number"] = 10

Convert string into array and do operations

I have a string like: "1234567334535674326774324423". I need to create a method to do the following:
Make an array consisting of digits in the string like [1, 2, 3, ..., 2, 3]
Sum all the odd positions of the array
Sum all the even positions of the array
Multiply the odd sum by 3
Sum step 4 and step 3.
Get the minimum number to sum to step 5 to get the sum that is a multiple of 5.
I don't know how to solve this with rails. If anyone can help me, I would be glad.
I have this:
barcode_array = #invoice.barcode.each_char.map {|c| c.to_i}
impares = [barcode_array[0]] + (1...barcode_array.size).step(2).collect { |i| barcode_array[i] }
pares = (2...barcode_array.size).step(2).collect { |i| barcode_array[i] }
suma_impares = impares.inject(:+)
mult_impares = suma_impares * 3
suma total = mult_impares + pares
I solved it. Here is the code if anyone needs it:
barcode_array = #invoice.barcode.each_char.map {|c| c.to_i}
impares = [barcode_array[0]] + (1...barcode_array.size).step(2).collect { |i| barcode_array[i] }
pares = (2...barcode_array.size).step(2).collect { |i| barcode_array[i] }
suma_impares = impares.inject(:+).to_i
mult_impares = suma_impares * 3
suma_pares = pares.inject(:+).to_i
suma_total = mult_impares + suma_pares
verificador = 10 - (suma_total - (suma_total / 10).to_i * 10)
#invoice.barcode = #invoice.barcode.to_s + verificador.to_s
I'm not sure what you mean in step 6, but here's how I would tackle 1-5:
s = '1234567334535674326774324423'
a = s.chars.map(&:to_i) # convert to an array of integers
odd_sum = 0
even_sum = 0
# sum up odds and evens
a.each_with_index {|n, i| n.even? ? even_sum += n : odd_sum += n}
total = even_sum + odd_sum * 3

How to convert a binary number into integer in lua

i have a array of 1 and 0,
for example 10110
values = {1,0,1,1,0}
max = 0
for value = 6,1,-1 do
max = max + 2*index*value
end
but how could get the index of the array in order to calculate the max
Try this:
values = {1,0,1,1,0}
max = 0
for index = 1,#values,1 do
max = max + 2^(#values-index)*values[index]
end
print(max)

How can I convert this function into an array function?

So I have this function here that runs through T2:T:
=IF($D$29<$N2,"", AVERAGE(INDIRECT("P"&IF($N2<11, 2,$N2-5)&":P"&$N2+5)))
Column P is a list of numbers starting at row 2. Column N is an index(goes up by 1 each row) which starts at row 2 and ends where P ends + 14, and D29 is just a number. In my current situation P ends at row 11 and N ends at row 25. And I'm trying to change it into an array formula so that when I add new rows it updates automatically. So after changing it I got this:
=ARRAYFORMULA(IF($D$29<$N2:N,"", AVERAGE(INDIRECT("P"&IF($N2:N<11, 2,$N2:N-5)&":P"&$N2:N+5))))
However, it is not functioning properly. It still occupies the same amount of rows, but each row is the same value. The value of the first row originally. How can I fix this problem? Thanks!
The problem here is that ARRAYFORMULA doesn't work with AVERAGE.
But you could always use javascript.
Open up the script editor and paste in this code.
function avg(nums, d) {
var r = [],
i, j, start, end, avg, count;
for(i = 0; i < nums.length; i++) {
if(d <= i) r.push([""]);
else {
if(i < 10) start = 0;
else start = i - 5;
end = i + 4;
avg = 0, count = 0;
for(j = start; j <= end; j++) {
if(nums[j]) {
avg += nums[j][0];
count++;
}
}
r.push([avg / count]);
}
}
return r;
}
Save it, go back to your spreadsheet and put this formula in any cell =avg(P2:P11, D29)

Resources