Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE1320 Loop Dr. Sajib Datta CSE@UTA.

Similar presentations


Presentation on theme: "CSE1320 Loop Dr. Sajib Datta CSE@UTA."— Presentation transcript:

1 CSE1320 Loop Dr. Sajib Datta

2 char data type char type technically is an integer type
Computer uses numeric codes to represent characters, and store characters as integers The mostly commonly used code in the U.S. is the ASCII code

3 char data type A char variable takes 8-bit unit of memory (1 byte), which can be verified by sizeof() C character constant: a single letter contained between single quotes Example: char mych = 'a'; printf("%d", sizeof(char));

4

5 Compare char values char ch; scanf(“%c”, &ch);
printf(“%c”, ch); // will print the character printf(“%d”, ch); //will print?

6 Compare char values

7 Input/output Char char letter1 = 'A'; char letter2 = 65;
printf("print the ASCII for 'A' - %d", letter1);// 65 will be printed printf("print the char value for ‘A' - %c", letter2); // A will be printed scanf("%c", &letter);// must read a character, even the input is a digit, it will be regarded as a character scanf("%d", &letter);// fail – type must match Not good programming to mix integer and char value, because it needs remembering ASCII for characters.

8 Non-printing characters
Characters which can not be printed directly Rather, some represent some actions such as backspacing or going to the next line or making the terminal bell ring.

9 Non-printing characters

10 while loop The basic form of the while loop is
while(test_condition) do_something; As long as test is true, the loop will repeat. test_condition should be an expression. If it is nonzero, it’s a true condition, and do something. If it is zero, the while statement will be skipped. Give the test_condition a chance to change in do_something, otherise, the loop will run for ever In general, (1) define a variable outside while loop and (2) use it to compose the test_condition, and then (3) change the variable in do_something

11 Example Input: 17 Output: 0 2 4 6 8 10 12 14 16
#include <stdio.h> int main(void) { int upbound; int i = 0; scanf("%d",&upbound); while(i <= upbound) if (i % 2 == 0) printf("%d ", i); i++; } printf("\n"); return 0;

12 Example Print out a triangle. * ** *** **** How do you solve this?

13 #include <stdio.h> int main(void) { int j, i = 1; while (i <= 4) j = 1; while (j <= i) printf("*"); j ++; } printf("\n"); i ++; return 0;

14 for basics Gathering three actions (initializing, testing, and updating) into one place The basic format: for (expression1; expression2; expression) do_something

15 for basics Notes: The parentheses following the keyword for contain three expressions separated by two semicolons. Expression1 is initialization. It’s done just once, when the for loop starts. Expression2 is the test condition, and will be evaluated before each potential execution of a loop. When it’s false, the loop will be terminated. Expression3, the change or update, is evaluated at the end of each loop.

16 #include<stdio.h> int main(void) { int number = 4; int count;
for (count = 1; count <= number; count++) printf("The index of the loop is %d.\n", count); return 0; } Output: The index of the loop is 1. The index of the loop is 2. The index of the loop is 3. The index of the loop is 4. Press any key to continue . . .

17 Example print out n square
#include<stdio.h> int main(void) { int num; printf(" n n square\n"); for (num = 1; num <= 4; num++) printf(" %d %d\n", num, num*num); return 0; } Output: n n square 1 1 2 4 3 9 4 16 Press any key to continue . . .

18 Example print out decreasing integers
#include<stdio.h> int main(void) { int num, start; scanf("%d", &start); printf("Print the five integers, starting from the input with a decrement of 3.\n"); for (num = 0; num <= 4; num++) printf("%d\n", start-num*3); return 0; }

19 Print out a triangle- with for loop
* ** *** ****

20 Arrays

21 1-D Array Example of declaring and initializing an array.
double someData[3]; /* declare the array someData that will hold 3 doubles variables*/ /* later we can provide the specific array values. notice how the array index begins at 0 */ someData[0] = 3.5; someData[1] = 4.0; someData[2] = 9.34;

22 1-D Array We may also declare and initialize the array at the same time, so we don’t need to include the number of array elements in the square brackets. double myData[] = {3.5, 4.0, 9.34}; Never access a variable in an array beyond the index bound!!!!

23 Initialization If we initialize only some of the array values when the array is declared, the initialized values will be at the beginning of the array. The remaining values will be initialized to zero for an array of int, “space” for an array of char.

24 Example of 1-D Array #include <stdio.h> int main(void) {
int j, intarr[3] = {2,3}; char charr[3] = {'a', 'b'}; for (j = 0; j < 3; j++) printf(" %d ", intarr[j]); } printf("\n"); printf(" %c ", charr[j]); return 0;

25 Find Min and Max from an array
Given int array1D[5] = { 32,44,33,12,65}; Output: The min value is 12. The max value is 65.

26 #include <stdio.h> int main(void) { int i, min, max, array1D[5] = {32,44,33,12,65}; min = max = array1D[0]; for (i = 1; i < 5; i++) if (array1D[i] > max) max = array1D[i]; if (array1D[i] < min) min = array1D[i]; } printf("The min value is %d.\n", min); printf("The max value is %d.\n", max); return 0;

27 Print an array in reverse
Original sentence: I am at UTA The reverse sentence: ATU ta ma I

28 #include <stdio.h> int main(void) { int i; char arraych[11] = {'I', ' ', 'a', 'm', ' ', 'a', 't', ' ', 'U', 'T', 'A'}; printf("Original sentence: "); for (i = 0; i <= 10; i++) printf("%c", arraych[i]); printf("\n"); printf("The reverse sentence: "); for (i = 10; i >=0; i--) return 0; }


Download ppt "CSE1320 Loop Dr. Sajib Datta CSE@UTA."

Similar presentations


Ads by Google