gpt4 book ai didi

c - 在数组中找到一对偶数,并将它们的平均值插入到它们之间

转载 作者:行者123 更新时间:2023-11-30 20:23:25 27 4
gpt4 key购买 nike

我几天前开始学习 C,但我很难完成这个简单的任务。

我需要做的是:

In dynamic array (all the numbers should be entered by the user) find a pair of even numbers and their average ( (arr[i] + arr[i+1])/2) ) should be added between that pair of even numbers

我写了这段代码:

#include "stdafx.h"
int *fillArray(int *arraySize);
int findPairs(int *array, int arraySize);
int newArray(int *array, int arraySize, int avg, int avgPos);
void printArray(int *array, int arraySize);
int main() {
int *array;
int arraySize;

// Creating the array
array = fillArray(&arraySize);

// Printing the array, which was entered
printf("You just have entered this: \n");
printArray(array, arraySize);

// Finding a pair of even numbers
findPairs(array, arraySize);

// Printing the new array
printf("The new array looks like this now! \n");
printArray(array, arraySize);

free(array);

_getch();
}

// Creating dynamicly allocated array
int *createArray(int arraySize) {
int *array;

array = (int*) malloc ( sizeof(int) * arraySize);
if (array == NULL) {
printf("No memmory avaible/n");
exit(8);
}

return array;
}

// Function for filling the array with elements
int *fillArray(int *arraySize) {
int *array = NULL;

// Array size
printf("Enter array size: ");
while (*arraySize < 2) {
scanf_s("%d", *&arraySize);
if (*arraySize < 2) {
printf("Array size should be bigger than 2! \n");
scanf_s("%d", *&arraySize);
}
}

// Calling function for creating array
array = createArray(*arraySize);

// Filling the array with numbers
printf("Enter %d numbers: \n", *arraySize);
for (int i = 0; i < *arraySize; i++) {
scanf_s("%d", &array[i]);
}

return array;
}

// Function for finding pairs of even numbers
int findPairs(int *array, int arraySize) {
int avg = 0, avgPos = 0;

for (int i = 0; i < arraySize; i++) {
if (array[i] % 2 == 0 && array[i + 1] % 2 == 0) {
avg = (array[i] + array[i + 1]) / 2;
avgPos = i + 1;

//Resizing the array size +1 position
*array = newArray(array, arraySize, avg, avgPos);

i++;
}
}
return *array;
}

// Function transfering into new array
int newArray(int *array, int arraySize, int avg, int avgPos) {
int *newArr = NULL;

arraySize++;
newArr = createArray(arraySize);

// Copy all the elements from 0 to the first position of first pair even numbers
for (int i = 0; i < avgPos; i++) {
newArr[i] = array[i];
}

// Add the average number between the even numbers
newArr[avgPos] = avg;

// Copy rest of the elements
for (int i = avgPos + 1; i < arraySize; i++) {
newArr[i] = array[i - 1];
}

free(array);

return *newArr;
}

// Function for printing the array
void printArray(int *array, int arraySize) {
for (int i = 0; i < arraySize; i++) {
printf("%d ", array[i]); printf("\n");
}
}

它在控制台中启动没有任何问题,但是在用数字填充数组后,您将收到此错误:

填充数组后出错:

Error after filling the array

错误列表中的错误:

Errors in error list

我哪里出错了?

最佳答案

试试这个(那些已经指出的修改)

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int *fillArray(int *arraySize);
int *findPairs(int *array, int *arraySize);
int *newArray(int *array, int arraySize, int avg, int avgPos);
void printArray(int *array, int arraySize);
int main() {
int *array;
int arraySize = 0;

// Creating the array
array = fillArray(&arraySize);

// Printing the array, which was entered
printf("You just have entered this: \n");
printArray(array, arraySize);

// Finding a pair of even numbers
array=findPairs(array, &arraySize);

// Printing the new array
printf("The new array looks like this now! \n");
printArray(array, arraySize);

free(array);

_getch();
}

// Creating dynamicly allocated array
int *createArray(int arraySize) {
int *array;

array = (int*) malloc ( sizeof(int) * arraySize);
if (array == NULL) {
printf("No memmory avaible/n");
exit(8);
}

return array;
}

// Function for filling the array with elements
int *fillArray(int *arraySize) {
int *array = NULL;

// Array size
printf("Enter array size: ");
while (*arraySize < 2) {
scanf_s("%d", arraySize);
if (*arraySize < 2) {
printf("Array size should be bigger than 2! \n");
scanf_s("%d", arraySize);
}
}

// Calling function for creating array
array = createArray(*arraySize);

// Filling the array with numbers
printf("Enter %d numbers: \n", *arraySize);
for (int i = 0; i < *arraySize; i++) {
scanf_s("%d", &array[i]);
}

return array;
}

// Function for finding pairs of even numbers
int *findPairs(int *array, int *arraySize) {
int avg = 0, avgPos = 0;

for (int i = 0; i < *arraySize -1; i++) {
if (array[i] % 2 == 0 && array[i + 1] % 2 == 0) {
avg = (array[i] + array[i + 1]) / 2;
avgPos = i + 1;

//Resizing the array size +1 position
array = newArray(array, *arraySize, avg, avgPos);
++*arraySize;
i++;
}
}
return array;
}

// Function transfering into new array
int *newArray(int *array, int arraySize, int avg, int avgPos) {
int *newArr = NULL;

arraySize++;
newArr = createArray(arraySize);

// Copy all the elements from 0 to the first position of first pair even numbers
for (int i = 0; i < avgPos; i++) {
newArr[i] = array[i];
}

// Add the average number between the even numbers
newArr[avgPos] = avg;

// Copy rest of the elements
for (int i = avgPos + 1; i < arraySize; i++) {
newArr[i] = array[i - 1];
}

free(array);

return newArr;
}

// Function for printing the array
void printArray(int *array, int arraySize) {
for (int i = 0; i < arraySize; i++) {
printf("%d ", array[i]); printf("\n");
}
}

关于c - 在数组中找到一对偶数,并将它们的平均值插入到它们之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36198333/

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