37
CECS 130 EXAM 1

CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Embed Size (px)

Citation preview

Page 1: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

CECS 130 EXAM 1

Page 2: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

To declare a constant (read only) value:const int x = 20;const float PI = 3.14;

Can we do this? const int x;

Page 3: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

TYPE SIZE VALUES

bool 1 byte true (1) or false (0)

char 1 byte ‘a’ to‘z’ , ‘A’ to ‘Z’, ‘0’ to ‘9’, space, tab, and so on

int 4 bytes -2,147,483,648 to 2,147,483,647

short 2 bytes -32,768 to 32,767

long 4 bytes -2,147,483,648 to 2,147,483,647

float 4 bytes + - (1.2 x 10^-38 to 3.4 x 10^38)

double 8 bytes +- (2.3 x 10^-308 to -1.7 x 10^308)

Page 4: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

#include <stdio.h>#include <stdlib.h>main(){

printf(“\nC you later\n”);system(“pause”);

}

Page 5: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Page 6: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Serve to control program execution and functionality. Must end with a semicolon(;) with the exception of:

Comments: /* */ Preprocessor Directives: #include or

#define Begin and end program identifiers:

{ } Function definition beginnings: main()

Page 7: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Functions allow you to group program statements under one name

C and C++ are case-sensitive so main(), MAIN(), and Main() are all different functions.

The main function is special because the values it returns are returned to the operating system

Most main functions in this course do not take or pass information to the operating system

Page 8: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

#include <stdio.h> Using a directive to include a header file

stdio.h = standard input output header file

stdlib.h = ‘system’ commands Iostream.h= Input/Output stream

header libraryMath.h= The Math library header file

Page 9: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Definition: Escape sequences are specially sequenced characters used to format output

\” Ex: printf(“ \ “This is quoted text \ “ “);

\’ Ex: printf(“ \n A single quote looks like \’ \n”);

\* *\ Comment Block

Page 10: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Character - %c Integer - %d Float (decimal)- %f String - %s Printf Format Tags:

%[flags][width][.precision][length]specifier

%[.precision]specifer -> %.2f

Page 11: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

#include <stdio.h>

int main() { int x; for ( x = 0; x < 10; x++ ) {

printf( "%d\n", x ); }

getchar(); }

Page 12: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

int main(){ printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, 650000L); printf (" %10d \n", 1977); printf ("%010d \n", 1977); printf ("floats: %4.2f \n", 3.1416); printf ("%s \n", "A string"); return 0; }

}

Page 13: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

printf (“%c %c \n", 'a', 65); aAprintf ("%d %ld\n", 1977, 650000L); 1977650000printf (" %10d \n", 1977);

1977printf ("%010d \n", 1977); 0000001977printf ("floats: %4.2f \n", 3.1416); 3.14

printf ("%s \n", "A string"); A string

Can you create a tabular data using printf?

Page 14: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Page 15: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

#include <stdio.h>

main(){

int iOperand1 = 0;int iOperand2 = 0;

printf(“\n Enter first operand: “);scanf(“%d”, &iOperand1);printf(“\n Enter second operand: “);scanf(“%d”, &iOperand2);

printf(“The result is %d \n”, 24/(iOperand1 * iOperand2)+6/3);

}

Page 16: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Page 17: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

#include <stdio.h>

main(){

int x = 4;int y = 9;

int result1, result2;

result1 = y/x;result2 = y%x;

printf(“The result is %d.%d \n”, result1, 25*result2);

}

Page 18: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Page 19: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

x++; Tells the function to use the current value of x and increment it by 1.++x; Increment the value of x by 1 and use the new value for calculations.

x--; Tells the function to use the current value of x and decrease its value by 1.--x; Decrease the value of x by 1 and use the new value for calculations.

x=0;printf(“The Value of x is: %d”, x++);printf(“\n The Value of x is: %d”,++x);

Would results in: The Value of x is: 0The Value of x is: 2

Page 20: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Do you know the answers to these?A. !( 1 || 0 ) B. !( 1 || 1 && 0 ) C. !( ( 1 || 0 ) && 0 )

Page 21: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

A. !( 1 || 0 ) ANSWER: 0

B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)

C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

Page 22: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Page 23: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Page 24: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

What’s the syntax of While(Logical Condition){ } Do{ } While(Logical Condition)

What’s the difference between While and

Do- While()?

Page 25: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

while ( condition ) { Code to execute while the condition is true }

do { } while ( condition );

Do{} while() executes code at least once!

Page 26: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Use when the number of iterations is already known

Syntax:for ( variable initialization; condition; variable increment/decrement) { Code to execute while the condition is true }

Page 27: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Write a program using a FOR Loop to display all of the multiples of 5 from 0 to 100.

Page 28: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

#include <stdio.h>

int main() { int x; for ( x = 0; x < =20; x++ ){

printf( "%d\n", x*5 ); }

getchar(); }

Page 29: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Use to manipulate flow in loopsWhat does a Break statement do

when executed within a loop?What does a Continue statement do

when executed within a loop?

Page 30: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

#include <stdio.h>

main() { int x; for ( x = 10; x >5;

x-- ){ if (x==7) break;

} printf( “\n %d \n ”, x );}

#include <stdio.h>

main() { int x; for ( x = 10; x >5;

x-- ){ if (x==7) continue;

printf( “\n %d \n ”, x );

}}

kmnich01
"In this program, the condition (x==7) becomes true after the third iteration. Next, the break statement is executed and program control is sent out from the for loop and continues with the printf statement"(Vine)
kmnich01
7 is not included in the output because once x==7 is true, the rest of the statements are skipped and the next iteration is executed.
Page 31: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Function Prototype Syntax

return-type function_name ( arg_type arg1, ..., arg_type argN)

Function Prototypes tell you the data type returned by the function, the data type of parameters, how many parameters, and the order of parameters

Function definitions implement the function prototype

Where are function prototypes located in the program?

Where do you find function definitions?

Page 32: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Where are function prototypes located in the program? Answer: before the Main(){} Function!

Function Definitions are self contained outside of the Main(){} function

Page 33: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

#include <stdio.h> int mult (int,int);

int main() { int x; int y;

printf( "Please input two numbers to be multiplied: " ); scanf( "%d", &x ); scanf( "%d", &y ); printf( "The product of your two numbers is %d\n",

mult( x, y ) ); getchar();

}

int mult (int a, int b) { return a * b; }

Page 34: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

#include <stdio.h> Void printReportHeader();

main(){

printReportHeader;}

void printReportHeader(){

printf(“\n Column1\tColumn2\tColumn3\tColumn4 \n”);}

Page 35: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

#include <stdio.h>

void printNumbers();int iNumber;

main() { int x;

for(x=0, x<10,x++){printf(“\n Enter a number:”);scanf(“%d”, &iNumber);printNumbers();

}}

void printNumbers(){

printf(“\n Your number is: %d \n”, iNumber);}

Page 36: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;

Variable scope defines the life time of a variable

Local Scope: defined within functions and loses scope after function is finished. Can reuse in other functions (ex. p.123)

Global Scope: defined outside of functions and can be accessed by multiple functions

Page 37: CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;