gpt4 book ai didi

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

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

#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,共 4 个:8

元素 3,共 4 个:4

元素 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。它是指向 array 中的 int 的指针,您可以使用 delete[] array 删除它。你不应该删除它。

另一个不通过参数返回值的函数在 swapMaxtoFront 中。我将其更改为:

void swapMaxtoFront(int* arr, int*& max_address) {
//...

Full code
模拟你的输入std::istringstream cin{"4 2 8 4 6"};

关于c++ - 试图弄清楚我需要做哪些更改才能使此代码正常工作。使用 Visual Studio ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58939001/

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