0

I'm working on the following homework question:

Build a program that uses a single-dimension array to store 10 numbers input by a user. After inputting the numbers, the user should see a menu with two options to sort and print the 10 numbers in ascending or descending order.

I'm running into some puzzling issues.

My code is posted below. I've managed to accept user input for the array's elements, and I found code for sorting an integer array here: http://www.java-samples.com/showtutorial.php?tutorialid=1577

The code in that tutorial works just fine, but as soon as I add in my code for user specified elements in the array instead of just initializing the array myself, I get a segmentation fault: core dumped error upon running the program. It'll work until it hits the sorting programming, then crashes. No errors upon compiling in gcc.

#include<stdio.h>
#include<stdbool.h>

main()
{
    int temporaryStorage;
    int counter;
    int numArray[] = {0};
    int i = 0;
    bool sortExists = true;

    for (i = 0; i < 10; ++i)
    {
        fprintf(stdout, "Enter element[%d]->", i);
        fscanf(stdin, "%d", &numArray[i]);
    }

    printf("Array Before Sorting\n");
    for(counter = 0; counter < 10; ++counter){
        printf("%d ", numArray[counter]);
    }

    while (sortExists == true)
    {
        sortExists = false;
        for (counter = 0; counter < 9; ++counter)
        {
            if (numArray[counter] > numArray[counter + 1])
            {
                temporaryStorage = numArray[counter];
                numArray[counter] = numArray[counter + 1];
                numArray[counter + 1] = temporaryStorage;
                sortExists = true;
            }
        }
    }

    printf("\n\nArray After Sorting – Ascending Order\n");
    for(counter = 0; counter < 10; ++counter){
        printf("%d ", numArray[counter]);
    }
}
2
  • Please always specify the return type of main in C, even if implicit typing was allowed in older standards. Commented Mar 2, 2013 at 5:00
  • I'm not entirely sure what you mean, as this isn't something that's been covered in our text so far. Is this why I've been seeing main declared like: int main() ? Commented Mar 2, 2013 at 8:18

2 Answers 2

3
int numArray[] = {0};

Arrays aren't dynamically sized in C. Since the right hand side is basically a int[1] your array has only a length of 1 (the length is deduced from the right hand side). You need to specify the size:

int numArray[10] = {0};

(Hint for further seg faults: Segmentation faults are almost always related to insufficient allocation, out-of-range accesses or dangling pointers)

Sign up to request clarification or add additional context in comments.

Comments

0

I have corrected your code and working fine.

#include<stdio.h>
#include<stdbool.h>

int main()
 {
  int temporaryStorage;
  int counter;
  int numArray[10];
  int i = 0;
  bool sortExists = true;

for (i = 0; i < 10; ++i)
{
    fprintf(stdout, "Enter element[%d]->", i);
    fscanf(stdin, "%d", &numArray[i]);
}

printf("Array Before Sorting\n");
for(counter = 0; counter < 10; ++counter){
    printf("%d ", numArray[counter]);
}

while (sortExists == true)
{
    sortExists = false;
    for (counter = 0; counter < 9; ++counter)
    {
        if (numArray[counter] > numArray[counter + 1])
        {
            temporaryStorage = numArray[counter];
            numArray[counter] = numArray[counter + 1];
            numArray[counter + 1] = temporaryStorage;
            sortExists = true;
        }
    }
}

printf("\n\nArray After Sorting – Ascending Order\n");
for(counter = 0; counter < 10; ++counter){
    printf("%d ", numArray[counter]);
}
return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.