{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Session - C Overview Session 3 Alireza Abedinejad C...

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

C++ Overview Session 3 Alireza Abedinejad

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

View Full Document
C++ Overview Selection Sort Factorial  Swap  Linear search  Search in a Table using linear search
Selection Sort #include <iostream> #include <time.h> // for using srand() and rand() functions using namespace std; void main() { int A[100]; // an array of integer with 100 elements int i, j; // Fill the array with random integers less than 10 // Seed the random-number generator with current time so that // the numbers will be different every time we run. srand( (unsigned)time( NULL ) ); for (i = 0; i < 100; i++) A[i] = rand() % 10;

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

View Full Document
Selection Sort, cont`d for (i = 0; i < 99; i++) { // find index of the minimum among A[i]..A[n] int min_index = i; for (j = i+1; j < 100; j++) if (A[j] < A[min_index]) min_index = j; // swap A[i] and A[min_index] int min = A[min_index]; A[min_index] = A[i]; A[i] = min; } // for i for (i = 0; i < 100; i++) cout << A[i] << endl; }
Factorial #include <iostream.h> int fact( int n) { int f = 1, i; for (i = 1; i <= n; i++) f *= i; return f; } void main() { int n; cout << "Enter your number: "; cin >> n; cout << n << "! = " << fact(n); }

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

View Full Document
Swap void swap( int & x, int & y) // This style of call- by-reference is C++ specific { int temp = x; x = y; y = temp; } void main() { ... int a = 1, b = 2; swap(a, b);

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

View Full Document

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