Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.

Slides:



Advertisements
Similar presentations
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Advertisements

Computer Science 1620 Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Introduction to Computer Algorithmics and Programming Ceng 113 Variables and Operators in C.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Conditional Statement
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CSE 1320 Basics Dr. Sajib Datta
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
ECE Application Programming
1-d Arrays.
CSE 220 – C Programming Loops.
REPETITION CONTROL STRUCTURE
Functions and Pointers
EKT120 COMPUTER PROGRAMMING
ECE Application Programming
Operators And Expressions
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
CSE 1320 Search, Sort and Strings
CSE1320 Loop Dr. Sajib Datta
Review of C… The basics of C scanf/printf if/elseif statements
Functions Dr. Sajib Datta
Week 4 – Repetition Structures / Loops
Java Programming: From Problem Analysis to Program Design, 4e
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Functions and Pointers
Looping.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, For loop While loop Do while loop
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Incremental operators
Your questions from last session
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
Arrays.
ECE 103 Engineering Programming Chapter 18 Iteration
DATA TYPES There are four basic data types associated with variables:
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Dr. Sajib Datta

 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

 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));

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

 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.

 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.

◦ 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

◦ Input: 17 ◦ Output: ◦ #include ◦ 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; ◦ }

◦ Print out a triangle. ◦ * ◦ ** ◦ *** ◦ ****

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

◦ 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.

 #include  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...

 #include  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 Press any key to continue...

 #include  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;  }

 *  **  ***  ****

 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;

 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!!!!

 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.

 #include  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");  for (j = 0; j < 3; j++)  {  printf(" %c ", charr[j]);  }  return 0;  }

 int n=19;  int iarr[n];  or  #define ARRAY_SIZE 19 // have it after #include  int iarr[ARRAY_SIZE];

 Given  int array1D[5] = { 32,44,33,12,65};  Output:  The min value is 12.  The max value is 65.

 #include  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;  }

 Original sentence: I am at UTA  The reverse sentence: ATU ta ma I

 #include  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--)  printf("%c", arraych[i]);  printf("\n");  return 0;  }