我正在尝试解决编码网站上的挑战.问题是关于int矩阵.我正在使用c#language.我需要创建一个函数,它将参数作为int[][](整数矩阵)并返回int[][](另一个整数矩阵).问题的开始(给我的空函数)就是这样
int[][] boxBlur(int[][] image) {
}
Run Code Online (Sandbox Code Playgroud)
这就是我到目前为止所尝试的
int[][] boxBlur(int[][] image) {
int pixelLength = image[0].Length - 2;
int[][] newBox = new int[pixelLength][pixelLength];
int pixelSum = 0;
int c = 0;
for(int t = 0; t<pixelLength; t++)
{
for(int i = 0; i<pixelLength; i++)
{
for(int j = t; j < 3 + t ; j++)
{
for(int k = i; k < i+3 ; k++)
{
pixelSum += image[j][k];
}
}
newBox[t][i] = pixelSum / 9; …Run Code Online (Sandbox Code Playgroud) 所以,我以这种方式动态分配了一个二维数组:
int rs, co;
cin >> ro;
cin >> co;
int **array = new int* [ro];
for(int i = 0; i < ro; i++){
array[i] = new int [co];
}
Run Code Online (Sandbox Code Playgroud)
我填写了:
for(int i = 0; i < ro; i++){
for(int j = 0; j < co; j++){
cout << "Row: " << i << " Column: " << j << " : ";
cin >> *(*(array + i) + j);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何释放用户给出X的X行或列?我知道我应该使用"删除"命令,但我无法理解
c++ multidimensional-array dynamic-memory-allocation delete-operator
如果我在php中的不同键上具有相同的值会发生什么?所以我需要删除其他有价值的键并且只保留一个.这两个数组可以是单数组也可以是多维数组.请看一下代码
Array
(
[success] => Array
(
[0] => Done.
[1] => Done.
)
[error] => Array
(
[0] => This request has already been processed.
[1] => This request has already been processed.
[2] => This request has already been processed.
[3] => You Don't Have Permission.
[4] => You Don't Have Permission.
)
[warning] => Array
(
)
)
Run Code Online (Sandbox Code Playgroud)
正如你可以看到[error]和[success]阵列上有不同的键2/3相同的值.现在我该怎么办?只保留一个.喜欢下面的一个
Array
(
[success] => Array
(
[0] => Done.
)
[error] => Array
( …Run Code Online (Sandbox Code Playgroud) using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class noise
{
public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, float scale)
{
float[,] noiseMap = new float[mapWidth, mapHeight];
if (scale <= 0)
{
scale = 0.0001f;
}
for(int y = 0; y<mapHeight; y++)
{
for (int x = 0; y < mapWidth; x++)
{
float sampleX = x/(mapWidth * scale);
float sampleY = y/(mapHeight * scale);
float perlinValue = Mathf.PerlinNoise(sampleX, sampleY);
noiseMap[x, y] = perlinValue;
Debug.Log(noiseMap.GetLength(0));
Debug.Log(noiseMap.GetLength(1));
}
}
return noiseMap; …Run Code Online (Sandbox Code Playgroud) 所以我创建了一个void函数,它应该为一个2D数组赋值178.然后我创建了一个if语句,检查这个数组是否不等于178,如果它不相等,那么计数器(int变量stevec)增加1.运行调试器我发现这个条件总是正确的,但它不应该,因为数组中的所有值在开头都等于178.我也声明了char matrix [10] [10] = {}; 在最开始的所有功能之外.
void set_matrix_to_default()
{
for(int y=1; y<=9; y++)
{
for(int x=1; x<=9; x++)
{
matrix[x][y]=178;
}
}
}
void Check_Win()
{
int stevec=0;
for(int y=1; y<=9; y++)
{
for(int x=1; x<=9; x++)
{
if(matrix[x][y]!=178)
{
stevec++;
}
else
{
}
}
}
if(stevec==9*9)
{
cout<<"Konec igre, zmagal si!";
exit(0);
}
else {}
}
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
#include<string.h>
struct name
{
char a[20];
double s;
};
void print(struct name v[][10],int row,int col)
{
int i,j;
for( i = 0; i < row; i++)
{
for(j = 0; j < col; j++){
printf("%lf\n ",v[i][j].s );
}
}
}
int main()
{
int m,n,i,j;
scanf("%d %d",&m,&n);
struct name v[m][n];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%s %lf\n",v[i][j].a,&v[i][j].s);
print(v,m,n);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,我想打印结构(v)的2d数组中的所有成员,但我无法打印第1行以外的元素(其余行元素打印为0).任何人都可以帮助如何打印第一行以外的成员?
堆栈溢出的嵌套数组问题有一个答案,它涉及一个return语句+ a + + b.(没有期间)这是什么?它有什么作用?它似乎补充说,但我不确定它是如何做到的.它与a + b的区别如何?代码如下,它有效:
var array= [1, 2, [3, 4], [], [5]];
var sum = array.toString().split(",").reduce(function(a, b) { return +a + +b;
});
console.log(sum);
Run Code Online (Sandbox Code Playgroud) 我无法打印数组"valores"中的任何项目.我不知道发生了什么.我在做一个sintaxis错误吗?当我执行console.log(valores.length)结果为0.
var valores = new Array();
fetch("https://mindicador.cl/api")
.then (data => data.json())
.then (data => {
valores["dolar"] = data.dolar.valor;
valores["euro"] = data.euro.valor;
valores["utm"] = data.utm.valor;
valores["uf"] = data.uf.valor;
});
for (var index in valores){
document.write(valores[index]+"<br>");
}
Run Code Online (Sandbox Code Playgroud) 我相信我遇到了一个错误,但我想确定我不只是错过了什么.我的实习生正在网站上工作,该网站将对可变数量的问题进行测试,并为员工完成培训提供可变数量的答案.我们将问题存储在2D数组中,其中包含问题ID和问题文本.我们循环遍历该数组,为每个问题创建div,隐藏除第一个之外的所有问题,并使用该问题ID在循环中获得该问题的答案.
我们遇到的是,当for循环到达最后一个索引时,会推送一个空元素@questions.这会使结果增加scalar @questions1,并且该循环无限重复.我们无法弄清楚的是导致空元素被推送到@questions的原因是什么?
my @questions;
my $getQuestion = $dbh->prepare("
SELECT ID, Question
FROM ACTestQuestions
WHERE TestID = ?
");
$getQuestion->execute(1);
while(my @question = $getQuestion->fetchrow_array()){
push(@questions, \@question);
}
my $sth = $dbh->prepare("
SELECT ID, AnswerText, CorrectAnswer
FROM ACTestAnswers
WHERE QuestionID = ?
ORDER BY SortOrder
");
# Irrelevant parts skipped
for(my $i = 0; $i < scalar @questions; $i++){
my $qCount = 1;
my $qID = $questions[$i][0];
my $hideClass = "hide";
if($i == 0){ …Run Code Online (Sandbox Code Playgroud) 所以我有这个Perl代码:
$array->[0][0] = "cc";
$array->[0][1] = "3";
$array->[1][0] = "aaaa";
$array->[1][1] = "2";
$array->[2][0] = "bb";
$array->[2][1] = "1";
Run Code Online (Sandbox Code Playgroud)
我需要按字母顺序(第二列)排序,以便$ array - > [0] [0]为"aaaa"而$ array - > [0] [1]为"2"
我必须在90年代的编程101期间睡着了.我花了几个小时在网上搜索代码和教程,却无法得到它.有人可以提供一些示例代码.谢谢!