Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9302835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:20:43+00:00 2026-06-18T23:20:43+00:00

I am trying to write code for homework in C that will take 10

  • 0

I am trying to write code for homework in C that will take 10 integers from user input into an array and sort it using a recursive merge sort. We have not gone over pointers yet so I wanted to avoid using that in my code (many online examples use pointers).

Here is my code:

/* This code will take input for 10 integers given by the user
into an array, sort them with a recursive merge function
and print the updated array in ascending order. */

#include <stdio.h>
#define ARRSIZE 10

void merge_sort (int arr[], int temp[], int left, int right);
void merge (int arr[], int temp[], int left, int mid, int right);

int main (void){
    int arr[ARRSIZE], temp[ARRSIZE], left, right, i;

    printf("Enter 10 integers for an array:");
    for(i=0;i<ARRSIZE;i++){
        printf("\narray value %d:", i+1);
        scanf("%d", &arr[i]);
    }
    left = 0;
    right = ARRSIZE-1;

    merge_sort(arr, temp, left, right);

    printf("\nHere is your updated array:");
    printf("\n{");
    for(i=0;i<ARRSIZE;i++){
        printf("%d,", arr[i]);
    }
    printf("}");

    return 0;
}

void merge_sort (int arr[], int temp[], int left, int right){
    if(left<right){
        int mid = (right-left)/2;
        merge_sort(arr, temp, left, mid);
        merge_sort(arr, temp, mid+1, right);
        merge(arr, temp, left, mid, right);
    }
}

void merge (int arr[], int temp[], int left, int mid, int right){
    int i, j, tempi = 0;
    for(i=left, j=mid+1; i<=mid && j<=right ;){
        // mid+1 is the start of the right array
        if(arr[i]<arr[j] && i<=mid){
            temp[tempi] = arr[i];
            tempi++;
            i++;
        }
        else if(arr[i]>arr[j] && j<=right){
                temp[tempi] = arr[j];
                tempi++;
                j++;
             }
    }
    for(i=0,j=right; i<=j; i++){
        arr[i] = temp[i];
    }
}

I keep getting a segmentation fault when I run this in my linux shell. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:20:44+00:00Added an answer on June 18, 2026 at 11:20 pm

    Well, I’ve found one subtle bug already: int mid = (right-left)/2; should be int mid = (left+right)/2;

    Also, what flows I’ve found:

    1. You should use tempi = left for simplicity. Simply copy incoming part of array to corresponding part of temporary array.

    2. In your merge cycle, you can place incrementing tempi inside for definition:

      for( …; …; ++tempi)

    3. Inside that loop you check boundaries AFTER you have read values from that place. This is very bad. VERY. Although, You haven’t encounter any problems here just because you are checking boundaries inside for definition 🙂 simply remove them:

      for (i = left, j = 1 + mid; i <= mid && j <= right; ++tempi)
      {  
          if (arr[i] < arr[j])        temp[tempi] = arr[i++];
          else /* arr[j] <= arr[i] */ temp[tempi] = arr[j++];
      }
      
    4. Cause this loop exits when either subarray has reached end, you have to copy rest of items from another subarray to temp[]:

      if (i > mid) i = j; /* if we need to copy right subarray */
      for (; tempi <= right; ++tempi, ++i) temp[tempi] = arr[i];
      
    5. So, your copying back from temporary array will look like

       for (i = left; i <= right; ++i) arr[i] = temp[i];
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write code that will load an image from a resource, and
I am trying to write some code that will generate accurate .proto files from
I'm trying to write code that will take a given string and compare it
I'm trying to write code that will infer types from a parameter list and
I'm trying to write code that will create multiple HashSets using a for loop.
I'm trying to write code in my controller that when run, will create a
I'm trying to write a code to actually sort my array in an ascending
I am trying to write code that will cycle between rendering an animation and
I'm trying to write code that will be executed if an SQLite query won't
I am trying to write code that will allow me to find the number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.