23 Pages

ch7

Course: CS 111, Fall 2009
School: Clemson
Rating:
 
 
 
 
 

Word Count: 559

Document Preview

7 Arrays 1 Introduction Chapter to Arrays A collection of data of same type Used for lists of like items temperatures, names, etc. Avoids declaring multiple simple variables Can manipulate `list' as one entity 2 Introduction to Arrays Used when we need to keep lots of values in memory Sorting Determining the number of scores above/below the mean Printing values in the reverse order of reading...

Register Now

Unformatted Document Excerpt

Coursehero >> South Carolina >> Clemson >> CS 111

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
7 Arrays 1 Introduction Chapter to Arrays A collection of data of same type Used for lists of like items temperatures, names, etc. Avoids declaring multiple simple variables Can manipulate `list' as one entity 2 Introduction to Arrays Used when we need to keep lots of values in memory Sorting Determining the number of scores above/below the mean Printing values in the reverse order of reading 3 Declaring Arrays Static entity same size throughout program Declaring the array allocates memory 4 Declaring Arrays General Format for declaring arrays Examples int scores[300]; float hi[3284]; char alphabet[26]; <data type> <variable> [<size>]; 5 Declaring Arrays Individual parts called many things: Indexed or subscripted variables Elements of the array Value in brackets called index or subscript Numbered from 0 to (size 1) 6 Accessing Array Elements To refer to an element of an array, specify Array name and position number Format: arrayname[position number ] Example rainfall[0] refers to the first element of array rainfall 7 Accessing Array Elements Example Printf("%d", score[3]); Note two uses of brackets: In declaration, specifies SIZE of array Anywhere else, specifies a subscript/index 8 Accessing Array Elements Example Given the declaration int rainfall [28], we reference elements of rainfall by rainfall [0], rainfall [1], ... rainfall [27] subscript/index 9 Accessing Array Elements Size, subscript need not be literal int score[MAX_SCORES]; score[n+1] = 99; If n is 2, identical to: score[3] 10 Array Usage Powerful storage mechanism Can issue command like: "Do this to ith indexed variable" where i is computed by program "Display all elements of array score" 11 Array Usage Can issue command like: "Fill elements of array score from user input" "Find highest value in array score" "Find lowest value in array score" 12 forloops with Arrays Natural counting loop Naturally works well 'counting thru' elements of an array Example: (index for = 0; index<5; index++) { printf("%d", score[index]); } 13 Major Array Pitfall Array indexes always start with zero! C will 'let' you go beyond range Unpredictable results Compiler will not detect these errors! Up to programmer to 'stay in range' 14 Defined Constant as Array Size Use defined/named constant for array size Example: const int NUMBER_OF_STUDENTS =5; int score[NUMBER_OF_STUDENTS]; Improves readability Improves versatility Improves maintainability 15 Uses of Defined Constant Use everywhere size of array is needed In forloop for traversal: for (index = 0; index < NUMBER_OF_STUDENTS; index++) { // Manipulate array } In calculations involving size: lastIndex = (NUMBER_OF_STUDENTS 1); When passing array to functions 16 Initializing Arrays Arrays can be initialized at declaration. int data[3] = {2, 12, 1}; Equivalent to the following: int data[3]; data[0] = 2; data[1] = 12; data[2] = 1; 17 AutoInitializing Arrays If fewer values than size supplied: Fills from beginning Fills 'rest' with zero of array base type Example: int b[5] = {5, 12, 11}; b[0] = 5 b[1] = 12 b[2] = 11 b[3] = 0 b[4] = 0 18 AutoInitializing Arrays If arraysize is left out Declares array with size required ...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Clemson - CS - 102
Vectors and Vector Functions Basic elements of 3-D coordinate systems and linear algebra Coordinatate systems are used to assign numeric values to locations with respect to a particular frame of reference commonly referred to as the origin. The numbe
Clemson - CS - 102
Surfaces of revolution Surfaces of revolution greatly expand the range of shapes that we can easily create. The objects shown below are surfaces of revolutions created using sine and cosine functions. The object on the left is gold in color and the o
Clemson - CS - 102
Introduction to C+IntroductionThe most significant additions to C are much stronger type checkingthe introduction of true OOclassesa formal inheritance mechanism in which derived classes canspecialize parent classesformal support
Clemson - CS - 102
TreesTree Concepts Previous data organizations place data in linear order Some data organizations require categorizing data into groups, subgroups This is hierarchical classification Data items appear at various levels within the organization
Clemson - CS - 101
IntroductionData Representation Part IDifferent types of dataData Representation Data comes in different forms such as numbers, text, images, audio, video, How does the computer handle all the different data types? The most efficient so
Clemson - CS - 102
Representing rgb dataIn the raytracer we will work with three types of rgb data: reflective materials emissive lights pixels We will use rgb data for all three, but will use different models for the interaction of lights with materials than for the
Clemson - CS - 102
Polymorphism and Virtual FunctionsLate Binding / Dynamic Bindingtechnique of waiting until runtime to determine the implementation of a function. The decision of which version of a member function is appropriate is decided at runtime. Polymo
Clemson - CS - 102
Additional vector &amp; matrix operations Cross product of two vectors V = (vx, vy, vz) W= (wx, wy, wz) V x W = (vy wz - vz wy, vz wx - vx wz, vx wy - vy wx) Projection the projection of a vector V onto a plane with normal N is given by: P = V - (N dot V
Clemson - CS - 102
Hits functions Given the viewpoint, ray direction and a pointer to an object_t the mission of a hit function is to determine if the ray hits the object. If it does, the hit point and the normal vector at the hitpoint should be stored in the object_t.
Clemson - CS - 102
List management functions A linked list is a linear collection of self-referential structures, called nodes, connected by pointer links. A linked list is accessed via a pointer to the first node of the list. Subsequent nodes are accessed via the link
Clemson - CS - 102
class fplane_t: public plane_t { public: fplane_t(); fplane_t(FILE *, model_t *, int); virtual double hits(vector_t *base, vector_t *dir); virtual void dumper(FILE *); void get_dims(double *); void get_rothit(vector_t *); protected: vector_t rothit;
Clemson - CS - 102
The finite plane The finite plane is a rectangular region of finite area within a general plane. It is properly implemented as a derived class of the general plane. It is specified in the following way:fplane origin { material white normal 1 0 1 po
Clemson - CS - 102
Overloading &gt; And &lt;Overloading &lt; &lt; can be overloaded so that you can use it to output objects of the classes you define. &lt; is a binary operator The first operand is a stream What value, if any, should be returned?Overloading &lt; and &gt;#includ
Clemson - CS - 102
Void Pointers A void pointer (i.e., void *) is a generic pointer that can hold the address of any pointer type. All pointer types can be assigned a pointer to void, and a pointer to void can be assigned a pointer of any type. Consider the following s
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 4}material gray{ diffuse 7 7 7}plane floor{ material gray normal 0 1 0 point 0 0 0}light ceiling{ emissivity 5 3 3 location 4 8 -4}light front{
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 4}material gray{ diffuse 7 7 7}plane floor{ material gray normal 0 1 0 point 0 0 0}light ceiling{ emissivity 5 3 3 location 4 8 -4}light front{
Clemson - CS - 102
camera cam1{ pixeldim 800 600 worlddim 8 6 viewpoint 4 3 3}material steelblue{ ambient 0 3 4.5}material yellow{ ambient 5 4 0}sphere center{ material steelblue center 4 1 -6 radius 5}material gray{ ambien
Clemson - CS - 102
camera cam1{ pixeldim 800 600 worlddim 8 6 viewpoint 4 3 2}material green{ ambient 0 4 0}material yellow{ ambient 4 3 0}plane leftwall{ material green normal 3 0 1 point 1 0 0}plane rightwall{ material ye
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 4}material gray{ diffuse 7 7 7}plane floor{ material gray normal 0 1 0 point 0 0 0}light ceiling{ emissivity 8 5 5 location 4 8 -4}material red{
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 8}material white{ diffuse 5 5 5 ambient 1 1 1}material brown{ diffuse 3 3 0 ambient 1 1 1}light center{ location 4 3 0 emissivity 10 10 10}texplan
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 8}material white{ diffuse 5 5 5 ambient 1 1 1}material brown{ diffuse 3 3 0 ambient 1 1 1}light center{ location 4 3 0 emissivity 10 10 10}texplan
Clemson - CS - 102
Test 1 HIT Returned distance: 9.000 Plane normal: 0.000 0.000 1.000 Coordinates at hit: 0.000 0.000 -5.000 Normal vec at hit: 0.000 0.000 1.000Test 2 MISSTest 3 MISSTest 4 MISSTest
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 4}material gray{ diffuse 7 7 7}plane backwall{ material gray normal 0 0 1 point 0 0 -4}light pinkfront{ emissivity 8 5 5 location 4 3 4}
Clemson - CS - 102
camera cam1{ pixeldim 800 600 worlddim 8 6 viewpoint 4 3 3}material steelblue{ ambient 0 4 6}material yellow{ ambient 5 4 0}plane backwall{ material steelblue normal 0 0 1 point 1 0 -4}
Clemson - CS - 102
mkproto main.c &gt; rayhdrs.hmkproto list.c &gt; rayhdrs.hmkproto image.c &gt; rayhdrs.hmkproto model.cpp &gt; rayhdrs.hmkproto material.cpp &gt; rayhdrs.hmkproto raytrace.c &gt; rayhdrs.hmkproto plane.cpp &gt; rayhdrs.hmkproto object.cp
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 4}material gray{ diffuse 7 7 7}plane floor{ material gray normal 0 1 0 point 0 0 0}light ceiling{ emissivity 5 3 3 location 4 8 -4}light front{
Clemson - CS - 102
camera cam1{ pixeldim 800 600 worlddim 8 6 viewpoint 4 3 3}material steelblue{ ambient 0 3 4.5}material yellow{ ambient 5 4 0}sphere center{ material steelblue center 4 1 -6 radius 5}material gray{ ambien
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 8}material white{ diffuse 2 2 2 ambient 1 1 1}material brown{ diffuse 3 3 0 ambient 1 1 1}light center{ location 4 3 0 emissivity 10 10 10}fplane
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 4 4}material white{ diffuse 5 5 5 ambient 2 2 2}material green{ diffuse 0 5 0 ambient 0 2 0}material yellow{ diffuse 5 5 0}plane floor{ material white
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 4 4}material white{ diffuse 5 5 5 ambient 2 2 2}material green{ diffuse 0 5 0 ambient 0 2 0}material yellow{ diffuse 5 5 0 ambient 1 1 0}plane floor{
Clemson - CS - 102
camera cam1{ pixeldim 800 600 worlddim 8 6 viewpoint 4 3 3}material green{ ambient 0 5 0}material yellow{ ambient 5 4 0}plane leftwall{ material green normal 3 0 1 point 1 0 0}plane rightwall{ material ye
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 3}material white{ diffuse 5 5 5 ambient 2 2 2}fplane floor{ material white normal 0 1 0 point 0 0 0 xdir 1 0 0 dimensions 8 6}
Clemson - CS - 102
camera cam1{ pixeldim 800 600 worlddim 8 6 viewpoint 4 3 3}material steelblue{ ambient 0 3 4.5}material yellow{ ambient 5 4 0}sphere center{ material steelblue center 4 3 -6 radius 5}
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 4}material red{ diffuse 2.0 0.5 0.5}sphere shadowmaker{ material red center 4 3 -2 radius 1.5}light pinkfront{ emissivity 8 5 5 location 4 3 4}
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 4 4}material white{ diffuse 5 5 5 ambient 2 2 2}material green{ diffuse 0 5 0 ambient 0 2 0}material yellow{ diffuse 5 5 0 ambient 0 0 12}plane floor{
Clemson - CS - 102
camera cam1{ pixeldim 800 600 worlddim 8 6 viewpoint 4 3 3}material green{ ambient 0 5 0}material yellow{ ambient 5 4 0}plane leftwall{ material green normal 3 0 1 point 1 0 0}plane rightwall{ material ye
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 8}material white{ diffuse 3 3 3 ambient 1 1 1 specular 1 1 1}material green{ diffuse 1 3 1 shininess 200.0 specular 1 1 1}material red{ diffuse 3
Clemson - CS - 102
camera cam1{ pixeldim 640 480 worlddim 8 6 viewpoint 4 3 8}material white{ diffuse 3 3 3 ambient 1 1 1 specular 1 1 1}material green{ diffuse 1 3 1 shininess 200.0 specular 1 1 1}light center{ location 4
Clemson - CS - 101
Image Manipulation OperationsThe grayscale operationObjectiveTo produce an output image in which the (r, g, b) components of the output image all have the same value making the image appear gray, and the value of each pixel is the NTSC lumina
Clemson - CS - 111
Image Manipulation OperationsThe grayscale operationObjectiveTo produce an output image in which the (r, g, b) components of the output image all have the same value making the image appear gray, and the value is the value is the NTSC luminan
Clemson - CS - 101
RepresentationPart IIRange of integers A mathematical integer ranges from - to + Consequently, a mathematical integer consists of an unbounded number of bits. No computer can store all the integers in this range (would require infinite storage
Clemson - CS - 101
RepresentationPart IIRange of integers A mathematical integer ranges from to + Consequently, a mathematical integer consists of an unbounded number of bits. No computer can store all the integers in this range (would require infinite stor
Clemson - CS - 101
IntroductionData Representation Part IDifferent types of dataData RepresentationData comes in different forms such as numbers, text, images, audio, video, . How does the computer handle all the different data types? The most efficient solut
Clemson - CS - 101
PPMImagesImage representation methodsBitmap graphic method of a black-and-white imageRepresentation of color pixelsRepresentationofimagedataImages(e.g.digitalphotos)consistofarectanglular arrayofdiscretepictureelementscalledpixels. Animage
Clemson - CS - 101
Input / OutputGeneral Input and OutputI/O support is support is provided through collections of function libraries. Low level I/O open( ) close( ) read( ) write( ) lseek( ) ioctl( )General Input and OutputI/O support is support is
Clemson - CS - 101
CharacterStringsIntroduction TheClanguagehasnocharacterstringdatatype Characterstrings,suchaswordsandsentences,are storedincharacterarrays. Endofstringmarkedwithnull,\0(orabyteofbinary 0's). The%sformatcodeisusedtoread/writestringsusing scanf(
Clemson - CS - 101
BitwiseOperatorsBitwise OperatorsCiswellsuitedtosystemprogrammingbecauseit containsoperatorsthatcanmanipulatedataatthebit level.Forexample,theInternetrequiresthatbitsbe manipulatedtocreateaddressesforsubnetsand supernets. Twocategoriesofbitoop
Clemson - CS - 111
Chapter 11Pointers,Arrays,Dynamic MemoryPointer Variable A variable that stores a memory address. Can be initialized to NULL.DeclaringPointersDeclarationofpointers &lt;type&gt;*variable; Examples: int*intPtr; double*xPtr,*yPtr; char*phrase;3Dec
Clemson - CS - 101
Other Control Structuresfor, dowhile, and switch statements1The for Repetition Structure Initialization, loop condition and update all built into the forloop structure! A natural 'counting' loop2The for Repetition StructureThe general
Clemson - CS - 111
Chapter 5 Part IILooping Subtasks1Looping Subtask: CountingCounting declare an int variable for the count initialize the count to zero the count is incremented in the body of the loop2Looping Subtask: CountingGeneral form: int
Clemson - CS - 101
Arrays1Introduction to Arrays A collection of data of same type Provide a useful way to access a block of adjacent memory cells using a single name a numeric index Used for collection of like items Avoids declaring multiple simple variable
Clemson - CS - 101
Chapter 5ProgrammerDefined Functions1ProgrammerDefined Functions Building blocks of programs A program that is more than 25 lines long should be broken up into functions that have no more than 25 lines. The divideandconquer approach makes
Clemson - CS - 101
Chapter2ProgramInput/Output1IntroductiontoInput/OutputUsefulcomputationsoftenrequireamechanismbywhich theuserofaprogrammayprovideinputdatathatistobe assignedtovariables,andanothermechanismbywhichthe programmayproduceoutputdatathatmaybeviewedb
Clemson - CS - 111
Chapter 5Program Looping1Flow of ControlFlow of controlthe order in which statements are executed. When the next statement executed is not the next one in sequenceTransfer of control2Flow of ControlControl structures combin
Clemson - CS - 101
Structured Data Types1Structured Data TypesA structure can be used to combine data of different types into a single (compound) data value.2Structured Data TypesLike all data types, structures must be declared and defined. C has three
Clemson - CS - 101
Chapter 3 Flow of Control1Example/ This program reads two integers and prints their sum #include &lt;stdio.h&gt; int main() { int num1; int num2; int sum; int inputCount; inputCount = fscanf(stdin, &quot;%d %d&quot;, &amp;num1, &amp;num2);
Clemson - CS - 111
Chapter 10StringsIntroduction Strings Array with base type char End of string marked with null, `\0' Strings Array with base type char One character per indexed variable One extra character: `\0' Called `null character' End marker
Clemson - CS - 101
Pointers, Arrays, and Dynamic MemoryPointer VariablesA pointer is a variable whose value is the address of another variable. The size of the pointer variable must be n bits where 2n bytes is the size of the address space.Pointer Variables
Clemson - CS - 111
Chapter 16File Input / OutputFiles A collection of related data treated as a unit. Two types Text Binary Stored in secondary storage devices. Buffer Temporary storage area that holdes data while they are being transferred to or from
Clemson - CS - 111
Chapter 8ProgrammerDefined FunctionsProgrammerDefined Functions Building blocks of programs Divide &amp; Conquer Construct a program from smaller pieces or components Each piece more manageable than the original program Improves readability2
Clemson - CS - 111
Chapter 9Structs1StructuresA structure can be used to combine data of different types into a single (compound) data value.2StructuresLike all data types, structures must be declared and defined. C has three different ways to define