I have a question about 2 dimensional array in GoLang. I do not know why the sort method I use does not work, but It works well in java, C, and C++, but I am not sure what is wrong when I applied the same sort method in Golang, sometimes it give me the right result, sometimes, it does not sort at all. Please help. Thank you so much in advance.
package main
import (
"fmt"
)
var employee int = 0
var day int = 1
func main() {
list := [][] int {{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}}
var result [8][2] int
for i := 0; i < len(result); i++ {
var total int = 0
for j := 0; j < len(list[i]); j++ {
total += list[i][j]
}
result[i][employee] = i
result[i][day] = total
}
sort(result)
fmt.Println("The decreasing order is:")
for i := 0; i < len(result); i++ {
fmt.Printf("Employee %v's total dayoff is %v\n", result[i][employee], result[i][day])
}
}
func sort(list[8][2] int) {
for i := 0; i < len(list); i++ {
var max_day int = list[i][day]
var max_employee int = list[i][employee]
var max_index int = i
for j := i + 1; j < len(list); j++ {
if list[j][day] > max_day {
max_day = list[j][day]
max_employee = list[j][employee]
max_index = j
}
}
if max_index != i {
list[max_index][employee] = list[i][employee]
list[max_index][day] = list[i][day]
list[i][employee] = max_employee
list[i][day] = max_day
}
}
}
You're modifying a copy of list in your sort() function, change its prototype to:
func sort(list *[8][2]int) {
and call it:
sort(&result)
I have a problem regarding index out of bound error in arrays.
I want to create a method that takes month and day as parameter and returns day of the year.If any of the parameters are incorrect, the method should return 0.
for example if the method receives 2 and 3, meaning the third of February it must return 34. If it receives 12 and 31, it must return 365.
but I am getting this problem of index out of bound and can't solv it any tips.
this is my code.
public class CalendarMethods {
public static int dayInYear(int month, int day){
int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int yearCalculation = 0;
int monthCalculation = 0;
for (int j = 0; j < month-1; j++) {
monthCalculation = monthCalculation + daysInMonth[j];
}
yearCalculation = monthCalculation + day;
if (month <= 0 ) {
yearCalculation = 0;
}
if (month >= 13){
yearCalculation = 0;
}
return yearCalculation;
}
}
You should do the boundary checks on month before the loop. A fix for that would be:
public class CalendarMethods {
public static int dayInYear(int month, int day){
int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int yearCalculation = 0;
int monthCalculation = 0;
if (month > 0 && month < 13) {
if (day > 0 && day <= daysInMonth[month - 1]) { // extra: check if entered day makes sense
for (int j = 0; j < month-1; j++) {
monthCalculation = monthCalculation + daysInMonth[j];
}
yearCalculation = monthCalculation + day;
}
}
return yearCalculation;
}
}
For functions, always do sanity checks first to ensure the inputs you are getting align with your expections, in this this case we expect months to be values between 1 and 12 so same implementation as you had but check inputs before processing the rest of the code. This saves you from exceptions from unexpected/bad inputs.
public static int dayInYear(int month, int day){
if (month <= 0||month >= 13 ) {
return 0;
}
int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int yearCalculation = 0;
int monthCalculation = 0;
for (int j = 0; j < month-1; j++) {
monthCalculation = monthCalculation + daysInMonth[j];
}
yearCalculation = monthCalculation + day;
return yearCalculation;
}
I'm stuck with this method which should "find the largest integer in an array passed as a parameter and also output the count of the maximum value", not sure what I'm doing wrong.
Thanks
public class q3 {
public static void main(String[] args) {
int[] arraY = {20, 20, 10, 19, 1, 4, 25, 19, 1, 11, 15, 11, 29, 27, 7, 22, 14, 26, 2, 6, 18};
findMax(arraY);
}
public static void findMax(int[] arraY) {
int max_value = arraY[0];
int countMax = 0;
for (int i = 1; i < arraY.length; i++) {
if (arraY[i] > max_value) {
max_value = arraY[i];
}
for (i = 0; i < arraY.length; i++) {
if (arraY[i] == max_value) {
//System.out.println(array[i]);
//System.out.println(max_value);
countMax++;
}
}
}
System.out.println("The maximum value is: " + max_value);
System.out.println("The count is: " + countMax);
}
}
edit: was an issue with scope, have rearranged the formatting and it now returns the correct result
code updated below
public class q3 {
public static void main(String[] args) {
int[] arraY = {20, 20, 10, 19, 1, 4, 25, 19, 1, 11, 15, 11, 29, 27, 7, 22, 14, 26, 2, 6, 18};
//int[] arraY = {30, 9, 20, 9};
findMax(arraY);
}
public static void findMax(int[] arraY) {
int max_value = arraY[0];
int countMax = 0;
for (int i = 1; i < arraY.length; i++) {
//System.out.println(arraY[i]);
if (arraY[i] > max_value) {
max_value = arraY[i];
}
}
for (int i = 0; i < arraY.length; i++) {
if (arraY[i] == max_value) {
//System.out.println(arraY[i]);
//System.out.println(max_value);
countMax++;
}
}
System.out.println("The maximum value is: " + max_value);
System.out.println("The count is: " + countMax);
}
}
I wrote a code to read 20 different values from 20 different addresses in C. My problem is that I only know how to do it in switch/case statement. Is there a way to simplify this code? Writing 20+ cases will make it very long.
float32 Voltage_Selection[20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
bool Stat[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 ,20 }; //20 status in an array
static uint8_t read = No_read;
uint16_t = answer;
void V_Read(void)
{
typedef enum
{
No_read = 0, read_1, read_2, read_3, read_4, read_5, read_6, read_7, read_8, read_9, read_10, read_11, read_12, read_13, read_14, read_15, read_16, read_17, read_18, read_19, read_20;
} read;
switch (read)
{
case No_read;
Read_function(NULL, Address_0)
break;
case read 1;
answer = Read_function(&Voltage_Selection, Address_1)
if(answer)
stat[1] = false;
else
stat[1] = true;
break;
case read 2;
answer = Read_function(&Voltage_Selection, Address_2)
if(answer)
stat[2] = false;
else
stat[2] = true;
break;
case read 3;
answer = Read_function(&Voltage_Selection, Address_3)
if(answer)
stat[3] = false;
else
stat[3] = true;
break;
//*** all the way till case 20 ***//
case read 20;
answer = Read_function(&Voltage_Selection, Address_20)
if(answer)
stat[20] = false;
else
stat[20] = true;
break;
default;
break;
if(read >= read_20)
{
read = read_1;
}
}
}
}
Below are two 2D objects, Array and Vector. As you can see the information described in both is identically. My game works flawlessly using the Array object, but with Vector is throws the following error:
[Fault] exception, information=RangeError: Error #1125: The index 10 is out of range 10.
var _platformMap:Array = [
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[00, 00, 10, 10, 10, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 00, 00, 00, 10],
[10, 10, 10, 00, 00, 10, 10, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[10, 10, 00, 00, 00, 00, 00, 10, 10, 10],
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
[00, 00, 00, 00, 00, 00, 00, 00, 00, 00]
];
var _platformMap:Vector.<Vector.<int>> = Vector.<Vector.<int>>(
[
Vector.<int>([10,10,10,10,10,10,10,10,10,10]),
Vector.<int>([00,00,10,10,10,10,10,10,10,10]),
Vector.<int>([10,10,10,10,01,01,01,10,10,10]),
Vector.<int>([10,10,10,00,10,10,10,10,01,10]),
Vector.<int>([10,10,10,00,10,10,01,01,01,00]),
Vector.<int>([10,10,10,10,01,01,01,01,01,10]),
Vector.<int>([00,00,00,00,00,10,10,10,10,10]),
Vector.<int>([00,00,00,00,00,00,00,00,00,00])
]
);
I read about that Vector objects have runtime range checking (or fixed-length checking) besides Arrays. Could this be the problem?
public class TileCollisionController
{
private var _softPlatformOpen:Boolean = true;
private var _elevatorOpen:Boolean = true;
public function TileCollisionController()
{}
public function platformCollision(gameObject:TileModel, platformMap:Vector.<Vector.<int>>, maxTileSize:uint, platform:uint):void
{
var overlapX:Number;
var overlapY:Number;
//check top-left corner
if (platformMap[gameObject.top][gameObject.left] == platform)
{
overlapX = gameObject.xPos % maxTileSize;
overlapY = gameObject.yPos % maxTileSize;
if (overlapY >= overlapX)
{
if (gameObject.vy < 0 && platformMap[gameObject.bottom][gameObject.left] != platform)
{
//Collision on top side of the object
gameObject.setY = gameObject.mapRow * maxTileSize;
gameObject.vy = 0;
}
}
else
{
//Collision on left side of the object
gameObject.setX = gameObject.mapColumn * maxTileSize;
gameObject.vx = 0;
}
}
//check top-right corner
if (platformMap[gameObject.top][gameObject.right] == platform)
{
overlapX = maxTileSize - ((gameObject.xPos + gameObject.width) % maxTileSize);
overlapY = gameObject.yPos % maxTileSize;
if (overlapY >= overlapX)
{
if (gameObject.vy < 0 && platformMap[gameObject.bottom][gameObject.right] != platform)
{
gameObject.setY = (gameObject.mapRow * maxTileSize);
gameObject.vy = 0;
}
}
else
{
//Collision on right
gameObject.setX = (gameObject.mapColumn * maxTileSize) + ((maxTileSize - gameObject.width) - 1);
gameObject.vx = 0;
}
}
//check bottom-left corner
if (platformMap[gameObject.bottom][gameObject.left] == platform)
{
overlapX = gameObject.xPos % maxTileSize;
overlapY = maxTileSize - ((gameObject.yPos + gameObject.height) % maxTileSize);
if (overlapY >= overlapX)
{
if (gameObject.vy > 0 && platformMap[gameObject.top][gameObject.left] != platform)
{
//trace("Collision on bottom");
//Collision on bottom
gameObject.setY = (gameObject.mapRow * maxTileSize) + (maxTileSize - gameObject.height);
gameObject.vy = 0;
gameObject.jumping = false;
}
}
else
{
//trace("Collision on bottom left");
//Collision on left
gameObject.setX = gameObject.mapColumn * maxTileSize;
gameObject.vx = 0;
}
}
//check bottom-right corner
if (platformMap[gameObject.bottom][gameObject.right] == platform)
{
overlapX = maxTileSize - ((gameObject.xPos + gameObject.width) % maxTileSize);
overlapY = maxTileSize - ((gameObject.yPos + gameObject.height) % maxTileSize);
if (overlapY >= overlapX)
{
if (gameObject.vy > 0 && platformMap[gameObject.top][gameObject.right] != platform)
{
//trace("Collision on bottom right");
//Collision on bottom
gameObject.setY = (gameObject.mapRow * maxTileSize) + (maxTileSize - gameObject.height);
gameObject.vy = 0;
gameObject.jumping = false;
}
}
else
{
//trace("Collision on right");
//Collision on right
gameObject.setX = (gameObject.mapColumn * maxTileSize) + ((maxTileSize - gameObject.width) - 1);
gameObject.vx = 0;
}
}
}
}
}
See example
public function platformCollision(gameObject:TileModel, platformMap:Vector.<Vector.<int>>, maxTileSize:uint, platform:uint):void
{
var overlapX:Number;
var overlapY:Number;
if(gameObject.bottom < platformMap.length && gameObject.right < platformMap[0].length)
{
//check top-left corner
//...
Example after updates
There is nothing wrong with the code you posted, but neither of those have an object at index 10? Maybe you should be looking at index 9 as it starts at 0?
Could you show how you are accessing the array/vector? I think that is where the error is coming from.
Try this code
var platformMap:Vector.<Vector.<int>> = new Vector.<Vector.<int>>(2);(
[
Vector.<int>([10,10,10,10,10,10,10,10,10,10]),
Vector.<int>([00,00,10,10,10,10,10,10,10,10]),
Vector.<int>([10,10,10,10,01,01,01,10,10,10]),
Vector.<int>([10,10,10,00,10,10,10,10,01,10]),
Vector.<int>([10,10,10,00,10,10,01,01,01,00]),
Vector.<int>([10,10,10,10,01,01,01,01,01,10]),
Vector.<int>([00,00,00,00,00,10,10,10,10,10]),
Vector.<int>([00,00,00,00,00,00,00,00,00,00])
]);
the vectors length is 10, so you can access from 0 to 9.
the vectors contain vectors, what also have a len.
try
trace("maxnumallowed", platformMap.length-1)
and note, that you can only access platformMap[n], if n is between 0 and platformMap.length-1