Question

In: Computer Science

This is my code for an array using the bubblesort in the function outside of main....

This is my code for an array using the bubblesort in the function outside of main. My bubblesort is not working correctly , and I was told i'm missing a +j instead of +i in the function for void sorter.Can someone please help me with making my sorter work properly? everything else is fine and runs great. Thank you

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void printer(int *ptr, int length); 
void randomFill(int *ptr, int length);
void sorter(int *ptr, int length);

int main(void)

{

        int size =30;
        int array[size];
        int *myPointer;
        myPointer = array;
        int i;
        
        
        for(i =0; i<size; i++)
        {
           *myPointer = 0;
           myPointer++;
        }
           
        printf("\nArray before:\n\n");
        printer(array, size);

        
        randomFill(array, size);
        printf("\nArray after insert:\n\n");
        printer(array, size);

        
        sorter(array, size);
        printf("\nArray after sort:\n\n");
        printer(array, size);

        return 0;

}

void printer(int *ptr, int length)

{
        int i =0;
        
        for(i =0; i<length; ++i)
        printf("a[%d] = %d\n\n", i, *(ptr + i));

}

void randomFill(int *ptr, int length)

{
        srand(time(NULL));
        int i;

        for(i =0; i<length; i++)
        {
           *ptr = (rand()% (205-55+1)) + 55; 
           ptr++;
        }

}

void sorter(int *ptr, int length)

{
        int i, j, temp;
        
        for(i =0; i<length; i++)
        {
           for(j = i+1; j<length; j++)
          { 
                if(*(ptr +i) > *(ptr +j))
                {
                   temp = *(ptr + i);
                   *(ptr + i) = *(ptr + j);
                   *(ptr + j) = temp;
                }
           }
        }
}
        

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//prototypes of functions
void printer(int *ptr, int length);
void randomFill(int *ptr, int length);
void sorter(int *ptr, int length);
//driver program
int main(void)

{

srand(time(NULL));
int size =30; //set the valeu of size to 30
int array[size];
int *myPointer;
myPointer = array; //assign the base address of array to myPointer
int i;
  
//set the values of all 30 elements to 0
for(i =0; i<size; i++)
{
*myPointer = 0;
myPointer++;
}
//print the array elements
printf("\nArray before:\n\n");
printer(array, size);

//Randomly fill 30 elements into array
randomFill(array, size);
//display the elements
printf("\nArray after insert:\n\n");
printer(array, size);

//sort the 30 elements
sorter(array, size);
//display the elements after sort
printf("\nArray after sort:\n\n");
printer(array, size);

return 0;

}
//printer() method will print the elements of array through pointer
void printer(int *ptr, int length)

{
int i =0;
  
for(i =0; i<length; ++i)
printf("a[%d] = %d\n\n", i, *(ptr + i)); //print the elements

}
//rgis method will generate a random integer and assign it to array
void randomFill(int *ptr, int length)

{

int i;

for(i =0; i<length; i++)
{
*ptr = (rand()% (205-55+1)) + 55;
ptr++;
}

}
//this method implements bubble sort to sort the array elements
void sorter(int *ptr, int length)

{
int i, j, temp;
//logic for bubble sort
for(i =0; i<length; i++)
{
for(j = 0; j<length-i; j++) // in this step it will skip one element from last after each iteration
{
if(*(ptr +j) > *(ptr +(j+1))) //compare the element with its next element
{ //perform swap operation
temp = *(ptr + j);
*(ptr + j) = *(ptr +(j+1));
*(ptr +(j+1)) = temp;
}
}
}
}

OUTPUT


Related Solutions

Using the following array: //may be declared outside of the main function const int NUM_Games =4;...
Using the following array: //may be declared outside of the main function const int NUM_Games =4; //may only be declared within the main function int scores[NUM_GAMES] = {122, 76, 92, 143}; Write a C++ program to run a menu-driven program with the following choices: 1) Display the scores 2) Change a score 3) Display game with the highest score 4) Display a sorted list of the scores 5) Quit Write a function called getValidScore that allows a user to enter...
.Variable created outside of function in the main script are available within the function. (Explain by...
.Variable created outside of function in the main script are available within the function. (Explain by giving an example) a/ is it possible to include HTML in a PHP script file?explain by giving an example here is a constant: define (“LocalHost”,”127.0.0.1”); which one of the following is the correct way to refer to the above constant? LocalHost b.$LocaolHost how many times will ”I love PHP programming!”be printed in the following program segment? $count = 0 While ($count<10) eco the PHP...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T extends Comparable<T>>    void swap(T[] data, int index1, int index2)    {       T temp = data[index1];       data[index1] = data[index2];       data[index2] = temp;    }    public void sort(T[] data)    {       int position, scan;       for (position = data.length - 1; position >= 0; position--)       {          for (scan = 0; scan <= position - 1; scan++)          {...
Develop the code for this sort algorithm: Shaker shakerSort is a variation of the bubbleSort where...
Develop the code for this sort algorithm: Shaker shakerSort is a variation of the bubbleSort where we alternately go up and then down switching out-of-order pairs until done. In it have code that counts the number of moves and number of compares.
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var triangle.js Write a program that is required to use nested loops to generate a triangle as shown in the sample run below. The program should begin by prompting the user...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: coinflip.js For this program you will have two functions, one called main and the second called flip. This program is also required the use of a loop construct. Write...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: cookout.js Assume that hot dogs come in packages of 10, and hot dog buns come in packages of 8. Write a program called cookout.js, that calculates the number of...
Using an array and a function, print the values of an array backwards. Please follow these...
Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything.
C Programming build a function that is outside of main domain but can be called Build...
C Programming build a function that is outside of main domain but can be called Build a function that will : - Take 3 arrays and their lengths as input: Array 1 for even numbers, Array 2 for odd numbers. Both 1 and 2 have the same size - put all elements in array 3 elements from array 1 should be placed in the even indexes and elements of array 2 should be placed in the odd indexes, in increasing...
Write a code using c# Maximum Sub Array.
Write a code using c# Maximum Sub Array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT