Hallo Ich bin neu hier und neu in C-Programmierung. Ich erhalte einen Segmentierungsfehler, wenn ich diesen Code ausführe. Im Fall 1
Ich versuche, die größte Zahl eines gesetzten Wertes zu erhalten, Fall 2
Ich addiere Zahlen zusammen, die ich zur Verfügung stelle, in Fall 3
ich nehme die Ausgabe einer Würfelrolle auf, aber ich bekomme einen Segmentierungsfehler. Ich weiß nicht, wo ich suchen soll, da der Fehler mir keine Zeilennummer für das Problem gibt.Segmentierungsfehlerfehler. C Programmierung
#include <stdio.h>
int ArrayMax(int arr[], int N) {
int i, Max = 0;
Max = arr[0];
for (i = 0; i < N; ++i) {
if (arr[i] > Max)
Max = arr[i];
}
return Max;
}
int ArrayAdd(int arr[], int N) {
int i, Sum;
Sum = 0;
for (i = 0; i < N; ++i) {
Sum = Sum + arr[i];
}
return Sum;
}
void DiceFill(int DR[], int many) {
int j;
for (j = 0; j < many; ++j) {
printf("You rolled: %d", DR[j]);
}
}
int main() {
int choice, DiceRoll, k, x, i, g, Max, Sum, Numb,
Numb2, Array[x], Array2[Numb2], MyArray[k];
do {
printf("\n");
printf("\n");
printf("------ Menu ------------\n");
printf("1) Find the maximum value in a set of numbers\n");
printf("2) Print the sum of a list of values. \n");
printf("3) Simulate many rolls of a standard dice.\n");
choice = -1;
while (choice < 0 || choice > 3) {
printf("Choice 0-14: ");
scanf("%d", &choice);
}
switch (choice) {
case 1:
printf("Type the # of elements you want to compare to get the largest value: ");
scanf("%d", &Numb);
for (x = 0; x < Numb; ++x) {
printf("Number %d is : ", x+1);
scanf("%d", &Array[x]);
}
Max = ArrayMax(Array, Numb);
printf("The largest value is %d", Max);
break;
case 2:
printf("Type the # of elements you want to add: ");
scanf("%d", &Numb2);
for (i = 0; i < Numb2; ++i) {
printf("Number %d is : ", i + 1);
scanf("%d", &Array2[i]);
}
Sum = ArrayAdd(Array2, Numb2);
printf("The Sum of the values is:%d", Sum);
break;
case 3:
srand(time(NULL));
printf("How many time do you want to roll a dice? ");
scanf("%d", &DiceRoll);
for (k = 0; k < DiceRoll; ++k) {
MyArray[k] = rand()% 6 + 1;
}
DiceFill(MyArray, DiceRoll);
break;
}
} while (choice > 0);
return 0;
}
Welches Betriebssystem und welchen Compiler verwenden Sie? Die Zeilennummer kann gefunden werden, aber es hängt vom System ab. – SurvivalMachine
Sie finden die Zeilennummer, indem Sie das Programm in einem Debugger ausführen. – Barmar
Sie verwenden 'x',' Numb2' und 'k' als Größen Ihrer Arrays, bevor Sie diesen Variablen etwas zuweisen. – Barmar