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...
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...
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...
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.
Write a code using c# Maximum Sub Array.
Write a code using c# Maximum Sub Array.
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 in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
1.Note: The following code is written only in main -- you assume there is an array...
1.Note: The following code is written only in main -- you assume there is an array bag and you are not adding any methods to that bag. Write the main program to create two array bags of type string. Place 5 strings of your choice in the first bag and 5 strings (of your choice) in the second bag. Your program must: a) determine if there are any strings in the first bag that appears in the second bag as...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT