垃圾值填入我的2d数组

时间:2018-02-14 06:02:23

标签: c++ multidimensional-array

输出如下。我很难过。它的numRows + 2因为我正在添加边框

int** mirrorFramedAry = new int *[numRows+2];
for(int i=0; i<numRows+2; ++i){
    mirrorFramedAry[i] = new int[numCols+2];
}
for(int i = 0; i < numRows+2; ++i)
    for(int j = 0; j < numCols+2; ++j)
     std::cout << mirrorFramedAry[i][j] << " ";

for(int row = 1; row<= numRows; row++){
    for(int col = 1; col<= numCols; col++){
        int value;
        input >> value;
        mirrorFramedAry[row][col] = value;
        //if(row == 1) mirrorFramedAry[0][col] = value; //mirrors top row
        //if(row == numRows) mirrorFramedAry[row+1][col] = value;   //mirrors bottom row
    }
}

1533360 0 1507664 0 1986348124 1886405421 1852394844 875976519 1852400220 1547322171 1735357008 544039282 1701603654 2015895667 1546204728 762733892 1550872643 1198418253 1546925655 997091682 1348221507 1919381362 1176530273 1936026729 947398688 1146890550 1127052901 1297903728 1464299113 1650209846 1127968361 1917869114 1634887535 1766203501 544433516 909670440 1698978857 1883450742 1766677616 911689582 1768053812 977484654 1533360 0 1507664 0 1886405421 1852394844 875976519 1852400220 1547322171 1735357008 544039282 1701603654 2015895667 1546204728 762733892 1550872643 1198418253 1546925655 997091682 1348221507 1919381362 1176530273 1936026729 947398688 1146890550 1127052901 1297903728 1464299113 1650209846 1127968361 1917869114 1634887535 1766203501 544433516 909670440 1698978857 1883450742 1766677616 911689582 1768053812 977484654 1869762652 1533360 0 1507664 0 1852394844 875976519 1852400220 1547322171 1735357008 544039282 1701603654 2015895667 1546204728 762733892 1550872643 1198418253 1546925655 997091682 1348221507 1919381362 1176530273 1936026729 947398688 1146890550 1127052901 1297903728 1464299113 1650209846 1127968361 1917869114 1634887535 1766203501 544433516 909670440 1698978857 1883450742 1766677616 911689582 1768053812 1342192494 1162368065 775771224 1533360 0 1507664 0 1161975342 1397370427 1397370427 1462647621 775636563 994595671 1129532718 1111707648 1145979218 1986089021 1869180009 1817182318 1868985441 1295871346 1342194755 1162039122 1380930387 1129464159 1163151688 1381323843 1296121157 3421764 1129271888 1330860869 1145659218 1230261829 1380272454 1145913661 1176515638 1818848609 825368697 1685015840 824208485 1951604793 1768976485 824207214 1967202348 1852139636 1533360 0 0 1507664 1380974641 1397048143 1599229779 1230390610 1313818963 808661309 1917845553 1634887535 1952531565 977485153 1869762652 1835102823 1635017028 1869762560 1835102823 1701603654 977485171 1869762652 1835102823 1818838560 1342206821 1919381362 1766223201 678651244 691419256 1547322173 1735357008 544039282 1701603654 2015895667 2700856 1735357008 1466786162 842216502 1547322173 1735357008 544039282 1701603654 1533360 0 1507664 0 2037603411 1835365491 1465659955 1868852841 1867543415 1400006007 1819043176 774993500 1867340848 1701606756 1127963763 1917869114 1634887535 1766203501 544433516 909670440 1766677545 1936683619 544499311 541872467 1987208531 828142181 1415327795 1936486255 2003783772 1750299237 1550609509 1969516365 1551066476 1547322171 1735357008 544039282 1701603654 1767332979 2003788910 2003783795 1750299237 1550609509 1533360 0 0 1507664 2015895667 1546204728 1919117645 1718580079 1146298484 1096577867 1701999994 2003783772 1750299237 1550609509 1869833554 1701016181 1634623821 1551000935 1920301633 1936020069 1668445551 1851870565 1919248225 977484636 1869762652 1835102823 1818838560 673215333 691419256 1667845468 1869836146 1394635878 1551059780 1920301633 1867537509 1400006007 1819043176 1919243100 1701013878 1634623821 17016681 99 995914862 1533360 0 1507664 0 1869767529 1952870259 1262768928 2051103859 1550152309 1702326096 1701335922 1398565996 1634889588 6055271 1279415632 1128088393 1934974010 1551069797 1818391888 1358979945 1447119444 977485121 1869762652 1835102823 1818838560 673215333 691419256 1769296220 1767140195 1365009773 1937331028 1550673268 1632261201 2049859958 1392537705 1230197573 1095650895 1128088909 1869835887 1392534892 1702130553 1769096301 1533360 0 1507664 0 5461839 1347241300 1547322173 1919251285 1398103155 1095651909 1096561022 1631875184 1281122676 1818321775 1835357276 1297350768 977485136 1702057308 1432122226 1314014547 1546747457 1148219457 1549890657 1633906508 1700027500 1426092141 1146242387 1229016399 1346911566 1163089152 1297040466 1598966081 1296125778 1346850377 1229344594 1211974988 1398079568 1095651909 1430078797 544367987 1701667150 1163089152 1533360 0 1507664 0 6647137 1481589334 1230196063 1397639519 1280065876 1413566559 977485128 1869762652 1835102823 1818838 560 1331458917 1818452338 1767267429 1635087474 2020557420 1398145116 1127232561 1414417743 1397509967 1547322173 1735357008 544039282 1701603654 2015895667 1546204728 1919117645 1718580079 1767252084 1818326387 1970557728 544172388 808334385 1836008284 929984365 1869567068 6058860 1684957559 1128100457 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1507664 0 1507664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

进程已退出,返回值为3221225477 按任意键继续 。 。

2 个答案:

答案 0 :(得分:0)

当您使用1作为以下循环中行和列的起始索引时,从第二行和第二列开始填充数据:

for(int row = 1; row<=numRows; row++){
    for(int col = 1; col<=numCols; col++){
        int value;
        input >> value;
        //cout <<value<<" ";
        mirrorFramedAry[row][col] = value;
    }
}

但是,当您使用0作为以下循环中行和列的起始索引时,您将尝试显示从第一行和第一列开始的数据:

for(int i = 0; i < numRows; ++i)
  for(int j = 0; j < numCols; ++j)
    std::cout << mirrorFramedAry[i][j] << " ";

如果你看到垃圾,那是因为你没有正确初始化第一行和第一列的内容。

您可以通过以下方式解决问题:

  1. 不使用未初始化的数组元素,或
  2. 通过初始化那些数组的所有元素。

答案 1 :(得分:0)

您的初始化循环从索引1而不是索引0开始,转到numRows / numCols而不是numRows-1 / numCols-1

for(int row = 1; row<=numRows; row++){
    for(int col = 1; col<=numCols; col++){
        int value;
        input >> value;
        //cout <<value<<" ";
        mirrorFramedAry[row][col] = value;
    }
}

应该是

for(int row = 0; row<numRows; row++){
    for(int col = 0; col<numCols; col++){
        int value;
        input >> value;
        //cout <<value<<" ";
        mirrorFramedAry[row][col] = value;
    }
}