Download presentation
Presentation is loading. Please wait.
1
Beginning C for Engineers
Lecture 6 – Wrapping it Up Section 2: 10/5/05 Section 4: 10/6/05
2
Homework Deadlines have been changed: Final Exam next class!
Final Project and Optional Homework due one week after final class (if you still want to hand them in by next class that’s fine too) Section 2: 10/19/05 Section 4: 10/20/05 Final Exam next class!
3
Preincrement vs. Postincrement
Recall that there are 2 versions of the ++ operator. When used as a single statement, the prefix and postfix forms of the ++ operator work exactly the same (i.e. they add one to the variable). BUT, when used in expressions they act differently. int count = 0; ++count; /* count = 1 */ int count = 0; count++; /* count = 1 */ int i = 10; int j = i++ - 4; // j is 6 // i is 11 int i = 10; int j = ++i - 4; // j is 7 // i is 11
4
Preincrement vs. Postincrement (continued)
The postincrement version says “Use it then increment”, where as the preincrement version says “Increment then use it” in the expression. The same applies to the -- operator. Examples: int j = 10, k = 10, s = 10, t = 10; printf(“%d\n”, j++); // prints 10. j = 11 after the statement printf(“%d\n”, ++k); // prints 11. k = 11 after the statement func(--s, t++); // calls the function with s = 9, t = 10 // after the call, s is 9 and t is 11
5
Recall that Character strings are represented as char Arrays
char name[10] = “Bettina”; // allocates 10 chars ‘B’ ‘e’ ‘t’ ‘i’ ‘n’ ‘a’ ‘\0’ 1 2 3 4 5 6 7 8 9 A character string always ends with a ‘\0’ (the null character, a byte with all 0’s) Therefore in this example you can only store 9 chars + ‘\0’ in the 10 character array.
6
More Operations on Character Strings #include <string.h>
char name[10]; Assignment - Use strcpy(dest, source); strcpy(name, “Bettina”); /*Note: Can’t use name = “Bettina”; */ Comparing - Use strcmp(name, “Bettina”); /* Can’t use == */ - strcmp returns 0 if name == “Bettina” - something less than 0 if name is less than “Bettina”, and something greater than 0 if name is greater than “Bettina”. (alphabetical order & cap letters are greater than lower case) Concatenating - Use strcat(str1, str2); - combines two strings so new string is str1 followed by str2 strcat(name, “ Schimanski”); /* name is now “Bettina Schimanski” */
7
Reading and Printing Character Strings
Use “%s” when using printf and scanf. scanf behaves differently - don’t need to use an ampersand (i.e. &) when reading in a character string (has to do with the memory location already specified by base address of array) Warning: Be sure your array is big enough! char name[20]; printf(“Enter your name: “); scanf(“%s”, name); /* NO & NEEDED */ printf(“Nice to meet you %s\n”, name);
8
Common Array Mistakes Remember:
To check for the size of the array so that you don’t go out of bounds If your array is higher than one dimension to pass in the size of each additional index except for the first Example: int somefunction ( int largearray[ ][100]) { code here…. } Not to pass in the [ ] with an array in a function call Arrays are passed by reference, not passed by value Cannot assign one array to another. Need to copy each index of one array into the other or use strcpy
9
Looping through a 2-D array: Use Nested loops
The outer loop should be the rows, the inner loop the columns const int NUM_STATES = 50 ; const int NUM_MONTHS = 12 ; … int stateHighs [ NUM_STATES ] [ NUM_MONTHS ] ; for (rows = 0; rows < NUM_STATES; rows++) for (cols = 0; cols < NUM_MONTHS; cols++) scanf (“%d”, &stateHighs[rows][cols]);
10
Initializing 1D and 2D Arrays
1 dimensional array: int myarray[4] = { 1, 2, 3, 4 }; 2 dimensional array: int myarray[2][2] = { { 1, 2 }, { 3, 4 } }; Remember this only works for when you first create the array!
11
Final Final is next week in class
Topics for the final have been posted on the web page Consists of just a written part (no computer component) Be sure to go over all Lecture slides, quizzes, labs, and homeworks and make sure you understand all that was covered in them Study Session: Monday 10/10/05 8pm in the DCC Come with Questions I will still have office hours this Monday 10/10/05 For help on topics for the Final, Final Project, or optional homework
12
Questions? Clarification of any topics we’ve covered in class?
Need some pointers on specific parts of the project? The rest of class may be used to work with your group or individually on the project. You can ask for help with compilation errors and/or get started on coding the rest of the functions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.