안녕하세요 저는 C 프로그래밍에 대해 새롭고 새로운 것 같습니다. 이 코드를 실행하면 Segmentation 오류가 발생합니다. 경우 1
설정 값의 가장 큰 숫자를 얻으려고 애 쓰고 2
내가 제공하는 숫자를 함께 추가합니다. 3
주사위 굴림 출력을 기록하고 있지만 세그먼트 오류가 발생합니다. 오류가 나에게 문제의 줄 번호를주지 않기 때문에 어디에서보아야할지 모르겠습니다. 당신은세그먼트 오류 오류입니다. C 프로그래밍
int choice,DiceRoll,k,x,i,g,Max,Sum,Numb,Numb2,Array[x],Array2[Numb2],MyArray[k];
ans와의 불확정 인덱스 x
와 Array
를 선언하는
#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;
}
어떤 운영체제와 컴파일러를 사용하고 있습니까? 줄 번호를 찾을 수 있지만 시스템에 따라 다릅니다. – SurvivalMachine
디버거에서 프로그램을 실행하여 줄 번호를 찾습니다. – Barmar
배열의 크기로'x','Numb2','k'를 사용합니다. – Barmar