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

Arrays, Arrays and Loops and Symbolic Constants - Lecture Slides | CSE A205, Study notes of Engineering

Material Type: Notes; Class: Introduction to C Programming for Engineers; Subject: Computer Systems Engineering ; University: University of Alaska - Anchorage; Term: Fall 2008;

Typology: Study notes

2009/2010

Uploaded on 03/28/2010

koofers-user-eym-1
koofers-user-eym-1 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE294A
Introduction to C Programming for Engineers
Lecture #11
Jeffrey Miller, Ph.D.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Arrays, Arrays and Loops and Symbolic Constants - Lecture Slides | CSE A205 and more Study notes Engineering in PDF only on Docsity!

CSE294A

Introduction to C Programming for Engineers

Lecture

Jeffrey Miller, Ph.D.

Arrays

An array is a group of memory locations related by the factthat they all have the same name and the same type

Arrays can be addressed using brackets with the index (orposition number) inside the brackets

The first element of an array is in the 0 th position

So arrays are addressed from 0 to one less than the length of thearray

Each element in an array can be used just as the otherscalar variables we have discussed

To define an array, you specify the type, the name, then thenumber of elements in brackets

int nums[10]; char names[20];

Symbolic Constants

Often array sizes are specified in constants that can be used throughoutthe program

To define a constant, use the #define preprocessor directive, which isplaced below the #include statements - NOTE: Preprocessor directives are not C code – it is a symbolic constant that isreplaced with the replacement text by the C preprocessor before the program iscompiled #include <stdio.h> #define SIZE 10 void main() { int numArray[SIZE]; int i; for (i=0; i < SIZE; i++) { numArray[i] = 1; } }

Passing Arrays to Functions

When defining the function, the size of the array isomitted, though the size is usually passed as anotherparameter

When calling the function, the brackets are left offcompletely

void my_function(int numArr[], int size) { } void main() { int numArray[10]; my_function(numArray, 10); }

Program

Write a program that randomly generates 5000 numbersbetween 1 and 6, which signify the number of a die.Output the frequency that each number is generated.

srand(time(NULL)); // seeds the rand function

rand() returns a random number between 0 and RAND_MAX,which is defined in stdlib.h to be at least 32767 - 1 + rand() % 6 then gives us a number between 1 and 6, inclusively

Homework

Homework #6 is posted!