Arrays - Introduction to Computer Programming - Study Guide | CPS 196, Study notes of Computer Science

Material Type: Notes; Class: Introduction to Computer Programming; Subject: Computational Science; University: Syracuse University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-qtg
koofers-user-qtg 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ARRAYS
int ages[4]; ages[0]
ages[1]
ages[2]
ages[3]
definition.
Sets up memory
ages[2] = 5; we can assign to an array element
int n;
We can use ages[2] anywhere we can use n.
ex:
t = 3 * ages[1]; formula
if (ages[3] > 65 ); comparison
abs( ages[2]); as an argument
scanf("%d", &ages[0] ); in a scanf
5
pf3
pf4
pf5

Partial preview of the text

Download Arrays - Introduction to Computer Programming - Study Guide | CPS 196 and more Study notes Computer Science in PDF only on Docsity!

ARRAYS

int ages[4]; ages[0] ages[1] ages[2] ages[3] definition. Sets up memory ages[2] = 5; we can assign to an array element int n; We can use ages[2] anywhere we can use n. ex: t = 3 * ages[1]; formula if (ages[3] > 65 ); comparison abs( ages[2]); as an argument scanf("%d", &ages[0] ); in a scanf

SOME WAYS TO DECLARE (DEFINE) AN ARRAY

double x[3] = { 1.1, 2.2, 3.3 }; initialization x[0] x[1] x[2] int m[ ] ={2, 4, 6 } m[0] m[1] m[2] int n[4] = { 5, 10 } n[0] n[1] n[2] n[3]

USING ARRAYS

int t[5]; int i; for ( i=0; i<5; i++) t[i] = 2*i; t[0] t[1] t[2] t[3] t[4] i If we now try: t = { 3, 6, 9, 12, 15 }; illegal! Only works when first declare variable. t[5] = 10; computer doesn't complain. Very bad. Something gets messed up. printf("%d", t[-1]); Computer doesn't complain. Prints junk.

char word[9] = "computer" int i; word [0] [1] [2] [3] [4] [5] [6] [7] [8] i for (i=7; i>=0; i--) printf("%c", word[i] ); output: retupmoc c o m p u t e r \ 0