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

  • Home
  • SEARCH
  • 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 6734297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:52:19+00:00 2026-05-26T10:52:19+00:00

This is code called by main to sort an array using selections sort in

  • 0

This is code called by main to sort an array using selections sort in the C language. I have a file opened in main and put the first ten ints into an array and the 11th into a variable and then a bunch of simple functions are called. The whole thing is repeated three times. For my test file, that last two iterations have the correct printed sort, but the first starts with 0, but I don’t have a 0 in my array. It also drops the last int.

Thanks in advance for all your help!!

Here is my code:

    void sortme (int arry [], int last_int)
{
     int temp;
     int smallest_int;
     int current_int;
     int target_searcher;
     int numPrinted;

     for(current_int = 0; current_int < last_int; current_int++)
        {
            smallest_int = current_int;

            for(target_searcher = current_int + 1; target_searcher <= last_int; target_searcher++)
                if(arry[target_searcher] < arry[smallest_int])
                    smallest_int = target_searcher;

            temp = arry[current_int];
            arry[current_int] = arry[smallest_int];
            arry[smallest_int] = temp;
        }                                                                       //end outter loop

     numPrinted = 0;

     printf("\nThe sorted array is: ");
     for(current_int = 0; current_int < SIZE; current_int++)
        {
            printf("%4d", arry[current_int]);
            if(numPrinted < COUNT)
                numPrinted++;
            else
                {
                    printf("\n");
                    numPrinted = 0;
                }
        }
     printf("\n");

     return;
}

Here is my output for reference (most of the stuff is commenetd out in main.c):

The file opened.

Scanned into a[] and target is  33

ARRAY[1]
The contents in the array are:   40  32  57  27  67   6   3  89   2  99
The sorted array is:    0   2   3   6  27  32  40  57  67  89
The value searched,   33, was  not found.

Scanned into a[] and target is   3

ARRAY[2]
The contents in the array are:   86  43  89  32  45  12   1  58  98   4
The sorted array is:    1   4  12  32  43  45  58  86  89  98
The value searched,    3, was  not found.

Scanned into a[] and target is  11

ARRAY[3]
The contents in the array are:    1   2   3   4   5   6   7   8   9  10
The sorted array is:    1   2   3   4   5   6   7   8   9  10
The value searched,   11, was  not found.
Closing the file.
The file closed.
  • 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-05-26T10:52:20+00:00Added an answer on May 26, 2026 at 10:52 am

    You allow target_searcher to be equal to last_int while searching for the minimum. So, sometimes you get a random small value injected into your array (and mess with memory that doesn’t belong to you). Of course, I am assuming last_int is the length of the array.

    When you are dealing with “valid indexes only”, the range is from 0 to len-1. You can see that with an array of length 1 (in case you are ever in doubt again). Since there is only 1 element, it is at array[0], or array[len-1].

    Having said that, it is usually customary to pass parameters as array and length instead of array and index of last valid element. It is more natural. Say, you have a large array with two blocks of len1 and len2, and a function that does something with these partitions. If you use length as parameter, you use:

    processBlock(arr, len1);
    processBlock(arr + len1, len2);
    

    If you were to use last valid index, there would be all these +/-1 terms you’d have to deal with. So it is either:

    processBlockIdx(arr, len1 - 1);
    processBlockIdx(arr + len1, len2 - 1);
    

    or:

    processBlockIdx(arr, lastIdx1);
    processBlockIdx(arr + lastIdx1 +1, lastIdx2 - lastIdx1 - 1);
    

    As for the answer of your second question: yes, the problem was caused by accessing an element that is outside the bounds of your array. Since C doesn’t have the safety net of checking array bounds, an error like this usually manifests itself as an unexplained value appearing in your results, or worse, an application crash. In some cases you are not as lucky, and it manifests the problem in a completely unrelated part of your program. So, it is best to be very sure about array element accesses.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which is called at an onChange event of an function
I have a curl class, called Curl. Let's presume i have this code: $url
I have this class called SiteAsyncDownload.cs Here's the code: public class SiteAsyncDownloader { WebClient
I pasted the code given in this link to a file called md5.js. http://www.webtoolkit.info/javascript-md5.html
I'm using the following code to update a DataGridView's cell value. This is called
I have this code: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView lv =
I have this sort of code in a desktop app This is just a
I have a file called main.py and a file called classes.py main.py contains the
When i run this code the paintComponent method is not being called It may
Consider this code: public class Main { static String s = -; public static

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.