Question about sorting 2 dimensional array in Golang - arrays

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)

Related

How can I create a power set at the end of the program?

i need to create a power set of array lo[], array lo[] is {5, 6, 7}; it's for my laboratories, I don't know how to do this in general power set is 5, {5, 6} {5, 7} {5, 6, 7}, all combinations and another important thing, power of array. the number of power set combinations is the same as the power. Power of array is 2^(elements of array); 2^3 = 8
should be 8 combination.
output:
!((C/A) U (A/B)): {5, 6, 7}
power set: {5}, {5, 6}, {5, 6, 7}, {6, 7}.....
#include <stdio.h>
#include <math.h>
int main() {
int a[10] = { 1,1,1,1,1,1,1,0,0,0 };
int b[10] = { 0,0,0,0,1,1,1,1,1,1 };
int c[10] = { 1,1,1,0,0,0,0,1,1,1 };
int c1[10];
int c2[10];
int c3[10];
int c4[10];
int temp[10], temp1[10], a2[10] = { 1, 2, 3, 4, 5 };
int k = 0;
for (int i = 0; i < 10; i++) {
c1[i] = c[i];
if (c[i] == a[i]) {
c1[i] = 0;
}
}
for (int i = 0; i < 10; i++) {
c2[i] = a[i];
if (a[i] == b[i]) {
c2[i] = 0;
}
}
for (int i = 0; i < 10; i++) {
if (c1[i] == 1 || c2[i] == 1) {
c3[i] = 1;
}
}
for (int i = 0; i < 10; i++) {
if (c3[i] == 0) {
c4[i] = 1;
}
else
c4[i] = 0;
} //// the main part of power set
int lo[3] = {0, 0, 0};
printf("!((C/A) U (A/B)): ");
for (int i = 0; i < 10; i++) {
if (c4[i] == 1) {
printf("%d ", i + 1);
lo[k] = (i + 1);
}
}
}

Two-dimensional Arrays (matrices) in C

