Docsity

Lecture 12: Multi-Dimensional Arrays and Sorting in C Programming for Engineers, Study notes of Engineering

Partial preview of the text

Download Lecture 12: Multi-Dimensional Arrays and Sorting in C Programming for Engineers and more Study notes Engineering in PDF only on Docsity!

CSE294A

Introduction to C Programming for Engineers

Lecture

Jeffrey Miller, Ph.D.

Multi-Dimensional Arrays

An array is a group of memory locations related by the factthat they all have the same name and the same type

The arrays we discussed last lecture were one-dimensionalarrays, meaning that they were addressed by a single index

int my_array[10];

Multi-dimensional arrays are addressed by more than oneindex

A two-dimensional array is actually an array of arrays, althoughpeople like to think of them as “tables” or “matrices”

int my_2d_array[10][20];

Sorting

A very common algorithm used in programming issorting

Problem

Assume you have an array of integers and the numberof integers in the array, put the integers in the samearray in ascending (or descending) order

Start

End

Bubble Sort

One approach is to iterate through the entire arrayand put the smallest number in the right place

After the smallest number is at index 0, find thesecond smallest number and put it at index 1

Continue until all of the numbers are in the rightplace

Now try to write the code to do this…beforelooking at the next slide

Program

Write a program that prompts the user to enter a numberbetween 2 and 5000. Randomly generate that manynumbers, print out the original set of values, then sort thevalue and print out the sorted set of values.

Homework

Homework #6 is posted!