2016-03-24 4 views
-2

Ich habe angefangen, C vor ein paar Tagen zu lernen, und ich habe Schwierigkeiten mit dieser einfachen Aufgabe.Finden Sie ein Paar gerade Zahl im Array und fügen Sie ihren Durchschnitt dazwischen ein

Was ich tun muss, ist:

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

ich dieses Stück Code geschrieben:

#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"); 
    } 
} 

Es beginnt in der Konsole ohne Probleme, aber nachdem Sie das Array mit Zahlen füllen, Sie werden diese Fehler erhalten:

Fehler nach dem Array Füllung:

Error after filling the array

Fehler in der Fehlerliste:

Errors in error list

Wo ich den Fehler gemacht haben?

+1

keine Bilder von Text schreiben Sie! Siehe [fragen]. – Olaf

+0

Haben Sie eine Liste erstellt? – Marievi

+0

Ich kann keine Bilder posten, dafür benötige ich 10 Punkte. Das tut mir leid! @Mariei Was für eine Liste? – D3N1EL

Antwort

1

versuchen, diese (Die Änderungen unterworfen, die bereits erwähnt worden sind)

#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"); 
    } 
} 
+0

Ich habe etwas verpasst, wie ++ * ArraySize. Danke! – D3N1EL

Verwandte Themen