APSC 160 Midterm 1
Mainly encompasses Units 1 - 7 (excluding 6)
Instruction Set - basic operations of a computer system ( ex. adding one to a number, checking if a number is equal to zero); different processors have different instructions sets
Program - collection of instructions necessary to solve a specific problem.
Algorithm - approach or method that is used to solve a problem.
Compiler - program which translates the the statements of the program developed in higher-level language into the particular instructions of the computer.
low
Assembly languages
‘low level’ language
The assembler translates the assembly language program from its symbolic format into the specfic machine instructions of the computer system (binary).
machine dependent; assembly language programs are written in terms of their instruction sets; the program will not run on a different processor type.FO
FORTRAN (formula translation)
machine independent; FORTRAN instruction/statement may result in many different machine instructions being executed.
utilizes a compiler
high
manages and operates all of the software and hardware on the computer.
controls all input and output operations performed on a computer system.
manages the computer’s systems resources and handles the execution of programs.
Unix which are the base of LinuX OR MAC OS X, Microsoft windows, iOS, Android,
Compilers
software program that analyzes a program developed in a particular computer language and translate it into a form suitable for execution on a particular system.
translates a programming language’s source code(high level language like Java or C++) into machine code.
program which manages the editing, compiling, running, and debugging of large software programs.
Xcode, Visual Studios
most IDEs support several different programming languages.
a program that directly executes the instructions in a high-level language, without converting it into machine code
programming languages in which programs are interpreted and not compiled: JavaScript, BASIC, shell, Python, sometimes C
kinda irrelevant stuff here tbh
Basic Facts about C:
you can begin typing anywhere on a line, it does not matter
case-sensitive
compilers recognize filenames that end with ‘.c’
all statements must end in ‘;’ because it terminates the program. for example the function printf() gets terminated by;
Basic Components of Any Program
#include <stdio.h>
tells the compiler information abt different functions (like printf())
int main (void)
tells system that the name of program is main() and returns an integer
void tells the system it takes no arguments
Return 0;
terminates program
0 is used by convention, you can use any integer though
Comments
tell the reader of the program the purpose of a particular program or a particular sequence of statements
// adds single line comments
/* makes all following text a comment but must be terminated like so: */
//see it all in action
#include <stdio.h>
int main ( void){
//prints out stuff
printf("I love knowt.com");
return 0;
}
Format Identifiers
describes the expected data
used in printf, scanf…
%i, %s …
Integer Type - [int] - %i
stores values of sequence of one or more digits
can be negative or positive
NO DECIMALS NO COMMAS NO SPACES OR ELSE
stored in a minimum of 32 bits of storage; at least 2 bytes - usually 4
unsigned int contains only positive values
a) short
only 2 bytes so it doesn’t get used too often
b) long - long int - %li
stores inter values up to (32 bits);
unsigned long can be used for a larger range with no negative
c) long long - long long int - %lli
too big like at least 64 bites
unsigned long long can be used for a larger range with no negative
Floating Number Type - float - %f
stores values containing decimal places
can be negative or positive
* you can omit digits before and after the decimal point but not both… (3. or .3, but not .) (apparently never tried)
* you can use %e to print it out in scientific notation (apparently never tried) and %g to tell print f to decide.
32 bits; 4 bytes
Extended Precision Type - double - %lf
can store roughly twice (64 bits) as many significant digits as float (32 bits); 8 bytes
can be negative or positive
* you can omit digits before and after the decimal point but not both… (3. or .3, but not .)
you can use %g to print it out in scientific notation (apparently never tried)
a) long double - long double - %Lf (floating point notation) or %Le(scientific)
stores doubles as at least 64 bits wide
Single Character Type - char - %c
stores a single character
formed by enclosing the character with a pair of single quotation marks so ‘a’, ‘;’ and ‘0’ are all valid characters constants
‘\n’, the newline character, is called an escape sequence and is apparently also a character constant even though its two characters?
1 byte
Boolean Data Type - _Bool
large enough to store the values 0 and 1
by convention, 0 indicates a false value and 1 indicated true
if you assign a value of 0, 0 is stored. Any nonzero value is stored as 1.
*<stdbool.h> defines values bool, true, and false
Summary Table:
Example Declaring and Outputting Each Data Type:
#include <stdio.h>
int main (void){
int integerVar = 100;
float floatingVar = 331.79;
double doubleVar = 8.44e+11;
char charVar = 'W';
_Bool boolVar = 0;
_Bool boolVar2 = 5;
printf("integerVar = %i\n", integerVar);
printf("floatingVar = %f\n", floatingVar);
printf("doubleVar = %lf\n", doubleVar);
printf("doubleVar = %g\n", doubleVar);
printf("charVar = %c\n", charVar);
printf("boolVar = %i\n", boolVar);
printf("boolVar2 = %i\n", boolVar2);
return 0;
}
Output:
integerVar = 100
floatingVar = 331.790009
doubleVar = 844000000000.000000
doubleVar = 8.44e+11
charVar = W
boolVar = 0
boolVar2 = 1
Basic Info
can store floats, ints, characters, pointers, etc.
must begin with a letter or underscore ‘_’
can be a combination of letters, underscores, or the digits 0-9
Invalid Variables
cannot contain invalid characters ie. sum&value
no embedded spaces ie. sum value
cannot start with number ie. 9sumvalue
cannot use a reserved word ie. int
Basic Info on Working With Arithmetic Expression
BEDMAS applies
binary arithmetic operators: operate on two values or terms
if you apply any of these operators with an int and a float, the output will be a float. If you use two integers, the output will be an integer.
if a float type value is assigned to an integer variable, they number will be truncated to only its integer portion. NOT ROUNDED. ie 123.5 becomes 123.
Binary Arithmetic Operators
*modulus only works with int types. you should use double fmod() in math.h otherwise.
Type Cast Operator
Casting values temporarily converts the value of the variable to another data type. It does not effect the value stored in the variable. Has higher precedence than all the arithmetic operators other than unary- and unary+.
* unary means on one operand so like x = -x.
Assignment Operators
Assigns after arithmetic operator has been applied.
*Im gonna assume we don’t need to know this. 🤞
includes 3 statements within corresponding fields
General Format:
for (initial expression; loop condition; loop_expression){
sum += n;
}
for (int a = 1, int j = 20; a <= 200; a++, j =- 10){
total+= a;
}
The first component - initial expression - sets the initial value of a variable.
The second component - loop condition - the condition, or conditions, that are necessary for the loop to continue.
The final component - loop expression - an expression that is evaluated each time after the body of the loop is executed.
Loop Variants: In all cases, you can include multiple expression in any of the fields but these should be separated by commas.
**keep in mind that == means equal to but = will assign a variable
You can print values using format specifiers with field width specifications. For example, %ni or %nlf will ensure that the value you print will take up n columns. If n = 2 and your integer takes up less than 2 colums, it will be displayed with a leading space.
**printf() + scanf() stuff. I’m not gonna bother. Remember when you use scanf(), ensure that your variable starts with &.
General Format:
while (expression){
program statements
}
while (n < 3){
print ("%i", n);
n++;
}
The while statement runs the inner program statements while the expression = TRUE. If the expression finally evaluates as FALSE, the loop is terminated.
For and While loops evaluates the condition before the loop is executed, do while loops ensure evaluate the condition after the loop. This ensures that the looping is executed at least once.
General Format:
do{
program statements
}
while (loop expression);
do {
printf("i", num);
n--;
}
while ( n >= 0);
You can use a break statement to leave a loop as soon as a certain condition occurs. The break statement causes the program to immediately exit from the loop it is executing, continuing to whatever statement follow the loop.
General Format:
break;
This statement causes the loop in which it is executed in to be continued. Any statement in the loop after the continue statement are automatically skipped and the next iteration of the loop is continued.
General Format
continue;
Statements inside the if statement only run if the expression is TRUE. Also, only runs once.
Statements inside the else if statement only run if the preceding if statement did NOT execute AND if the expression is TRUE.
Statements inside the else statement only run if the preceding if statements and else if statements did NOT execute.
General Format:
if (expression){
program statement
}
else if (expression){
program statement
}
else{
program statement
}
*Nested while, for, do, or if loops just means another while, for, do, or if loop inside the outside statement respectively.
*Prob won’t be tested on this but really easy stuff.
Essentially, the switch acts like an if statement. If the expression is true, each case acts as an if else statement and the default statement as an else statement.
*notes are from the screencast bc I think they’re better
Instead of writing all the code in the function main, you can call a function and instead, write the code within that function outside of main.
void printRow( int row );
int main( void ) {
int size;
int row;
printf( "Enter size of triangle: " );
scanf( "%d", &size );
row = 1;
while( row <= size ) {
printRow( row );
row++;
}
system( "PAUSE" );
return 0;
}
*/
void printRow( int row ) {
int star;
star = 1;
while( star <= row ) {
printf( "*" );
star++;
}
printf( "\n" );
}
You must first call a function before you run it. To call a function, include the type of data the function will be outputting, the name of the function, two parenthesis with the data type of the argument which will be inputted into the function, followed by a semicolon.
You must then run the function. To execute a function, write the function name, and semicolons which include the argument which will be
function’s name followed by two parentheses and a semicolon.
Mainly encompasses Units 1 - 7 (excluding 6)
Instruction Set - basic operations of a computer system ( ex. adding one to a number, checking if a number is equal to zero); different processors have different instructions sets
Program - collection of instructions necessary to solve a specific problem.
Algorithm - approach or method that is used to solve a problem.
Compiler - program which translates the the statements of the program developed in higher-level language into the particular instructions of the computer.
low
Assembly languages
‘low level’ language
The assembler translates the assembly language program from its symbolic format into the specfic machine instructions of the computer system (binary).
machine dependent; assembly language programs are written in terms of their instruction sets; the program will not run on a different processor type.FO
FORTRAN (formula translation)
machine independent; FORTRAN instruction/statement may result in many different machine instructions being executed.
utilizes a compiler
high
manages and operates all of the software and hardware on the computer.
controls all input and output operations performed on a computer system.
manages the computer’s systems resources and handles the execution of programs.
Unix which are the base of LinuX OR MAC OS X, Microsoft windows, iOS, Android,
Compilers
software program that analyzes a program developed in a particular computer language and translate it into a form suitable for execution on a particular system.
translates a programming language’s source code(high level language like Java or C++) into machine code.
program which manages the editing, compiling, running, and debugging of large software programs.
Xcode, Visual Studios
most IDEs support several different programming languages.
a program that directly executes the instructions in a high-level language, without converting it into machine code
programming languages in which programs are interpreted and not compiled: JavaScript, BASIC, shell, Python, sometimes C
kinda irrelevant stuff here tbh
Basic Facts about C:
you can begin typing anywhere on a line, it does not matter
case-sensitive
compilers recognize filenames that end with ‘.c’
all statements must end in ‘;’ because it terminates the program. for example the function printf() gets terminated by;
Basic Components of Any Program
#include <stdio.h>
tells the compiler information abt different functions (like printf())
int main (void)
tells system that the name of program is main() and returns an integer
void tells the system it takes no arguments
Return 0;
terminates program
0 is used by convention, you can use any integer though
Comments
tell the reader of the program the purpose of a particular program or a particular sequence of statements
// adds single line comments
/* makes all following text a comment but must be terminated like so: */
//see it all in action
#include <stdio.h>
int main ( void){
//prints out stuff
printf("I love knowt.com");
return 0;
}
Format Identifiers
describes the expected data
used in printf, scanf…
%i, %s …
Integer Type - [int] - %i
stores values of sequence of one or more digits
can be negative or positive
NO DECIMALS NO COMMAS NO SPACES OR ELSE
stored in a minimum of 32 bits of storage; at least 2 bytes - usually 4
unsigned int contains only positive values
a) short
only 2 bytes so it doesn’t get used too often
b) long - long int - %li
stores inter values up to (32 bits);
unsigned long can be used for a larger range with no negative
c) long long - long long int - %lli
too big like at least 64 bites
unsigned long long can be used for a larger range with no negative
Floating Number Type - float - %f
stores values containing decimal places
can be negative or positive
* you can omit digits before and after the decimal point but not both… (3. or .3, but not .) (apparently never tried)
* you can use %e to print it out in scientific notation (apparently never tried) and %g to tell print f to decide.
32 bits; 4 bytes
Extended Precision Type - double - %lf
can store roughly twice (64 bits) as many significant digits as float (32 bits); 8 bytes
can be negative or positive
* you can omit digits before and after the decimal point but not both… (3. or .3, but not .)
you can use %g to print it out in scientific notation (apparently never tried)
a) long double - long double - %Lf (floating point notation) or %Le(scientific)
stores doubles as at least 64 bits wide
Single Character Type - char - %c
stores a single character
formed by enclosing the character with a pair of single quotation marks so ‘a’, ‘;’ and ‘0’ are all valid characters constants
‘\n’, the newline character, is called an escape sequence and is apparently also a character constant even though its two characters?
1 byte
Boolean Data Type - _Bool
large enough to store the values 0 and 1
by convention, 0 indicates a false value and 1 indicated true
if you assign a value of 0, 0 is stored. Any nonzero value is stored as 1.
*<stdbool.h> defines values bool, true, and false
Summary Table:
Example Declaring and Outputting Each Data Type:
#include <stdio.h>
int main (void){
int integerVar = 100;
float floatingVar = 331.79;
double doubleVar = 8.44e+11;
char charVar = 'W';
_Bool boolVar = 0;
_Bool boolVar2 = 5;
printf("integerVar = %i\n", integerVar);
printf("floatingVar = %f\n", floatingVar);
printf("doubleVar = %lf\n", doubleVar);
printf("doubleVar = %g\n", doubleVar);
printf("charVar = %c\n", charVar);
printf("boolVar = %i\n", boolVar);
printf("boolVar2 = %i\n", boolVar2);
return 0;
}
Output:
integerVar = 100
floatingVar = 331.790009
doubleVar = 844000000000.000000
doubleVar = 8.44e+11
charVar = W
boolVar = 0
boolVar2 = 1
Basic Info
can store floats, ints, characters, pointers, etc.
must begin with a letter or underscore ‘_’
can be a combination of letters, underscores, or the digits 0-9
Invalid Variables
cannot contain invalid characters ie. sum&value
no embedded spaces ie. sum value
cannot start with number ie. 9sumvalue
cannot use a reserved word ie. int
Basic Info on Working With Arithmetic Expression
BEDMAS applies
binary arithmetic operators: operate on two values or terms
if you apply any of these operators with an int and a float, the output will be a float. If you use two integers, the output will be an integer.
if a float type value is assigned to an integer variable, they number will be truncated to only its integer portion. NOT ROUNDED. ie 123.5 becomes 123.
Binary Arithmetic Operators
*modulus only works with int types. you should use double fmod() in math.h otherwise.
Type Cast Operator
Casting values temporarily converts the value of the variable to another data type. It does not effect the value stored in the variable. Has higher precedence than all the arithmetic operators other than unary- and unary+.
* unary means on one operand so like x = -x.
Assignment Operators
Assigns after arithmetic operator has been applied.
*Im gonna assume we don’t need to know this. 🤞
includes 3 statements within corresponding fields
General Format:
for (initial expression; loop condition; loop_expression){
sum += n;
}
for (int a = 1, int j = 20; a <= 200; a++, j =- 10){
total+= a;
}
The first component - initial expression - sets the initial value of a variable.
The second component - loop condition - the condition, or conditions, that are necessary for the loop to continue.
The final component - loop expression - an expression that is evaluated each time after the body of the loop is executed.
Loop Variants: In all cases, you can include multiple expression in any of the fields but these should be separated by commas.
**keep in mind that == means equal to but = will assign a variable
You can print values using format specifiers with field width specifications. For example, %ni or %nlf will ensure that the value you print will take up n columns. If n = 2 and your integer takes up less than 2 colums, it will be displayed with a leading space.
**printf() + scanf() stuff. I’m not gonna bother. Remember when you use scanf(), ensure that your variable starts with &.
General Format:
while (expression){
program statements
}
while (n < 3){
print ("%i", n);
n++;
}
The while statement runs the inner program statements while the expression = TRUE. If the expression finally evaluates as FALSE, the loop is terminated.
For and While loops evaluates the condition before the loop is executed, do while loops ensure evaluate the condition after the loop. This ensures that the looping is executed at least once.
General Format:
do{
program statements
}
while (loop expression);
do {
printf("i", num);
n--;
}
while ( n >= 0);
You can use a break statement to leave a loop as soon as a certain condition occurs. The break statement causes the program to immediately exit from the loop it is executing, continuing to whatever statement follow the loop.
General Format:
break;
This statement causes the loop in which it is executed in to be continued. Any statement in the loop after the continue statement are automatically skipped and the next iteration of the loop is continued.
General Format
continue;
Statements inside the if statement only run if the expression is TRUE. Also, only runs once.
Statements inside the else if statement only run if the preceding if statement did NOT execute AND if the expression is TRUE.
Statements inside the else statement only run if the preceding if statements and else if statements did NOT execute.
General Format:
if (expression){
program statement
}
else if (expression){
program statement
}
else{
program statement
}
*Nested while, for, do, or if loops just means another while, for, do, or if loop inside the outside statement respectively.
*Prob won’t be tested on this but really easy stuff.
Essentially, the switch acts like an if statement. If the expression is true, each case acts as an if else statement and the default statement as an else statement.
*notes are from the screencast bc I think they’re better
Instead of writing all the code in the function main, you can call a function and instead, write the code within that function outside of main.
void printRow( int row );
int main( void ) {
int size;
int row;
printf( "Enter size of triangle: " );
scanf( "%d", &size );
row = 1;
while( row <= size ) {
printRow( row );
row++;
}
system( "PAUSE" );
return 0;
}
*/
void printRow( int row ) {
int star;
star = 1;
while( star <= row ) {
printf( "*" );
star++;
}
printf( "\n" );
}
You must first call a function before you run it. To call a function, include the type of data the function will be outputting, the name of the function, two parenthesis with the data type of the argument which will be inputted into the function, followed by a semicolon.
You must then run the function. To execute a function, write the function name, and semicolons which include the argument which will be
function’s name followed by two parentheses and a semicolon.