Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab 10: Arrays in CSCI 101 - Fundamentals of Computer Programming, Lab Reports of Acting

The concept of arrays in the context of the csci 101: fundamentals of computer programming course, focusing on lab 10. Arrays are introduced as a solution to the problem of limited data storage in programs. Array initialization, indexing, and passing arrays into functions. An example program is provided to illustrate these concepts. Students are then asked to implement a simple security system program as an exercise.

Typology: Lab Reports

Pre 2010

Uploaded on 11/08/2009

koofers-user-qtw
koofers-user-qtw 🇺🇸

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download Lab 10: Arrays in CSCI 101 - Fundamentals of Computer Programming and more Lab Reports Acting in PDF only on Docsity! 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]; // array initialization Rainfall[0] = 10.0; // 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:
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved