r/programminghomework • u/Katanawolf • Aug 04 '17
[Ruby]My program can identify 2 pixels within a multidimensional array instead of 3
This is to help me use and manipulate multidimensional arrays
I am given a sample array
sample = [
[[65, 67, 23], [234, 176, 0], [143, 0, 0]],
[[255, 30, 51], [156, 41, 38], [3, 243, 176]],
[[255, 255, 255], [0, 0, 0], [133, 28, 13]],
[[26, 43, 255], [48, 2, 2], [57, 89, 202]]
]The question states that these are RGB values of pixels and I have to count the red pixels
red is defined as an R value that is greater than 100 and G and B values that are both less than the R value/4
I have identified Pixel #4 and #9 as red but Pixel #3 does not return red when it should What could I be doing wrong...
I think my if statements are not written properly but I'm not sure what to change
MY CODE
#pixel counter
#red = [255,0, 0]
#green = [0, 255, 0]
#blue = [0, 0, 255]
#black = [0, 0, 0]
#white = [255, 255, 255]
#yellow = [255, 255, 0]
#Define RED
#R value must be greater 100
#G and B values must be less than R/4
sample = [
[[65, 67, 23], [234, 176, 0], [143, 0, 0]],
[[255, 30, 51], [156, 41, 38], [3, 243, 176]],
[[255, 255, 255], [0, 0, 0], [133, 28, 13]],
[[26, 43, 255], [48, 2, 2], [57, 89, 202]]
]
r4 = 0
red = 0
green = 0
blue = 0
redcheck = false
greencheck = false
bluecheck = false
redpixelcount = 0
pixelcount = 1
i = 0
j = 0
k = 0
line = 0
while (i < sample.length)
#look through entire row of sample
#DEBUG TOOL puts "Row: " + i.to_s
while (j < sample[i].length)
#look throiugh index of each row in sample
#DEBUG TOOL puts "Group: " + i.to_s + ","+ j.to_s
puts "PIXEL # " + pixelcount.to_s
while (k < sample[i][j].length)
red = 0
redcheck = false
greencheck = false
bluecheck = false
#Look through each value in each line within a row of sample
if (line == 0)
puts "START"
end
if (line == 0)
puts "Red is " + sample[i][j][k].to_s
red = sample[i][j][k]
r4 = red/4
puts "R4 is " + r4.to_s
end
if (line == 1)
puts "Green is " + sample[i][j][k].to_s
green = sample[i][j][k]
end
if (line == 2)
puts "Blue is " + sample[i][j][k].to_s
blue = sample[i][j][k]
end
#CHECK
if (red >= 100)
puts red.to_s + " IS GREATER THAN " + 100.to_s
if (green <= r4)
puts green.to_s + " IS LESS OR EQUAL TO " + r4.to_s
if (blue <= r4)
puts blue.to_s + " IS LESS OR EQUAL TO " + r4.to_s + " SO..."
puts "THIS IS RED"
redpixelcount += 1
end
end
end
#line 0 = Red line 1 = Green line 2 = Blue
line += 1
#Display value
#puts sample[i][j][k].to_s
k += 1
end
pixelcount += 1
k = 0
j += 1
line = 0
end
j = 0
i += 1
end
#if (r > 100 && )
#end
puts "There are: " + (pixelcount-1).to_s + " pixels"
puts redpixelcount.to_s + " are RED"My Output:
PIXEL # 1
START
Red is 65
R4 is 16
Green is 67
Blue is 23
PIXEL # 2
START
Red is 234
R4 is 58
234 IS GREATER THAN 100
Green is 176
Blue is 0
PIXEL # 3
START
Red is 143
R4 is 35
143 IS GREATER THAN 100
Green is 0
Blue is 0
PIXEL # 4
START
Red is 255
R4 is 63
255 IS GREATER THAN 100
0 IS LESS OR EQUAL TO 63
0 IS LESS OR EQUAL TO 63 SO...
THIS IS RED
Green is 30
Blue is 51
PIXEL # 5
START
Red is 156
R4 is 39
156 IS GREATER THAN 100
30 IS LESS OR EQUAL TO 39
Green is 41
Blue is 38
PIXEL # 6
START
Red is 3
R4 is 0
Green is 243
Blue is 176
PIXEL # 7
START
Red is 255
R4 is 63
255 IS GREATER THAN 100
Green is 255
Blue is 255
PIXEL # 8
START
Red is 0
R4 is 0
Green is 0
Blue is 0
PIXEL # 9
START
Red is 133
R4 is 33
133 IS GREATER THAN 100
0 IS LESS OR EQUAL TO 33
0 IS LESS OR EQUAL TO 33 SO...
THIS IS RED
Green is 28
Blue is 13
PIXEL # 10
START
Red is 26
R4 is 6
Green is 43
Blue is 255
PIXEL # 11
START
Red is 48
R4 is 12
Green is 2
Blue is 2
PIXEL # 12
START
Red is 57
R4 is 14
Green is 89
Blue is 202
There are: 12 pixels
2 are RED
2
Upvotes
View all comments
Show parent comment
1
u/Katanawolf Aug 09 '17 edited Aug 09 '17
Yes sir when I added the
for all the corresponding colors and everything ran perfectly. Looking back it seems like a very simple thing I could have changed..Thanks again It's a huge relief I was stressing out about it. Now I checked out the 5 lines of code you wrote and I had to read what flatten does and still need to understand the flatten select portion. But I get the for loop that you used. It's beginning to make me understand how much planning before hand needs to go into writing code and the importance of really digging into the documentation of ruby to make my code better.
I'm not in a class or anything I'm reading Oreilly's "Computer Science Programming Basics in Ruby" and had trouble on one of there questions. In addition I visit tutorialspoint and read the ruby tutorials they have there...but still a long way to go