Reputation: 3
Hello I am new here and new to C programming. I am receiving a Segmentation fault when I run this code. In case 1
I am trying to get the largest number of a set value, case 2
I'm adding numbers together that I provide, in case 3
I am recording the output of a dice roll but I am getting a segmentation fault. I don't know where to look since the error does not give me a line number to the problem.
#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;
}
Upvotes: 0
Views: 106
Reputation: 4681
You are declaring arrays with the value of the variable but you are not taking any values for those variables. If the garbage value of that variable is negative, then it will indicate that you are trying to allocate negative size to array which is not right and you will get an error. First take those variables as input from the user and then limit the array size.
int i, a[1000], x; // set the initial size of array as a large value.
scanf("%d", &x);
for (i = 0; i < x; i++) { // then limit the size by the input you gave
scanf("%d", &a[i]);
}
Upvotes: 0
Reputation: 311146
The arrays have indeterminate sizes because variables x, Numb2 and k were not initialized.
int choice,DiceRoll,k,x,i,g,Max,Sum,Numb,Numb2,Array[x],Array2[Numb2],MyArray[k];
If your compiler supports variable length arrays then you could define the arrays inside cases code blocks. For example
int main( void )
{
int choice;
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:
{
int Numb;
printf("Type the # of elements you want to compare to get the largest value: ");
scanf("%d", &Numb);
int Array[Numb];
^^^^^^^^^^^^^^^^
for(x=0;x<Numb;++x)
{
printf("Number %d is : ", x+1);
scanf("%d", &Array[x] );
}
int Max = ArrayMax(Array, Numb);
printf("The largest value is %d", Max);
break;
}
//...
You should declare variables in the minimum scope where they are used.
Upvotes: 0
Reputation: 21965
You are declaring Array
with an indeterminate index x
in
int choice,DiceRoll,k,x,i,g,Max,Sum,Numb,Numb2,Array[x],Array2[Numb2],MyArray[k];
ans so is the case with Array2[Numb2]
& MyArray[k]
.
The standard says automatic variables which are not explicitly initialized will have indeterminate values.
Upvotes: 0
Reputation: 6405
The first line in your main
is
int choice,DiceRoll,k,x,i,g,Max,Sum,Numb,Numb2,Array[x],Array2[Numb2],MyArray[k];
This leaves x
as an integer with an undefined value (as you did not initialize it); it could be 1, 12345678, or -654321 - anything. Further to the right, you try to declare an int
Array (Array[x]
), which is supposed to be of that size. That is the root cause.
Depending on the (more or less random) value of x, this line might still work, but then it dumps later.
In C, you need to declare the size of an array when you declare it; it does not magically adjust itself to changes of the variable of x
later.
you should declare a constant upfront with the max array size you want to handle, or as a quick solution, pull the declaration for x in an extra line, and assign a value:
int x=1000;
int choice,DiceRoll,k,i,g,Max,Sum,Numb,Numb2,Array[x],Array2[Numb2],MyArray[k];
The same applies to k
, Numb2
, etc.
Upvotes: 2