I'm writing code that checks if m2 matrix is a sub-matrix of m1.
Currently I have created a method that receives matrix m1 and matrix m2 of fixed size.
First of all I check that m1 is larger than m2 otherwise m2 cannot be a sub-matrix of m1. Then I have the variable result which indicates 1 if m2 is sub-matrix, or indicates 0 if it is not.
I can't find the right technique to check this thing out. I have started scanning the rows and columns of the arrays, but I don't know how to correctly structure the code to verify it correctly.
This is the code:
void CheckSubMatrix (int rows, int cols, int m1[][cols], int m2[2][2]){
int rowsm2=2;
int colsm2=2;
int result=1;
if(rows >= rowsm2 && cols >= colsm2){
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(m1[i][j] != m2[i][j]
result=0;
}
}
} else {
result = 0;
if (result)
printf("Is a Sub-Matrix");
else
printf("Is not a Sub-Matrix");
I'm a beginner and I'm trying to understand how two-dimensional arrays and matrices work in order to understand even the simplest one-dimensional arrays.
Thanks to those who will help me.
Note:
example of sub-matrix: image
EDIT:
I know that the code if(m1[i][j] != m2[i][j] result=0; doesn't work, because the program will take the values of i and j at the last for loop. But I just didn't know how to be able to implement it.
Making judicious use of functions makes the code easier, I think. Using VLA notation for all array arguments means that the code can handle your three test cases, plus a couple of extras with non-square matrices. (The use of void CheckSubMatrix (int rows, int cols, int m1[][cols], int m2[2][2]){ in the code in the question prevents any of the sample tests from working; that checking function only accepts 2x2 sub-matrices). Since the code is only checking the matrices, the matrices should be specified with the const qualifier.
#include <stdio.h>
#include <assert.h>
static int SubMatrixHere(int r, int c,
int rows1, int cols1, const int m1[rows1][cols1],
int rows2, int cols2, const int m2[rows2][cols2])
{
assert(r + rows2 <= rows1 && c + cols2 <= cols1);
for (int i = 0; i < rows2; i++)
{
for (int j = 0; j < cols2; j++)
{
if (m1[r+i][c+j] != m2[i][j])
return 0;
}
}
return 1;
}
static int CheckSubMatrix(int rows1, int cols1, const int m1[rows1][cols1],
int rows2, int cols2, const int m2[rows2][cols2])
{
if (rows1 < rows2 || cols1 < cols2)
return 0;
int ub_rows = rows1 - rows2 + 1;
int ub_cols = cols1 - cols2 + 1;
for (int i = 0; i < ub_rows; i++)
{
for (int j = 0; j < ub_cols; j++)
{
/* The test for m1[i][j] == m2[0][0] is a big saving */
if (m1[i][j] == m2[0][0] &&
SubMatrixHere(i, j, rows1, cols1, m1, rows2, cols2, m2))
return 1;
}
}
return 0;
}
static void dump_matrix(const char *tag, int rows, int cols, const int matrix[rows][cols])
{
printf("%s (%dx%d):\n", tag, rows, cols);
for (int i = 0; i < rows; i++)
{
const char *pad = "";
int length = 0;
for (int j = 0; j < cols; j++)
{
length += printf("%s%d", pad, matrix[i][j]);
if (length > 60)
{
putchar('\n');
length = 0;
pad = "";
}
else
pad = ", ";
}
if (length > 0)
putchar('\n');
}
putchar('\n');
}
static void test_submatrix(const char *t1, int r1, int c1, const int m1[r1][c1],
const char *t2, int r2, int c2, const int m2[r2][c2])
{
dump_matrix(t1, r1, c1, m1);
dump_matrix(t2, r2, c2, m2);
if (CheckSubMatrix(r1, c1, m1, r2, c2, m2))
printf("%s is a sub-matrix of %s\n", t2, t1);
else
printf("%s is not a sub-matrix of %s\n", t2, t1);
putchar('\n');
}
int main(void)
{
int m1[4][4] =
{
{ 1, 4, 6, 8 },
{ 2, 5, 7, 0 },
{ 3, 6, 9, 0 },
{ 4, 5, 8, 1 },
};
int m2[3][3] =
{
{ 1, 4, 6 },
{ 2, 5, 7 },
{ 3, 6, 9 },
};
int m3[3][3] =
{
{ 5, 7, 0 },
{ 6, 9, 0 },
{ 5, 8, 1 },
};
int m4[3][3] =
{
{ 1, 4, 6 },
{ 2, 5, 7 },
{ 3, 6, 2 },
};
test_submatrix("m1", 4, 4, m1, "m2", 3, 3, m2);
test_submatrix("m1", 4, 4, m1, "m3", 3, 3, m3);
test_submatrix("m1", 4, 4, m1, "m4", 3, 3, m4);
const int m5[6][4] =
{
{ 68, 59, 61, 70 },
{ 65, 86, 44, 9 },
{ 23, 55, 24, 31 },
{ 51, 21, 10, 99 },
{ 19, 99, 43, 95 },
{ 64, 25, 79, 67 },
};
const int m6[2][3] =
{
{ 55, 24, 31 },
{ 21, 10, 99 },
};
const int m7[4][2] =
{
{ 44, 9 },
{ 24, 31 },
{ 10, 99 },
{ 43, 96 },
};
test_submatrix("m5", 6, 4, m5, "m6", 2, 3, m6);
test_submatrix("m5", 6, 4, m5, "m7", 4, 2, m7);
return 0;
}
Output:
m1 (4x4):
1, 4, 6, 8
2, 5, 7, 0
3, 6, 9, 0
4, 5, 8, 1
m2 (3x3):
1, 4, 6
2, 5, 7
3, 6, 9
m2 is a sub-matrix of m1
m1 (4x4):
1, 4, 6, 8
2, 5, 7, 0
3, 6, 9, 0
4, 5, 8, 1
m3 (3x3):
5, 7, 0
6, 9, 0
5, 8, 1
m3 is a sub-matrix of m1
m1 (4x4):
1, 4, 6, 8
2, 5, 7, 0
3, 6, 9, 0
4, 5, 8, 1
m4 (3x3):
1, 4, 6
2, 5, 7
3, 6, 2
m4 is not a sub-matrix of m1
m5 (6x4):
68, 59, 61, 70
65, 86, 44, 9
23, 55, 24, 31
51, 21, 10, 99
19, 99, 43, 95
64, 25, 79, 67
m6 (2x3):
55, 24, 31
21, 10, 99
m6 is a sub-matrix of m5
m5 (6x4):
68, 59, 61, 70
65, 86, 44, 9
23, 55, 24, 31
51, 21, 10, 99
19, 99, 43, 95
64, 25, 79, 67
m7 (4x2):
44, 9
24, 31
10, 99
43, 96
m7 is not a sub-matrix of m5
Use the correct types for sizes (size_t)
You need more loops
int issubmatrix(size_t hrows, size_t hcols, int (*haystack)[hcols],
size_t nrows, size_t ncols, int (*needle)[ncols])
{
int itis = 0;
int exitloop = 0;
if(haystack && needle && ncols <= hcols && nrows <= hrows)
{
for(size_t start_row = 0; start_row <= hrows - nrows; start_row++)
{
for(size_t start_col = 0; start_col <= hcols - ncols; start_col++)
{
exitloop = 0;
for(size_t row = 0; row < nrows; row++)
{
for(size_t col = 0; col < ncols; col++)
{
if(haystack[start_row + row][start_col + col] != needle[row][col])
{
exitloop = 1;
break;
}
if(exitloop) break;
}
if(exitloop) break;
}
if(!exitloop) itis = 1;
}
if(itis) break;
}
}
return itis;
}
int main(void)
{
int haystack[][4] = {
{1,4,6,8},
{2,5,7,0},
{3,6,9,0},
{4,5,8,1},
};
int needle1[][3] =
{
{1,4,6},
{2,5,7},
{3,6,9},
};
int needle2[][3] =
{
{5,7,0},
{6,9,0},
{5,8,1},
};
int needle3[][3] =
{
{1,4,6},
{2,5,7},
{3,6,2},
};
printf("%d\n", issubmatrix(4,4, haystack, 3,3, needle1));
printf("%d\n", issubmatrix(4,4, haystack, 3,3, needle2));
printf("%d\n", issubmatrix(4,4, haystack, 3,3, needle3));
}
https://godbolt.org/z/YTMfWP8ca

Add 2 condition for for loop

I am using to arrays which might be differnet length.
I want to compare elements.
So I need check smallest length of array store it in var & then have to use that var in for loop.
To avoid this I want to check this lenth in for loop
like
for i:=0; (i<len(arr1)&&(i<len(arr2)) ; i++{
if arr1[i] < arr2[i] {}
}
OR
for i:=0; true == (i<len(arr1)&&(i<len(arr2)) ; i++{
if arr1[i] < arr2[i] {}
}
you could precompute which slice is longer
package main
import (
"fmt"
)
func longest(l1 []int, l2 []int) int {
if len(l1) > len(l2) {
return len(l2)
}
return len(l1)
}
func main() {
a := []int{1, 2, 3, 46, 7, 8, 9, 12, 13, 99, 87, 34}
b := []int{3, 4, 5, 6, 7, 8}
maxlen := longest(a, b)
for i := 0; i < maxlen; i++ {
if a[i] > b[i] {
fmt.Printf("%d yes", i)
}
}
}
https://play.golang.org/p/SXUwKrQrEo2

Checking for numbers within arrays in C

The function below is looking through Order1-3 and looks if any of the elements are within each of the 3 column values of each of the 8 rows inside Winning_order. So in this case Order1 values are within the first row of Winning_order so it comes out as true as well as Order2. However Order2 is not a valid output for the program, how can I modify the iterating function below so that it checks to be true. Order3 is meant to be a false as well since its not within Winning_order. The code has been gotten from the answer of this issue issue.
#include <stdio.h>
// Iterating function
int match_arrays(int *arr1, int *arr2, int len)
{
for (int i = 0; i < len; i++) {
if (arr1[i] != arr2[i]) {
return 0;
}
}
return 1;
}
// Main function
int main(void)
{
int Order1[3] = {1,5,9};
int Order2[4] = {1,2,5,3};
int Order3[3] = {4,4,4};
int Winning_order[8][3] = {{1,2,3}, {4,5,6}, {7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
for (int i = 0; i < 5; i++) {
if (match_arrays(Order1, Winning_order[i], 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3)) {
printf("Order3");
}
}
return 0;
}
Expected Output
Order 1 Order 2
This matching method will output
Order2Order1
Could you explain some detail why Order2 will be true?
#include <stdio.h>
int match_arrays(int* arr_order, int* arr_win, int len_order, int len_win)
{
int idx = 0;
int err = 0;
for (int i = 0; i < len_order; i++) {
if (arr_order[i] != arr_win[idx]) {
err++;
if (err > len_order - len_win) {
return 0;
}
} else {
idx++;
if (idx == len_win)
{
return 1;
}
}
}
return 1;
}
int main()
{
int Order1[3] = {1, 5, 9};
int Order2[4] = {1, 2, 5, 3};
int Order3[3] = {4, 4, 4};
int Winning_order[8][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, {1, 5, 9}, {3, 5, 7}};
for (int i = 0; i < 8; i++) {
if (match_arrays(Order1, Winning_order[i], 3, 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 4, 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3, 3)) {
printf("Order3");
}
}
return 0;
}

num cannot be resolved to a variable in java

I am creating program to sort the total number of hours employees work in a week in descending order. I am only having one compilation error when I try to execute my code. It says that "num" cannot be resolved to a variable...
The error occurs at following line:
sortHours(num); //Error occurs here
Where am I going wrong?
public class WorkerHours {
public static void main(String[] args) {
int[][] hours = {
{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}};
int[] weeklyHours = totalHours(hours);
sortHours(num); //Error occurs here
displayDescSort(weeklyHours);
}
public static int[] totalHours(int[][] hours){
int[] result1 = new int[8];
for (int i = 0; i < hours.length; i++){
int sum = 0;
for (int j = 0; j < hours[i].length; j++){
sum += hours[i][j];
}
result1[i] = sum;
}
return result1;
}
public static void sortHours(int[] num){
for (int i = 0; i < num.length - 1; i++){
int currentMax = num[i];
int currentMaxIndex = i;
for (int j = i + 1; j < num.length; j++){
if (currentMax < num[j]){
currentMax = num[j];
currentMaxIndex = j;
}
}
if (currentMaxIndex != i){
num[currentMaxIndex] = num[i];
num[i] = currentMax;
}
}
}
public static void displayDescSort(int[] weeklyHours){
for (int i = weeklyHours.length-1; i >= 0; i--){
System.out.println("Employee" + i + ": " + weeklyHours[i] + " hours");
}
}
}

Resources