Processing Arrays - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Processing Arrays

Description:

Counting how many times each letter of the alphabet appears in a sentence... Quiz ... Bubble Sort. Start at beginning. Compare adjacent pairs. If out of order swap ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 32
Provided by: michael382
Category:

less

Transcript and Presenter's Notes

Title: Processing Arrays


1
Processing Arrays
  • Foundation Programming With C

Hang in there, retirement is only fifty years
away.
2
Quiz
  • What will the following code output?

include ltstdio.hgt int main() int i,
sum0 for(i0 ilt11 i2) sumi prin
tf("i\n", sum)
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

3
Quiz
  • Solution
  • 2 4 6 8 10 30
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

4
Quiz
  • What type of loop is most applicable to the
    following problems (please select one option per
    problem)?
  • Initialising an array of 10 elements to zero
  • Sentinel Controlled Loop
  • Counter Controlled Loop
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

5
Quiz
  • Reading in numbers from a user until they enter
    the value 0 (zero).
  • Sentinel Controlled Loop
  • Counter Controlled Loop
  • Reading in 10 numbers from a user
  • Sentinel Controlled Loop
  • Counter Controlled Loop
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

6
Quiz
  • Answers
  • Counter Controlled Loop (b)
  • Sentinel Controlled Loop (a)
  • Counter Controlled Loop (b)
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

7
Quiz
  • The following piece of code attempts to sum
    numbers entered by a user until the value 9999 is
    encountered. The solution below compiles but
    shows a number of errors when it is run.
    Describe the errors in words?
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

8
Quiz
include ltstdio.hgt int main() int sum int
input while(input!9999) printf("Enter
number ") scanf("i", input) sum
input printf("i\n", sum)
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

9
Quiz
  • Answers
  • sum is not initialised, the sum will be incorrect
    sometimes, but not always
  • input is not initialised or primed before while
    test
  • The sentinel will be included in the sum
  • Input from user is not validated
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

10
Quiz
  • In the following code there are a number of boxes
    where comments are to be placed. Write a comment
    for each box that describes the purpose of the
    code below it.
  • The function getValidPositiveInteger() is
    declared on a second slide.
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

11
Quiz
int main() int max, int input, int i //
max 0 // for(i0 ilt5 i)
// input getValidPositiveInteger()
// if(input gt max) max
input // printf("i\n", max)
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

12
Quiz
int getValidPositiveInteger() int input int
numbersReadIn // printf("Enter a positive
integer ") numbersReadIn scanf("i",
input) // while(numbersReadInlt1
inputlt0) // scanf("\n") scanf
("c") // printf("Invalid input.
Enter a positive integer ") numbersReadIn
scanf("i", input) // return
input
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

13
Concepts Covered
Standard Input
Types of Loops
Storing Numbers
scanf()
The while Loop
Arrays for Categories
Reading Strings
The for Loop
Searching
Scansets
The do-while Loop
Sorting
Ignoring Input
Identifiers and Loops
Multidimensional Arrays
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

Clearing Standard Input
When to Use What Loop
Command Line Arguments
File Redirection
Sentinel Controlled Loop
Validating Input
Finding Min/Max
Week 7
Week 8
Week 9
14
Storing Numbers
  • Variables good for single pieces of data
  • With arrays and loops we can
  • Store and process multiple pieces
  • In a uniform manner
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

15
Storing Numbers
  • Example Part 1

include ltstdio.hgt const int MAX_LENGTH
5 int main() int arrayMAX_LENGTH int
i int sum 0 int mean 0 for(i0 i lt
MAX_LENGTH i) printf("Enter number i ",
i1) scanf("i", arrayi) sum
arrayi
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

16
Storing Numbers
  • Example Part 2

