试图弄清楚我需要做哪些更改才能使代码正常工作.使用Visual Studio
Trying to figure out what changes I need to make for this code to work correctly. Using Visual Studio
#include <iostream>
using namespace std;
void getMaximumPositive(int* arr, int size, int* max)
{
max = nullptr; //Set default value of max pointer
for (int i = 0; i < size; i++)
{
if (arr[i] > 0) //Only positive numbers are considered while locating the maximum
{
if (max == nullptr)
max = &arr[i];
else if (arr[i] > * max)
max = &arr[i];
}
}
}
void getMaximumNegative(int* arr, int size, int* max)
{
max = nullptr; //Set default value of max pointer
for (int i = 0; i < size; i++)
{
if (arr[i] < 0) //Only negative numbers are considered while locating the maximum
{
if (max == nullptr)
max = &arr[i];
else if (arr[i] > * max)
max = &arr[i];
}
}
}
void printArrayForwards(int* arr, int size)
{
cout << "Array contents (Forwards): " << endl;
for (int i = 0; i < size; i++)
cout << arr[i] << " : ";
cout << endl;
}
void printArrayBackwards(int* arr, int size)
{
cout << "Array contents (Backwards): " << endl;
for (int i = 0; i < size; i++)
cout << *(arr - i - 1) << " : ";
cout << endl;
}
void swapMaxtoFront(int* arr, int* max_address)
{
if (max_address == nullptr)
return; //Do nothing if max_address is null
//Print the addresses and their corresponding values of the first element and the maximum element of the array
cout << endl << "Swapping elements:" << endl;
cout << "Address 1: " << arr << " Value: " << *arr << endl;
cout << "Address 2: " << max_address << " Value: " << *max_address << endl << endl;
//Code for the swap
int temp = *arr;
*arr = *max_address;
*max_address = temp;
max_address = arr;
}
void PrintArray(int* arr, int size)
{
printArrayForwards(arr, size);
printArrayBackwards(arr, size);
}
int main()
{
//Initialize pointers to null
int* array = nullptr; //Do Not Change This Variable Definition
int* max = nullptr; //Do Not Change This Variable Definition
int arr_size;
cout << "Enter the size of your array: ";
cin >> arr_size;
array = new int[arr_size]; //Reserve memory for array of size given by user
cout << "Enter array elements: " << endl;
for (int i = 0; i < arr_size; i++)
{
cout << "Element " << i + 1 << " of " << arr_size << " : ";
cin >> *(array++);
}
PrintArray(array, arr_size);
cout << endl << endl;
cout << "-----------------------------------------------" << endl << "Finding maximum positive number in array..." << endl;
getMaximumPositive(array, arr_size, max); //Max should point to the Maximum positive value in the array
swapMaxtoFront(array, max); //Swap the maximum positive number with the first element of the array
PrintArray(array, arr_size); //Print array with swapped values.
//Print maximum positive number
cout << endl;
if (max == nullptr)
cout << "*******No positive numbers found in array" << endl;
else
cout << "*******Maximum positive number in array: " << *max << endl; //Max should point to the Maximum positive value in the array, which should now be the first element
cout << "-----------------------------------------------" << endl;
cout << endl;
cout << "-----------------------------------------------" << endl << "Finding maximum negative number in array..." << endl;
getMaximumNegative(array, arr_size, max); //Max should point to the Maximum negative value in the array
swapMaxtoFront(array, max); //Swap the maximum negative number with the first element of the array
PrintArray(array, arr_size); //Print array with swapped values.
//Print maximum negative number
cout << endl;
if (max == nullptr)
cout << "*******No negative numbers found in array" << endl;
else
cout << "*******Maximum negative number in array: " << *max << endl; //Max should point to the Maximum negative value in the array, which should now be the first element
cout << "-----------------------------------------------" << endl;
delete[] array;
delete max;
array = nullptr;
max = nullptr;
return 0;
}
输出(我正在努力寻找(:
输入阵列的大小:4
输入数组元素:
元素1(共4个(:2
元素2:8
元素3/4
要素4/6
数组内容(转发(:
2:8:4:6:
阵列内容(向后(:
6:4:8:2
正在数组中查找最大正数。。。
交换元件:
地址1:015E10A0值:2
地址2:015E10A4值:8
阵列内容(转发(
8:2:4:6:
阵列内容(向后(
6:4:2:8:
*******阵列中的最大正数:8
正在数组中查找最大负数。。。
数组内容(转发(:
8:2:4:6:
阵列内容(向后(:
6:4:2:8:
*******在阵列中找不到负数
我注意到的最大问题是如何处理array
:
array = new int[arr_size];
//...
for(size_t i = 0; i < arr_size; i++) {
cin >> *(array++); // here you step array
}
// ... and you use the final pointer "array" everywhere, undefined behaviour. It
// points one element outside of the area you allocated
delete[] array; // the final nail in the coffin,
修复:
for(int i = 0; i < arr_size; i++) {
cout << "Element " << i + 1 << " of " << arr_size << " : ";
cin >> array[i];
}
修复后,这就成了一个问题:
void printArrayBackwards(int* arr, int size) {
cout << "Array contents (Backwards): " << endl;
// arr points at the start, so arr - 0 - 1 points before start here:
for(int i = 0; i < size; i++) cout << *(arr - i - 1) << " : ";
cout << endl;
}
修复:
void printArrayBackwards(int* arr, int size) {
cout << "Array contents (Backwards): " << endl;
for(int i = size - 1; i >= 0; --i) cout << arr[i] << " : ";
cout << endl;
}
另一件事是getMaximumPositive
和getMaximumNegative
函数。您不返回指向max
中最大值的指针。我把它改为引用int*
:
void getMaximumPositive(int* arr, int size, int*& max) {
max = nullptr; // Set default value of max pointer
for(int i = 0; i < size; i++) {
if(arr[i] > 0)
{ // Only positive numbers are considered while locating the maximum
if(max == nullptr)
max = &arr[i];
else if(arr[i] > *max)
max = &arr[i];
}
}
}
void getMaximumNegative(int* arr, int size, int*& max) {
max = nullptr; // Set default value of max pointer
for(int i = 0; i < size; i++) {
if(arr[i] < 0)
{ // Only negative numbers are considered while locating the maximum
if(max == nullptr)
max = &arr[i];
else if(arr[i] > *max)
max = &arr[i];
}
}
}
我还删除了你的delete max
。它是指向使用delete[] array
删除的array
中的int
的指针。你不应该删除它。
另一个不通过参数返回值的函数在swapMaxtoFront
中。我把它改成:
void swapMaxtoFront(int* arr, int*& max_address) {
//...
使用std::istringstream cin{"4 2 8 4 6"};
模拟输入的完整代码
您将指针作为参数传递。
要接收指针,您应该传递指针的指针。
你可以这样写。。
void getMaximumPositive(int* arr, int size, int** max) {
...
*max = &arr[ i ];
...
}
...
getMaximumPositive( arr, 10, &max );
相关文章:
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- C++代码在台式机上工作正常,但在笔记本电脑上则不行
- 我想使用此代码单击某个特定窗口,但它无法正常工作
- C++代码停止工作错误使用cout内部函数
- 我的代码无法正常工作。谁能指出错误?
- 有人可以详细解释这个回文代码是如何工作的吗?
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 代码在Visual Studio 2017中不起作用,但在VS代码中工作
- 以下代码如何工作以每次为唯一调用堆栈唯一实例化模板函数?
- 添加新行时工作代码引发异常.调试技巧?
- 一个非常简单的win32套接字代码,但工作错误
- 需要WXLISTCTRL的代码更改为虚拟样式WXListCtrl的工作代码
- C++ / CannyEdgeDetection.exe 已停止工作 代码块 /OpenCV 错误:断言失败
- C //尝试重写一个工作代码以包括我的功能
- 如何在不保存文件的情况下制作打印屏幕并将其发送到FTP服务器?我的工作代码将文件保存到HDD
- 如何在不破坏当前工作代码的情况下添加小数
- 无法理解工作代码和损坏代码之间的区别
- .exe已停止工作代码块 CPP
- rapidjson:用于从文件中读取文档的工作代码