gpt4 book ai didi

c++ - 无法找出 C++ 代码的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 16:19:15 25 4
gpt4 key购买 nike

我的代码如下。当我尝试运行 addArray() 函数时会出现问题。我是 C++ 的新手,所以我不知道段错误是什么意思。

我也知道可能有更好的方法来初始化和返回二维数组,但我正在慢慢弄清楚。

我现在的主要问题是段错误。我猜这与我访问变量的方式有关?

#include <iostream>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;





int c, q, w, row, coll, quit, qq, opt;
int** arr1;
int** arr2;
int** ans;


//Method Prototypes
int menu();
inline int** getArray(int opt);
inline void printArray(int** arr, int height, int width);
void addArray();
void subtractArray();
void multiplyArrays();

void determArray();
void transposeArray();
void inverseArray();

//Prints out the menu for choosing which option to go with

int menu() {

cout << "Press 1 for Addition\n";
cout << "Press 2 for Subtraction\n";
cout << "Press 3 for Multiplication\n";
cout << "Press 4 for Determinant\n";
cout << "Press 5 for Transpose\n";
cout << "Press 6 for Inverse\n";
cout << "Press 0 to quit\n\n";

cin >> c;
return c;

}

//Main method

int main(void) {
cout << "C++ 2d Matrix Operations Menu\n";
c = menu();

while (c != 0) {

if (c == 1) {
addArray();
} else if (c == 2) {
subtractArray();
} else if (c == 3) {
void multiplyArrays();
} else if (c == 4) {
void determArray();
} else if (c == 5) {
void transposeArray();
} else if (c == 6) {
}


c = menu();
}

cout << "Press Enter to Quit. GOOD BYE";

cin >> quit;

return 0;
}

/*
Prints out the specified array.
It's arguments are the actual array and the height/weight



*/
inline void printArray(int** arr, int height, int width) {

for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
std::cout << arr[i][j] << ' ';
}
std::cout << std::endl;
}


}

//Returns an array.

inline int** getArray(int opt) {

if (opt == 0) {
cout << "How many rows and columns should be in the array?\n";
cin >> q >> w;
} else {
q = 3;
w = 3;
}

int** ary = new int*[q];
for (int i = 0; i < q; ++i) {
ary[i] = new int[w];
}

for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
cout << "What should the value be for item" << row << "," << coll << "\n";
cin >> ary[row][coll];
}
}
return ary;
}



//Adds arrays

void addArray() {

arr1 = getArray(0);
int h1 = q;
int w1 = w;

arr2 = getArray(0);
int h2 = q;
int w2 = w;

if ((h1 != h2) || (w1 != w2)) {
cout << "Both arrays must be the same size.";
return;
}



for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
ans[row][coll] = arr1[row][coll] + arr2[row][coll];
}
}
printArray(ans, q, w);

}

//Subtracts Arrays

void subtractArray() {

arr1 = getArray(0);

int h1 = q;
int w1 = w;

arr2 = getArray(0);

int h2 = q;
int w2 = w;

if ((h1 != h2) || (w1 != w2)) {
cout << "Both arrays must be the same size.";
return;
}



for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
ans[row][coll] = arr2[row][coll] - arr1[row][coll];
}
}
printArray(ans, q, w);

}

//Calculate the determinate of an array.

void determArray() {

arr1 = getArray(1);
printArray(arr1, q, w);

//There must be a better/more efficient way to do this using loops.
int determinant = arr1[0][0]*((arr1[1][1] * arr1[2][2]) - (arr1[2][1] * arr1[1][2])) - arr1[0][1]*(arr1[1][0] * arr1[2][2] - arr1[2][0] * arr1[1][2]) + arr1[0][2]*(arr1[1][0] * arr1[2][1] - arr1[2][0] * arr1[1][1]);

printf("\nDeterminant of vector using method 1 is: %d\n", determinant);



}


//Transpose an array.

void transposeArray() {
cout << "IN TRANS";
arr1 = getArray(0);
printArray(arr1, 3, 3);

//Flip the values
for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
ans[row][coll] = arr1[coll][row];
}
}

cout << "----------" << endl << "The new vector looks like: \n";
printArray(ans, q, w);

}

/*
Multiply arrays. One option is to just multiply it by a number and the other is to multiply it by another array.



*/
void multiplyArrays() {


arr1 = getArray(0);
int h1 = q;
int w1 = w;


cout << "Do you wish to multiply the first vector by a number(Enter 1), or by a second vector(Enter 2)?";
cin >> qq;

int mu;
//First Option is to multiply it by a single number
if (qq == 1) {


cout << "What number do you wish to multiply the vector by?";
cin >> mu;



for (row = 0; row < q; row++) {
for (coll = 0; coll < w; coll++) {
ans[row][coll] = arr1[row][coll] * mu;
}
}

printArray(ans, h1, w1);

//Multiply two arrays
} else if (qq == 2) {

arr2 = getArray(0);
int h2 = q;
int w2 = w;



int n1 = h1;
int n2 = w2;


int nCommon = n1;
if (n2 == nCommon) {
cout << "Amount of columns for vector 1 must match amount of rows for vector 2";
return;
}
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < nCommon; k++) {
ans[i][j] += arr1[i][k] * arr2[k][j];
}
}
}


printArray(ans, n1, n2);

}

}

最佳答案

您永远不会为ans 分配内存。就像在填充两个输入数组之前需要为它们分配存储空间一样,您需要为答案分配存储空间。

当您尝试写入您无权访问的内存时,会生成段错误。在这种情况下,因为 ans 数组没有初始化,它指向随机内存。当你执行 ans[row][coll] = arr2[row][coll] - arr1[row][coll]; 时,你会得到一个段错误,因为 ans[row][col] 指向程序空间之外的某处。

关于c++ - 无法找出 C++ 代码的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19096464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com