... if(MAX_LENGTH!0) mean
sum/MAX_LENGTH printf("Mean is i\n",
mean) printf("Numbers greater than mean
are...\n") for(i0 i lt MAX_LENGTH i)
if(arrayi gt mean) printf("i\n",
arrayi)
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

Enter number 1 4 Enter number 2 2 Enter number
3 5 Enter number 4 3 Enter number 5 1 Mean is
3 Numbers greater than mean are... 4 5
17
Arrays for Categories
  • We can also use arrays to
  • Track if items from a list have been encountered,
    or
  • How many times an item has appeared
  • Example
  • Counting how many times each letter of the
    alphabet appears in a sentence...
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

18
Arrays for Categories
int letters26
  • We could declare an array where
  • letters0 will record how many times 'a' has
    been used,
  • letters1 will record how many times 'b' has
    been used, and so on
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

19
Arrays for Categories
  • Initialise before beginning

// Initialise all array elements to 0 for(i0
ilt26 i) lettersi 0
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

20
Arrays for Categories
  • Read in and categorise each character until
    newline

// Process the user input printf("Please input a
sentence...\n") scanf("c", inputLetter) while(
inputLetter ! '\n') if(isalpha(inputLetter))
letterstolower(inputLetter)-'a' scanf
("c", inputLetter)
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

21
Arrays for Categories
  • Report how many times each character appeared

// Output occurences of letters for(i0 ilt26
i) if(lettersi gt 0) printf("c i\n",
'a'i, lettersi)
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

Enter a sentence... My name is Michael a 2 c
1 e 2 h 1 i 2 ...
... l 1 m 3 n 1 s 1 y 1
22
Arrays for Categories
  • Set Count plan


CCL or SCL
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

Input Item to Count
Match Item to Array Elementand Increment Element

23
Searching in Arrays
  • We can count the number of times a value appears
    in an array

int count(int target, int array, int length)
int count0 // Counts appearances of
target int i // Iterative counter //
Compare each element in the array for(i0
iltlength i) if(arrayi target)
count return count
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

24
Searching in Arrays
  • With arrays and repetition we can now search
    through arrays

bool search(int target, int array, int length)
bool found false // Boolean search
flag int i 0 // Iterative
counter // Search until found or end of
array while(!found iltlength) // Match
array element to target value found
arrayitarget i return found
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

25
Sorting Values in Arrays
  • Many sorting algorithms
  • Bubble Sort
  • Start at beginning
  • Compare adjacent pairs
  • If out of order swap
  • Repeat n-1 times where n is the size of the array
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

26
Sorting Values in Arrays
  • Example
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

7
8
2
5
4
2
8
8
5
8
4
7
2
5
4
8
2
7
7
5
7
4
2
5
4
7
8
4
5
27
Multidimensional Arrays
  • An array of arrays
  • Declaration example

int array35
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

array03
array11
row 0
3
row 1
1
row 2
4
array24
28
Command Line Arguments
  • The main() function can have either none or two
    arguments as follows

int main(int argc, char argv) ...
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

The number of arguments including the executable
name
An array of strings, one string per argument
29
Command Line Arguments
  • Example

include ltstdio.hgt int main(int argc, char
argv) int i printf("Argument count
i\n", argc) for(i0 iltargc i)
printf("i s\n", i, argvi)
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

a.out hello there Argument count 3 0 a.out 1
hello 2 there
30
Checking Comm. Line Args
  • Check whenever command line arguments are needed

include ltstdio.hgt int main(int argc, char
argv) // Check for the correct number of
arguments if ( argc lt 2 ) printf("USAGE s
guesses\n", argv0) exit(1) // Rest of
program ...
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

31
Naming Executables
  • The -o switch allows naming
  • Using DOS/Windows append .exe

g mygrep.cpp o mygrep
  • Quiz
  • Concepts
  • Storing Numbers
  • Categories
  • Searching
  • Sorting
  • Multi-D arrays
  • Command line args

gpp mygrep.cpp o mygrep.exe
Write a Comment
User Comments (0)
About PowerShow.com