Question

2. Using Pointers and Arrays 2.1 Write a function bubblesort which will accept an array of double precision floating point numbers of length N and sort the array. Ref: https://en.wikipedia.org/wiki/Bubble_sort (I can verify this is accurate) 2.2 Write a program which exercises the bubblesort. For a good test, the program should have more than 20 values.

Program must be in C language and MUST be able to run on visual studio

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
void bubbleSort(double a[], int n) {

double temp = 0;
int i,j;
for(i=0; i < n; i++){
for(j=1; j < (n-i); j++){

if(a[j-1] > a[j]){
//swap the elements!
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}

}
}

}
int main()
{
  
int i;
double a[20]={11,2,3,5,4,6,16,14,18,19,20,10,7,9,8,12,13,15,17,55};
printf("Array elements before sorting: \n");
for(i=0;i<20;i++){
printf("%lf ", a[i]);
}
printf("\n");
bubbleSort(a, 20);
printf("Array elements after sorting: \n");
for(i=0;i<20;i++){
printf("%lf ", a[i]);
}
printf("\n");

return 0;
}

Output:

$gcc -o main *.c
$main
Array elements before sorting: 
11.000000 2.000000 3.000000 5.000000 4.000000 6.000000 16.000000 14.000000 18.000000 19.000000 20.000000 10.000000 7.000000 9.000000 8.000000 12.000000 13.000000 15.000000 17.000000 55.000000 
Array elements after sorting: 
2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000 10.000000 11.000000 12.000000 13.000000 14.000000 15.000000 16.000000 17.000000 18.000000 19.000000 20.000000 55.000000 
Add a comment
Know the answer?
Add Answer to:
Program must be in C language and MUST be able to run on visual studio 2....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be done in C++ 2013 microsoft visual studio Write a program that creates a 4...

    Must be done in C++ 2013 microsoft visual studio Write a program that creates a 4 x 5 two-dimensional array. The program should use loops to populate the array using the rand, srand and time functions with random numbers between 10 and 60. After the values have populated the array, output the values of the array to the screen in something that looks like a 4 x 5 table.

  • in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should...

    in a c++ visual studio 2017 ...Write a program that simulates a lottery. The program should have an array of five integers named lottery and should generate a random number in the range 0 through 9 for each element in the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding element in the two arrays and keep a count of the digits that match. For...

  • Using assembly language in microwoft visual studio write the following code with procedure with c...

    using assembly language in microwoft visual studio write the following code with procedure with correct number of Fib numbers Your program should detect the largest Fib number that can be calculated. Besides storing the result in an array, print them on the screen.

  • this is assembly language for x-86 processors using microsoft visual studio Create a procedure named FindThrees...

    this is assembly language for x-86 processors using microsoft visual studio Create a procedure named FindThrees that returns 1 if an array has three consecutive values of 3 somewhere in the array. Otherwise, return 0. The procedure’s input parameter list contains a pointer to the array and the array’s size. Use the PROC directive with a parameter list when declaring the procedure. Preserve all registers (except EAX) that are modified by the procedure. Write a test program that calls FindThrees...

  • Using Atmel studio 7 -C language programming Write a program to find the median of an...

    Using Atmel studio 7 -C language programming Write a program to find the median of an array of 8-bit unsigned integers. When the array has an even number of elements, the median is defined as the average of the middle two elements. Otherwise, it is defined as the middle element of the array. You need to sort the array in order to find the median. SHOW THAT THE CODE IS WORKING PROPERLY IN ATMEL STUDIO 7

  • C++ Visual Studio Program must, Accept a unknown amount of intergers Use vectors Print to screen...

    C++ Visual Studio Program must, Accept a unknown amount of intergers Use vectors Print to screen the index-order Print to screen the reverse index-order

  • Create a C# program using WINDOWS FORM App in Visual Studio. THE ANSWER MUST BE IN...

    Create a C# program using WINDOWS FORM App in Visual Studio. THE ANSWER MUST BE IN C#, IN WINDOW FORM APP, IN VISUAL STUDIOS. Write the code to: Write a method called MaximumDiffrence that accepts an integer array as a parameter and return the maximum difference between adjacent values in the array, where the gap is defined as the absolute value of the difference between the 2 adjacent values. Example: if the array contains {5, 7, 4, 9, 6, 12,...

  • MUST BE IN C++ COMPATIBLE WITH VISUAL STUDIO ALL FILES INCLUDED MUST WORK MUST BE DONE...

    MUST BE IN C++ COMPATIBLE WITH VISUAL STUDIO ALL FILES INCLUDED MUST WORK MUST BE DONE USING A CHARACTER ARRAY TO MEET THE REQUIREMENT 1.You have the following clients for your consulting firm: Client Business Type Acme Construction Johnsohn Electrical Brown Heating and Cooling Smith Switches Jones Computers Williams Cleaning Equipment Machinery design Switch manufacturing Boiler design Switch manufacturing Computer sales Machinery sales To keep track of your clients in an orderly manner, you need a program that can arrange...

  • programming in Microsoft visual studio. Write a C++ program for the following algorithm. Compile, run, and...

    programming in Microsoft visual studio. Write a C++ program for the following algorithm. Compile, run, and verify the result by choosing some test data. Prompt user to write X1 value in double Read X1 Prompt user to write X2 value in double Read X2 Prompt user to write Y1 value in double Read Y1 Prompt user to write Y2 value in double Read Y2 Compute the lengths of the two sides of the right triangle generated by the two points...

  • This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT