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

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

View Full Document Right Arrow Icon
Announcements Assignment 2 Topics for today Partially filled arrays rray algorithms Array algorithms Multidimensional arrays
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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); . . .
Background image of page 4
#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 ”); } . . .
Background image of page 5

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

View Full DocumentRight Arrow Icon
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”,
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online