试图弄清楚我需要做哪些更改才能使代码正常工作.使用Visual Studio

Trying to figure out what changes I need to make for this code to work correctly. Using Visual Studio

本文关键字:常工作 代码 工作 Studio Visual 使用 弄清楚      更新时间:2023-10-16
#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;
}

另一件事是getMaximumPositivegetMaximumNegative函数。您不返回指向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 );