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 8587303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:32:14+00:00 2026-06-11T22:32:14+00:00

I am participating in Harvard’s opencourse ware and attempting the homework questions. I wrote

  • 0

I am participating in Harvard’s opencourse ware and attempting the homework questions. I wrote (or tried to) write a program in C to sort an array using bubble sort implementation. After I finished it, I tested it with an array of size 5, then 6 then 3 etc. All worked. then, I tried to test it with an array of size 11, and then that’s when it started bugging out. The program was written to stop getting numbers for the array after it hits the array size entered by the user. But, when I tested it with array size 11 it would continuously try to get more values from the user, past the size declared. It did that to me consistently for a couple days, then the third day I tried to initialize the array size variable to 0, then all of a sudden it would continue to have the same issues with an array size of 4 or more. I un-did the initialization and it continues to do the same thing for an array size of over 4. I cant figure out why the program would work for some array sizes and not others. I used main to get the array size and values from the keyboard, then I passed it to a function I wrote called sort. Note that this is not homework or anything I need to get credit, It is solely for learning. Any comments will be very much appreciated. Thanks.

/**************************************************************************** 
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 ***************************************************************************/

#include <cs50.h>
#include <stdio.h>

#include "helpers.h"

void 
sort(int values[], int n);

int main(){

    printf("Please enter the size of the array \n");
    int num = GetInt();
    int mystack[num];
    for (int z=0; z < num; z++){
        mystack[z] = GetInt();
    }

    sort(mystack, num);
}


/*
 * Sorts array of n values.
 */

void 
sort(int values[], int n)
{
    // this is a bubble sort implementation
    bool swapped = false; // initialize variable to check if swap was made

    for (int i=0; i < (n-1);){ // loops through all array values

        if (values[i + 1] > values [i]){ // checks the neighbor to see if it's bigger
            i++; // if bigger do nothing except to move to the next value in the array
        }
        else{ // if neighbor is not bigger then out of order and needs sorting
            int temp = values[i]; // store current array value in temp variable for swapping purposes
            values[i] = values[i+1]; //swap with neighbor
            values[i+1] = temp; // swap neighbor to current array value
            swapped = true; // keep track that swap was made
            i++;
       }

       // if we are at the end of array and swap was made then go back to beginning
       // and start process again.
       if((i == (n-1) && (swapped == true))){ 
           i = 0;
           swapped = false;
       }

       // if we are at the end and swap was not made then array must be in order so print it
       if((i == (n-1) && (swapped == false))){
           for (int y =0; y < n; y++){
                printf("%d", values[y]);
           }
           // exit program
           break; 
       }

   } // end for

   // return;
}
  • 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-11T22:32:15+00:00Added an answer on June 11, 2026 at 10:32 pm

    As it turns out the reason why it was doing this is because when comparing an array’s neighbor to itself as in:

    if (values[i + 1] > values [i])

    The fact that I was just checking that it is greater than, without checking if it is ‘=’ then it was causing it to behave undesirably. So if the array is for example [1, 1, 5, 2, 6, 8] then by 1 being next to a 1, my program did not account for this behavior and acted the way it did.

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

Sidebar

Related Questions

I understand that a torrent minimizes the server load by using other participating members
When using .hide() , the element is no more participating in the layout of
i'm participating in the iPhone Developer Program and i have access to the iPhone
I'm participating in a computer security competition this weekend and I'm trying to make
i am participating in google codejam 2012. I have red their Quick start guide
I'm really interested in participating as a new developer in an OpenSource project. My
I am participating in the development of a distributed solution, based on RMI, and
I'm participating in the 2010 code jam and I solved two of the problems
I am using LINQ as the ORM in a web application project using VS2010.
Is there a way to write a regular expression pattern that will create one

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.