Unformatted text preview:

CSCI 101 Fundamentals of Computer Programming Lab 10 Arrays Fall 2005 Introduction Up till now the amount of information stored within a program is severely restricted by the number of variable names declared prior to running the program This is restrictive as programs often deal with a large number of data Therefore the introduction of arrays provides a solution to this problem However one restriction of arrays is that all the variable types that are to be stored in that array must be the same An array is basically a list of cells strung together by a common name Therefore arrays are composed of two components the array name and the array index The structure of arrays is shown below double Rainfall 10 Rainfall 0 10 0 array initialization array cell declaration where Rainfall is the variable name of type double and the number 10 in brackets indicates the number of storage cells defined Notice that the number is enclosed in square brackets and not in parenthesis or curly braces The second statement stores the value 10 0 in a cell in the Rainfall array The index of that cell is denoted by the number in the brackets 0 in this case This brings us to the next point which is that indexes of arrays start with 0 and not 1 This is a very common mistake and if initialized wrongly could cause segmentation faults Generally an array is somewhat identical to a pointer where the array name is the pointer name and the brackets are the equivalent to the in pointers Therefore arrays can also be passed into functions the same way pointers are The example in the next section will clearly illustrate this concept Example The program below prompts the user to input the amount of rainfall collected for five consecutive days stores the information in an array and outputs them in a legible manner This program can be downloaded from http scf usc edu csci101 labs lab10array c include stdio h define SIZE 5 void CollectRainfall float int void PrintRainfall float int int main float Rainfall SIZE printf Inputting data n CollectRainfall Rainfall SIZE printf nOutputting data n PrintRainfall Rainfall SIZE return 0 void CollectRainfall float Rainfall int size int i 0 while i size printf Please enter the amount of rainfall collected on day d in mm i 1 scanf f Rainfall i i void PrintRainfall float Rainfall int size int i 0 while i size printf Rainfall collected on day d f mm n i 1 Rainfall i i Important points to note in the program above are how a the size of arrays are initialized b the arrays are initialized c the arrays are passed into functions d information is stored within the cells in the array e the information in the cells are accessed Exercise This week you will be implementing a simple security system program You will incorporate file input and output loops and arrays Download the lab10security dat file from http scf usc edu csci101 labs lab10security dat This is the database of authorized users that you will be writing a program for The first column indicates the ID of the personnel while the second are the access codes for those personnel You may assume that there are only 10 authorized personnel You are to write a program that does the following a Read in the file and store all the IDs and the respective access codes in appropriately named arrays b Prompts the user to input their ID number and checks if the ID number matches any of those in your database c If the ID number is invalid print a message stating that an invalid ID number has been entered and request the user to enter the ID again If the user enters the wrong ID three times print an appropriate message stating that he she has ran out of tries d If the ID number is valid prompts the user for their access code If the access code matches the one in the database print a message that access is authorized e If the access code is wrong ask the user to enter it again up to three times If the user enters the wrong access code three times in a row print an appropriate message Your TA LA will show you a sample output for this exercise Once you are done show your TA LA your completed program for lab credit Feel free to implement additional features on your program


View Full Document

USC CSCI 101L - lab10

Documents in this Course
Load more
Download lab10
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lab10 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lab10 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?