260 likes | 391 Views
B Smith: 1/28/2005: Discuss printing %, variable field width, catching large integers. B Smith: 1/28/2005 4:03 PM: Score 3. B Smith: Fa05: two lectures, some time spent on defining a byte and discussing computer representation of data. B Smith:
E N D
B Smith: 1/28/2005: Discuss printing %, variable field width, catching large integers B Smith: 1/28/2005 4:03 PM: Score 3. B Smith: Fa05: two lectures, some time spent on defining a byte and discussing computer representation of data B Smith: This lecture presents a great opportunity to introduce and work with a debugger to inspect variables! But is this too much, too soon? Too complex? Isn’t an introduction to DOS and cmd.exe required? Math 130Introduction to Computing“” C Fundamentals – 3 “ (assignment, increment, scanf()) Lecture # 04 B Smith: 1/31/2005 11:31 AM. Score 3: Not exciting or very well organized but passable.
B Smith: Cover assignment operators better Overview • Assignment (§3.1) • Assignment operators (+=, -=, *=,/=, %=) • Accumulators • Addresses (§3.2) • Pointers • scanf() (§3.3 and 3.4) • Symbolic Constants (§3.5)
B Smith: or more multiple declarations and assignments The Assignment Operator “=“ We have looked at declaring variables, but to assign a value to a variable you will use the assignment operator, = • A typical assignment would be stated as • velocity = 30.0; • velocity = 30; • velocity = “q”; • Which of the above you use will depend on how velocity has been declared in your program • You can also do declaration and assignment concurrently: • floatvelocity = 30.0; • intvelocity = 30;
B Smith: Stopped here on 1/27/06: Student had “philosophical” questions on the meanign of the line: int x; or double y; She believed that they were doing something; that they were commands Other operators • Increment and decrement operators: • x++ is equivalent to x = x + 1; • y-- is equivalent to y = y - 1; • Compound (or abbreviated) assignment operators: (+=, -=, /=, %=, *=) • x += 3; is equivalent to x = x + 3; • sum -= y; is equivalent to sum = sum - y; • product *= z; is equivalent to product = product * z; • d /= 4.5; is equivalent to d = d / 4.5 ; • r %= 2; is equivalent to r = r % 2 ;
Accumulators • General format for accumulators: • variable = variable + newValue Example: int x; x = 0; /* initialize x to 0*/ x = x+4; /* add 4 to the initial value*/ x = x+96; /* add 96 to new value */ x = x+10; /* add 10 to the running total */ printf(“the final value for x is %d”, x);
Accumulator: What will be the output? #include <stdio.h> int main() { int sum; sum = 0; printf("\nThe value of sum is initially set to %d.", sum); sum = sum + 2; printf("\n sum is now %d.", sum); sum = sum + 3; printf("\n sum is now %d.", sum); sum = sum + 5; printf("\n sum is now %d.", sum); sum = sum + 10; printf("\n The final sum is %d.", sum); printf("\n"); return 0; }
Incremental Counting (summation)Accumulation while incrementing by a fixed number #include <stdio.h> int main() { int count; count = 0; printf("\nThe value of count is initially set to %d.",count); count = count + 2; printf("\n count is now %d.", count); count = count + 2; printf("\n count is now %d.", count); count = count + 2; printf("\n count is now %d.", count); count = count + 2; printf("\n The final count is %d.", count); printf("\n"); return 0; }
More Incremental CountingIt's appropriate to use “++” when incrementing by 1 #include <stdio.h> int main() { int count; count = 0; printf("\nThe value of count is initially set to %d.", count); count = count + 1; /* can be simplified to ++count */ printf("\n count is now %d.", count); count = count + 1; printf("\n count is now %d.", count); count = count + 1; printf("\n count is now %d.", count); count = count + 1; printf("\n The final count is %d.", count); printf("\n"); return 0; }
More Incremental Countingsimplifying with ++count #include <stdio.h> int main() { int count; count = 0; printf("\nThe value of count is initially set to %d.", count); ++count;/* substitute ++count for count = count + 1 */ printf("\n count is now %d.", count); ++count; printf("\n count is now %d.", count); ++count; printf("\n count is now %d.", count); ++count; printf("\n The final count is %d.", count); return 0; }
More Incremental Counting-after further simplification... #include <stdio.h> int main() { int count; count = 0; printf("\n count value initially set to %d.", count); printf("\n count is now %d.", ++count); /* now ++count*/ printf("\n count is now %d.", ++count); printf("\n count is now %d.", ++count); printf("\n The final count is %d.", ++count); return 0; }
Incremental Counting: n++ vs ++n The difference between count++ and ++count? • The postfix increment operator • x = count++; • The prefix increment operator • x = ++count;
It depends on the problem: If n = 0, and k = 1, then what is printed? B Smith: Stopped here on Fri, 9/2/05. Quiz took about 15 minutes B Smith: 1/30/06: Stopped here and did live examples. Should I post or pre increment? printf(“%d”,++n;) /* This yields the same effect as k = ++n; */ n = n + 1; printf(“%d”,n); printf(“%d”,n++;) /* This yields the same effect as k = n++; */ printf(“%d”,n); n = n + 1;
Addresses • Associated with every variable is • a value ( e.g. 231.23 ) • a type ( e.g. float ) • a location, an address ( e.g. 0x0012FF7C )
Addresses include <stdio.h> int main() { int num; num = 22; printf("The value stored in num is %d.",num); printf("\nThe computer uses %d bytes to store this value", sizeof(num)); return 0; } The output is: The value stored in num is 22. The computer uses 4 bytes to store this value
1 2 3 4 bytes below 00 12 FF 7C (in decimal this is: 1,245,052) The output is: num = 22 The address of num is 0012FF7C. Press any key to continue B Smith: Stopped here on Mon 1/30/06 Addresses #include <stdio.h> int main() { int num; num = 22; printf(“num = %d \n”, num); printf("The address of num is %p.\n", &num); return 0; }
Addresses (cont'd) • Generally you will not need to actually display addresses • You will use the address for referencing data • The address operator, &, is used for referencing data addresses. • To access the address of the integer num you simply use the operator in front of the variable: • &num can be read ‘the address of variable num’ • Note that the author's book uses 2 bytes for an integer, hence an address such as FFE0 vs my computer is 0012FF7C (see pg 92, Bronson)
B Smith: Fa04:This was used as an opportunity to further discuss pointers, int*, int, foat, float*, etc., vs *numPtr (dereferencing) Addresses (cont'd) • We can assign the address of variable num to another variable: • For example: • myAddress= # • Pointers • The variable myAddress would be considered a “pointer”, or a “pointer variable” • A pointer is a type of variable that holds the address of another variable
scanf() • scanf() is used for taking information from the user • typically you will want to save this information and use it possibly in a calculation • the scanf() syntax is such that it does not read the data directly into a variable, but rather an address. • For example, the following would require you to rewrite the program every time you wanted to change num1 or num2: float num1,num2,product; num1 = 300.0; num2 = .05; product = num1 * num2; printf("%f times %f is %f", num1, num2, product);
B Smith: Less emphasis on scanf means more time on Computer Science! scanf() • The alternative to “hard coding” your variables is to prompt for user input float num1, num2, product; printf("Please type in a number: "); scanf("%f",&num1); printf("Please type in another number: "); scanf("%f",&num2); product = num1 * num2; printf("%f times %f is %f",num1, num2, product);
scanf() • The control string represents a picture of the anticipated input float a; int b; printf("Please type in 2 numbers: "); scanf("%d %f",&a, &b); printf("%d times %f is %f",a,b,a*b);
scanf() • It’s helpful to think of scanf() as performing a simple matching operation between the control string and the input float a; int m, d, y; printf("Please enter date as mm/dd/yy: "); scanf("%d/%d/%d", &m, &d, &y); printf("The month entered was %d \n", m );
B Smith: Make this part of a broader discussion on macros. Point them to the manual Symbolic Constants • In the statement: circum = 2 * 3.14159265 * radius; • what are the variables? what are the constants? • Since the values of the constants will be what is literally typed, they are called literals, or literal data • Imagine a program requiring high precision and you had to type in 3.1415926535897932384626433832795 for every reference to (=PI)! • You could get around this by using a symbolic name for the value of : • #define PI 3.14159265 • The statement becomes:circum = 2 * PI * radius
B Smith: 2/2/2005 12:07 PM: Somewhat redundant since this should be part of the previous notes’ discussion Symbolic Constants – #define • A #define is usually placed at the top of a file, before functions • #define and #include statements can be intermixed • Just as with #include, the # sign is a flag to the C preprocessor (prior to compilation) • The #define statement tells the preprocessor to replace each occurrence of the defined symbol with the associated information (number, data, etc.)
B Smith: Elaborate more on format specifiers for strings. See notes at bottom. Also, good opportunity to elaborate on using 5.3f or 13.5f, etc. Use of #define #defineSALESTAX 0.0775 #defineSTART_CURLY_BRACE { #defineSTOP_CURLY_BRACE } #include <stdio.h> int main() START_CURLY_BRACE float amount, taxes, total; printf("\nEnter in the amount purchased: "); scanf("%f",&amount); taxes = SALESTAX * amount; total = amount + taxes; printf("The sales tax is $%4.2f",taxes); printf("\nThe total bill is $%5.2f",total); return 0; • STOP_CURLY_BRACE
Summary • Assignment (§3.1) • Accumulators • Addresses (§3.2) • Pointers • scanf() (§3.3 and 3.4) • Symbolic Constants (§3.5)