# BIL104E_w7 - BIL104E INTRODUCTION TO SCIENTIFIC AND...

This preview shows pages 1–6. Sign up to view the full content.

BIL104E INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTING (C) WEEK – 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Sorting Arrays Sorting array elements from Smaller to bigger (increasing order) Original: 3 4 2 6 7 Sorted: 2 3 4 6 7 Bigger to smaller (decreasing order) Original : 3 4 2 6 7 Sorted: 7 6 4 3 2
Increasing order: for ( pass = 1; pass < 8; pass++ ) { for ( i = 0; i < 7; i++ ) { if ( a[ i ] > a[ i + 1 ] ) { hold = a[ i ]; a[ i ] = a[ i + 1]; a[ i + 1 ] =hold; } } } a = {2,6,8,3,9,5,10,7} i=0 a[0]=2 and a[1]=6 a[0]<a[1] Skip ------------------------------------------------------------- i=1 a[1]=6 and a[2]=8 a[1]<a[2] Skip ------------------------------------------------------------- i = 2 a[2] =8 and a[3] = 3 a[2] > a[3] hold = a[2]= 8 a[2] = a[3] = 3 a[3] = hold = 8 a[2] = 3, a[3] = 8 a={2,6,3,8,9,5,10,7} ------------------------------------------------------------- i = 3 a[3] = 8 and a[4] = 9 a[3]<a[4] Skip ------------------------------------------------------------- i = 4 a[4] = 9 and a[5] = 5 a[3] >a[4] hold = a[4]= 9 a[4] = a[5] = 5 a[5] = hold = 9 a[4] = 5, a[5] = 9 a={2,6,3,8,5,9,10,7} a = {2,3,5,6,7,8,9,10} AFTER SEVERAL STEPS In decreasing order use ( a[ i ] < a[ i + 1 ] ) instead of ( a[ i ] > a[ i + 1 ] )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Application-I Sort the following array in both increased and decreased order A[N] = {11,42,4,3,8,12,9,34,25,10} Define N by using #define Use functions for Increased order Decreased order Call functions in main function and print the original, increased ordered and decreased ordered arrays
Application-I #include <stdio.h> #define N 10 void increased(int array[],int arraysize); void decreased(int array[],int arraysize); int main(){ int i; int A[N] = {11,42,4,3,8,12,9,34,25,10}; printf(" A original: \n"); for(i=0;i<N;i++){ printf("%d \t",A[i]); } printf(“\n”); printf(" Increased order: \n");

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern