×
Log in to StudySoup
Get Full Access to UNR - CS 135 - Study Guide - Midterm
Join StudySoup for FREE
Get Full Access to UNR - CS 135 - Study Guide - Midterm

Already have an account? Login here
×
Reset your password

cs 135 unr

cs 135 unr

Description

School: University of Nevada Reno
Department: Computer Science and Engineering
Course: Computer Science I
Professor: Monica david feil-seifer
Term: Fall 2016
Tags: Computer Science
Cost: 50
Name: CS 135 Study Guide 2
Description: These notes cover arrays, functions, and other commands relating to the CS 135 exam 2.
Uploaded: 04/03/2017
8 Pages 7 Views 9 Unlocks
Reviews


CS 135 Study Guide 2 Professors Nicolescu and Kelley


What are the Integer types?



Austin Cagle - April 2, 2017

The switch statement:

alternative to cascaded if statement

Ex:

switch (grade) {

case 4: printf(“Excellent”);

break; *breaks out of switch statement,

case 3: printf(“Good”); execution continues at the next  

break; statement after the switch*

default: printf(“Illegal grade”);

break;} *must be integer*

general format:

switch (expression) { *constant expression:

case constant-expression: characters are OK

statements floating point numbers not OK

… no variables/function calls*

default:

statements}

Conditional Operator “?:” (Ternary):

exp 1 ? exp2 : exp3 *must include “?” and “:” symbols*

reads: if exp1 then exp2 else exp3

Break statements:

can be used to break out of a loop

Ex:


What are the six combinations of integer types?



for (d = 2; d < n; d++) *breaks as soon as condition if is found if (n % d == 0) instead of d < n is not true*

break;

Continue statement:

transfers control to a point just before the end of the loop body (limited to loops only) 1

Goto statement:

capable of jumping go any statement in a function, provided that it has a label a label is an identifier placed at the beginning of a statement

identifier: statement

has the form:

goto identifier;

goto L transfers control to the statement that follows the label L

Ex:

for (d = 2; d < n; d++)

if (n % d == 0)

goto done;

done: If you want to learn more check out psy 2012 exam 4

if (d < n) *used in place of break, continue, and printf(“…”, n, d); return; not common*

else

printf(“…”, n);

Integer types:

divided into two categories: signed and unsigned


What are the two kinds of Escape Sequences?



signed integers

sign bit is 0 if it’s positive/zero and 1 if it’s negative We also discuss several other topics like calcetines masculine or feminine

01111111 100100101

positive/zero negative

six combinations of integer types:

short int unsigned short int

int unsigned int

long int unsigned long int

reading and writing integers

unsigned int u:

scanf(“%u”, &u); reads u in base 10

printf(“%u”, u); writes u in base 10

scanf(“%o”, &u); reads u in base 8

printf(“%o”, u); writes u in base 8

scanf(“%x”, &u); reads u in base 16

printf(“%x”, u); writes u in base 16

2

short int s:

scanf(“%h”, &s); for long int, use “l”; for long  

long int, use “ll” printf(“%h”, s);

Floating types:

three floating types:

float - single precision floating point

double - double precision floating point

long double - extended precision floating point

stored in scientific notation, with a sign, exponent, and fraction

single precision:  

S

Exponent

Fraction

1 bit 8 bits 23 bits

reading and writing floating point numbers

conversion specifications for single-precision floating point numbers:

%e, %f, and %g

for reading double, put the letter l in front of e, f, or g:

double d:

scanf(“%lf”, &d); *use l in scanf, not printf*

for long double, put the letter L in front of e, f, g for reading and writing Character types:

basic character type is char Don't forget about the age old question of ma 26100

character sets:

char ch; *character constants use single quotes, not double* ch = ‘a’; *can be assigned to any single character*

ch = ‘A’;

ch = ‘0’; *zero*

ch = ‘ ‘; *space*

characters are treated as small integers:

char ch;

int i;

i = ‘a’; *i is now 97*

ch = 65; *ch is now a*

ch = ch + 1; *ch is now b*

ch ++; *ch is now c*

characters can be compared like numbers

if (‘a’ <= ch && ch <= ‘z’)

ch = ch - ‘a’ + ‘A’; *comparisons use integer values of characters* 3 If you want to learn more check out cs110 rutgers

signed and unsigned characters:

signed characters range: -128 to 127

unsigned characters range: 0 to 255

the words signed and unsigned can modify char:

signed char sch;

unsigned char uch;

Character Handling Functions

toupper

ch = toupper(ch) *changes the characters to upper case*

must have the directive: We also discuss several other topics like which of the following is not considered a product cost for a merchandising firm?

#include <ctype.h>

Reading/Writing Characters Using scanf and printf

scanf(“%c”, &ch); *reads one character*

printf(“%c”, ch); *writes one character*

scanf(“ %c”, &ch); *skips white space before reading character* getchar and putchar Commands

putchar

writes a single character:

putchar(ch);

getchar

reads one character, which it returns:

ch = getchar(); *doesn’t skip white space*

returns an int value, not char If you want to learn more check out cmn-110

Escape Sequences

two kinds:

character escapes

numeric escapes

Alert (bell) \a

Backspace \b

Form feed \f

New line \n

Carriage return \r

Horizontal tab \t

Vertical tab \v

Backslash \\

Question mark \?

Single quote \’

Double quote \”

4

Constants

integer constants

numbers that appear in the text of a program

can be octal (base 8), hexadecimal (base 6), or decimal (base 10)

octal numbers

use digits 0-7 and each octal number represents power of 8

hexadecimal numbers

use digit 0-9 plus letters A-F, which are 10-15

Decimal Constants Octal Constants Hexadecimal Constants must not begin with a 0 must begin with a 0 always begin with a 0x

15, 255, 3424242 017, 0367, 078878 0xff, 0xfF, 0xFf, 0xFF, 0Xff, 0XfF,  0XFf, 0XFF

*may be either upper or lower

case*

Storing Integer Constants

Decimal Constants Octal Constants Hexadecimal Constants int int int

long int unsigned int unsigned int unsigned long int long int long int

unsigned long int unsigned long int

*compiler goes through these from top to bottom until it finds one capable of representing the  

constant*

Type Conversions and Casting

a cast expression has the form

(type-name) expression

*specifies type to be converted to*

Ex:

(int) 2.34567 *converts to integer*

= 2

Arrays

one-dimensional arrays (1D)

data structures containing a number of data values of the same type

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

a[7]

a[8]

a[9]

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

a[7]

a[8]

a[9]

array of 10 elements5

declaring a 1D array

int a[10];

number of elements

type of array’s elements

accessing the elements of an array

array_name[index_value];

integer value in range from 0 to n-1

name of array of size n

assigning values to array elements

a[0] = 1;

printf(“%d\n”, a[5]); *printf value of a[5]*

++a[7]; *increments value of a[7]*

Array Initialization

give a value to elements at time of declaration

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

1

2

3

4

5

6

7

8

9

10

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

a[7]

a[8]

a[9]

if the initializer is shorter than the array, the remaining elements are valued at 0 we can initialize an array to all zeros  

int a[10] = {0};

can write array as  

int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *array size is 10, don’t need to put [10]* The sizeof Operator

determines the size of an array in bytes

can find length of array  

sizeof(a)/sizeof(a[0]);

Multi-Dimensional Arrays

int m[5][9]; *creates 2D array*

5 rows 9 columns

accessing elements in 2D array

m[i][j] *returns element on i-th row and j-th column*

initializing a multidimensional array

int m[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

Constant Arrays

const char hex_chars[] = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘A’, ‘B’, ‘C’};

*shouldn’t be modified by program*

6

Functions

series of statements grouped together and given a name *program within a program* double average(double a, double b)

{ return (a+b)/2;}

function parameters and types

type of return value

calling the function

avg = average(x,y); *x and y are arguments to the function*

arguments can be:

variables

any expression of compatible types (x/2, y/3) or (3.4, 78.43)

Use of Return Values

return value can be used and discarded:

printf(“Avg: %g\n”, average(x,y));

return value can be saved into variable:

avg = average(x,y);

Functions With No Return Values

void print_count(int n) *return value is void*

{printf(“T minus %d and counting\n”, n);}

Functions With No Parameters

void print_pun(void)

{printf(“To C or not to C\n”);}

print_pun(); *parenthesis must be present*

print_pun; *WRONG*

General Form of a Function Definition

return-type function-name (parameters)

{ declarations List of: parameter 1-type paramater1-name,  statements} …

parameter N-type parameterN-name

Variable Use in Functions

double average(double a, double b)

{ double sum; *declaration*

sum = a + b; *statement*

return sum/2; *statement*

}

*variable declarations must come before all statements in body of function* Arguments

can use parameters as variables within the function, reducing the number of genuine  variable needed

7

int power (int x, int n) *n is a copy of the original parameter, so we can  {int i, result = 1; modify it inside the function*

for (i = 1; i <=n; i ++)

result = result * x;

return result;}

Variable Scope and Duration

duration  

portion of program during which storage for the variable exists

scope

portion of program in which variable can be referenced

local variables are limited to the body of the function in both duration and scope static local variable

have permanent storage location and retain value throughout execution void f(void)

{ static int i; *always in same location*

…}

global variables have scope and duration that equal entire program they apply to Program Termination

return function

the return value exits a function and goes back to the main function or  terminates the program entirely if it’s in the main function already

return (void)  

returns no value back to main

return (variable)

returns a specific variable value back to the main function

exit function

exits program entirely regardless of what function it’s in

exit (expression)

requires <stdlib.h>

8

Page Expired
5off
It looks like your free minutes have expired! Lucky for you we have all the content you need, just sign up here