P.Lecture - 2.5.08

# P.Lecture - 2.5.08 - Announcements Assignment 2 Topics for...

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

Announcements Assignment 2 Topics for today Partially filled arrays rray algorithms Array algorithms Multidimensional arrays

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

View Full Document
Partially Filled Array Arrays cannot grow after they have been allocated You can allocate more space (i.e. extra) than you believe p ( ) y your program will need If you guess too low, you will still run out of space and terminate You do not need to use all the elements in an array (but total computer memory is finite) You must keep track of the actual number of elements in the partially filled array 2
Simple Array Algorithms Once you have an array with values in it there are a few common operations (and variants) that are often used in handling arrays Ordered Vs Unordered arrays it depends Counting the number of values Finding a given value Removing a value Inserting a value t i dd it Sorting an unordered array into an ordered array Example used is a partially filled array of test scores 3 each score is an integer from 0 100

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

View Full Document
Counting Occurrences /* program to find out how many students did not pass exam */ #define CLASS_SIZE 136; /* allows for reuse next year */ . . . . int exam1scores [CLASS_SIZE]; /* assume the code to fill in exam1scores is in here */ int passingValue = 60; /* the value to be used for passing */ int count = 0; r (i = 0; i < LASS SIZE i++) { for (i = 0; i < CLASS_SIZE ; i++) { if (exam1scores [i] < passingValue) { count = count + 1; } } rintf (“The number of failed scores were: %d ” count); 4 printf (The number of failed scores were: %d\n , count); . . .
#define FALSE 0 Searching For a Given Value #define TRUE 1 #define CLASS_SIZE 175 /* exam1scores is a partially filled array this time */ int exam1scores [CLASS_SIZE], numberOfStudents, i; * assume the code to fill in exam1scores is in here */ / assume the code to fill in exam1scores is in here / /* the question is: did anyone get a perfect score? */ typedef int Boolean; int targetScore = 100 ; i= 0; Boolean found = FALSE; /* This loop terminates as soon as it finds the target score */ while (i < numberOfStudents && !found) { if (exam1scores [i] = = targetScore) { found = TRUE; } else { i=i+ 1; } } if (found) { printf (“The item was found at index %d\n ”, i); 5 } else { printf (“The item was not found \n ”); } . . .

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

View Full Document
Find the Smallest Value /* goal find the lowest score in the exam1Scores array */ / g y / #define ARRAY_SIZE = 175 . . . int exam1Scores [ARRAY_SIZE ], numberOfStudents; int i, smallestIndex = 0; /* assume that the array gets filled with values in here */ /* assume that numberOfStudents holds the actual # of students */ /* start search by assuming that the 0th element is the smallest */ r (i= 1; i < numberOfStudents; i++) { for (i= 1; i < numberOfStudents; i++) { if (exam1Scores[i] < exam1Scores[smallestIndex]) { smallestIndex = i; * replace current smallest index with smaller one’s index */ / replace current smallest index with smaller one s index / } } printf (“ the lowest score found was: %d \n”,
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/22/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas.

### Page1 / 35

P.Lecture - 2.5.08 - Announcements Assignment 2 Topics for...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online