Week #10 Kyle Dewey. Overview Meeting times Typos Project #3 tidbits Exam Review.

Slides:



Advertisements
Similar presentations
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Advertisements

Week 5 Part I Kyle Dewey. Overview Exam will be back Thursday New office hour More on functions File I/O Project #2.
Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.
C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));
Lectures 10 & 11.
Computer Programming w/ Eng. Applications
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Sort the given string, without using string handling functions.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Week 8 Kyle Dewey. Overview Exam #2 Multidimensional arrays Command line arguments void* Dynamic memory allocation Project #3 will be released tonight.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
Kernighan/Ritchie: Kelley/Pohl:
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
EXERCISE IN CLASS CHAPTER 3. PART 1 IDENTIFIER SCENARIO 1 o record1 o 1record o file_3 o return o $tax o Name o name and address o name-and-address o.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
Pointers and Arrays C and Data Structures Baojian Hua
C Slides and captured lecture (video and sound) are available at:
Week 9 Part 2 Kyle Dewey. Overview Announcement More with structs and memory Assertions Exam #2 Course review.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
C Programming - Lecture 6 This lecture we will learn: –Error checking in C –What is a ‘wrappered function’? –What is a clean interface? –How to earn your.
Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Principles of Imperative Computation Lecture 1 January 15 th, 2012.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
Week 5 Part 2 Kyle Dewey. Overview Scope Lifetime Testing Exam #1 overview.
Computer Organization and Design Pointers, Arrays and Strings in C
Week 3 Part 2 Kyle Dewey.
CSE 220 – C Programming Pointers.
C Basics.
CS 2308 Exam I Review.
Programming in C Pointer Basics.
Programming in C Pointer Basics.
design OO words Debug Variables Data types
Programming in C Pointer Basics.
EE 312 Exam I Review.
EE 312 Exam I Review.
Presentation transcript:

Week #10 Kyle Dewey

Overview Meeting times Typos Project #3 tidbits Exam Review

Meeting Times Earliest I can possibly meet with anyone is Wednesday after lab I will be in my office Thursday (Phelps 1410) from 10 AM - 2 PM I can meet from PM Friday...and that’s it

Typos Week 9 part 1 Exam review “struct named bar” Syntax error with pointer to struct Look online for latest versions

Project #3

Planning Hours of coding can save minutes of planning Longer functions = more code in one place = more that needs to be read to understand a piece of code = more headaches More variables in one place = more possible interactions = more headaches

Simplicity Simpler designs are usually more reliable Simplicity is often initially more difficult to achieve than complexity But it pays off in the long run

Functions int main() { int input; int n = 0; int s = 0; while ( scanf( “%i”, &input ) == 1 ) { s += input; n++; } printf( “%i\n”, s / n ); return 0; }

Functions int averageFromInput() { int input; int n = 0; int s = 0; while ( scanf( “%i”, &input ) == 1 ) { s += input; n++; } return s; } int main() { printf( “%i\n”, averageFromInput() ); return 0; }

Functions int main() { int arr[] = { 1, 2 }; int temp = arr[ 0 ]; arr[ 0 ] = arr[ 1 ]; arr[ 1 ] = temp; temp = arr[ 1 ]; arr[ 1 ] = arr[ 0 ]; arr[ 0 ] = temp; }

Functions void swap( int* arr, int pos1, int pos2 ) { int temp = arr[ pos1 ]; arr[ pos1 ] = arr[ pos2 ]; arr[ pos2 ] = temp; } int main() { int arr[] = { 1, 2 }; swap( arr, 0, 1 ); swap( arr, 1, 0 ); }

...or don’t... We aren’t grading you on your code structure...but more complexity = more room for bugs Refactoring: rewriting code to have the same functionality but be simpler

Common Pitfalls

“What should this look like?” There is too much possible variation in this project to answer this precisely I can look at your code to try to understand your approach Different approaches will vary WIDELY in their implementation

Reusing Memory char* dictionary[ MAX_NUM_WORDS ]; void readWordsFromTerminal() { int x = 0; char input[ INPUT_SIZE ]; while( scanf( “%s”,input ) == 1 ) { dictionary[ x ] = input; x++; }

First Word foobar\0 Input: (at 0x100) 0x100?? Dictionary: scanf( “%s”,input ); dictionary[ 0 ] = input;

Second Word baz\0ar Input: (at 0x100) 0x100 ? Dictionary: scanf( “%s”,input ); dictionary[ 1 ] = input;

Third Word moot\0r Input: (at 0x100) 0x100 Dictionary: scanf( “%s”,input ); dictionary[ 2 ] = input;

Function Returns ??????? Input: (at 0x100) 0x100 Dictionary: {... char input[ INPUT_SIZE ];... } (no longer alive)

Comparing Strings char input[ 10 ]; scanf( “%s”, input ); // inputs “foo” input == “exit” // true or false?

Comparing Strings char input[ 10 ]; scanf( “%s”, input ); // inputs “exit” input == “exit” // true or false?

What’s Happening ?????????? input: (at 0x100) exit\0 exit: (at 0x200) Type: char[]

What’s Happening ?????????? input: (at 0x100) exit\0 exit: (at 0x200) Type: char[] input == “exit”

What’s Happening ?????????? input: (at 0x100) exit\0 exit: (at 0x200) Type: char[] input == “exit” 0x100 == 0x200?

What’s Happening The addresses themselves are being compared The content of the arrays is completely ignored

Comparing Strings The correct way: use strcmp If strcmp returns 0, it means the string contents are the same

Comparing Strings char input[ 10 ]; scanf( “%s”, input ); // inputs “foo” strcmp( input, “exit” ); // 0?

Comparing Strings char input[ 10 ]; scanf( “%s”, input ); // inputs “exit” strcmp( input, “exit” ); // 0?

Using realloc You will almost certainly need to use realloc Look at stddev_class_version.c and stddev_my_version.c for more information (specifically the readNumbers function)

Strings With Constant Max Length You can use a declaration like this: char dictionary[ 100 ][ 100 ]; scanf( “%s”, dictionary[ 0 ] );//word 1 scanf( “%s”, dictionary[ 1 ] );//word 2... Wastes memory, but that’s ok for this assignment

Exam #3 Review