159101 Condensed Lecture notes PDF

Title 159101 Condensed Lecture notes
Author Laressa Hood
Course Technical Programming
Institution Massey University
Pages 24
File Size 529.8 KB
File Type PDF
Total Downloads 543
Total Views 689

Summary

159.Definitions:- Algorithm• A series of instructions• The order is important• Yo u n e e d a l a n g u a g e• E. Baking- Computer Program• An algorithm• In a programming language (c)• In a form ready to run on a computer- Syntax• Rules of the language• Layout of your program• Grammar - is a collect...


Description

159101 Programming

159.101 Definitions:

- Algorithm • A series of instructions • The order is important • You need a language • E.g. Baking - Computer Program • An algorithm • In a programming language (c) • In a form ready to run on a computer - Syntax • Rules of the language • Layout of your program • Grammar - is a collection of rules of the language - Semantics • Tells you the meaning of a piece of language

If else: if (result >= 75) { printf (“first class pass”); } else if (result >= 67) { printf (“upper second class pass”); } else if (result >= 60) { printf (“lower second class pass”); } else if (result >= 50) { printf (“third class pass”); } else { printf (“fail”); 1

159101 Programming }

• Better: - goes faster - uses less memory Input scanf("%d", &value); //

*& = load a value into, load a variable

Field width: %4d = make the number 4 spaces! %04d = make the number 4 spaces, and fill the spaces with 0’s! %1d = it will still write 37. %-4 = spaces being the number 37_ _ %4.2f - 4 = total number of spaces - .2 number of after the dot. %1.2f - ignores the 1, .2 is still carried out %7.3f _ _7.800 %1.0f

8

Library #include #include #include time.h stdlib.h

2

159101 Programming

Functions in stdlib.h

- exit(15); kills/stops the program - abs; calculates the absolute value of an integer (always positive) • distance = abs (b-a); - system; enables Dos (windows) commands from inside your program. • system(“cls”); - rand; produces a random number

Loops counter = 0; while (counter < 5) { printf("Hello"); counter++; } }

while (true) { /* infinite loops*/ printf("Enter a year (-1 to stop):\n"); scanf("%d", &year); if (year == -1) { break; }

Checking valid data input: while (true) { printf("Enter mark for test 1:\n"); scanf("%d", &mark1); if ((mark1 >=0)&&(mark1 A, m —> M if ((letter >= 'a')&&(letter december but we can use arrays: float rain[12]; 0

1

2

3

4

5

6

7

8

9

10

for (i = 0; i large) • descending order (large —> small)

48 13 13 13

Result: 9, 13, 27, 48, 62 a=3;

13 48 27 27

Data Pass 27 62 27 62 48 62 48 9

9 9 9 62

b=5; Maximum = the max number of people this room can hold. Total = the total number of people in the room CURRENTLY.

temp = a; a = b; b = temp;

for (pass = 0; pass < num; pass++) { for (i=0; i < num-1; i++) { if (array[i] > array[i+1]) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; } }

Searching: 1. success (found) 2. failure (not found)

//section 3: Ask for an ID and search for that ID printf("Enter ID numbers (-1 to stop)\n"); while (true) { printf("Enter a student ID: "); scanf("%d", &searchid); if (searchid == -1) { break; } 6

159101 Programming select = -1; for (i=0;i id[i]) { i++; } correct = i;

if (newid > id[num-1]) {

// special case

correct = num; }

// section 4 : shuffle up the array for (i = num; i > correct; i--) { id[i] = id[i-1]; }

// section 5 : insert the new ID number id[correct] = newid; num++;

8

159101 Programming // section 6 : display the array including the new ID number printf("\nHere is the full list of ID numbers\n"); for (i = 0; i < num; i++) { printf("%d

", id[i]);

} printf("\n"); }

Deleting an item from a sorted array: Num = 5 Delete 643 num = 4 #include int num, removeid, correct, i, id[100]; int main(int argc, char *argv[]) { printf("Enter ID numbers: "); num = 5; for (i=0;i string concatenate ‘putting two things together’ • “how ” and “are you?” = “how are you?” • result is copied back into the first string. • a is a string variable • b can be a string variable OR a literal string - something = strlen(a) —> Length # of char’s • doesn’t include \0 • e.g. cat = length = 3 • e.g. “r s” = length = 3 —> spaces included • a can be a string variable or a literal string - result = strcmp(a, b) • a is a string variable OR a literal string • b can be a string variable OR a literal string

Reverse a string: Input: “My cat”

Output: “tac ym”

int main() { printf("Type in a sentence: "); gets(sentence);

16

159101 Programming len = strlen(sentence); // len variable gets loaded with the string length of my sentence j=0; for (i=len-1; i>=0; i--) { reverse[j] = sentence[i]; j++; } reverse[j] = '\0'; //DONT FORGET! printf("The reverse string is %s", reverse); }

String parameters: Char word[25] F

i 0

s 1

h 2

\0 3

4 …

locations in memory, have an ‘address’

Sting parameters use address of the first element in the string. A. Do not have to pass the whole string B. Can change the string back in main because the location is known

void stringcopy(char *first, char *second) { int i; for (i=0;i...


Similar Free PDFs