Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
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.
Strings.
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.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Dr. Sajib Datta  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CS 201 String Debzani Deb.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
Week 7 – String. Outline Passing Array to Function Print the Array How Arrays are passed in a function call Introduction to Strings String Type Character.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CHAPTER 8 CHARACTER AND STRINGS
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
CS 108 Computing Fundamentals March 26, Class Notes Last day to withdraw from a class is Monday, April 6 Next week I sent s to a small number.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
 Integers and Characters  Character Strings  Input and Output.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Strings, Pointers and Tools
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Characters and Strings
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Dr. Sajib Datta Feb 14,  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Introduction to Programming Using C Arrays. 2 Contents Arrays Subscripting.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Strings CSCI 112: Programming in C.
(Numerical Arrays of Multiple Dimensions)
INC 161 , CPE 100 Computer Programming
CSE 1320 Search, Sort and Strings
CSE1320 Loop Dr. Sajib Datta
Functions Dr. Sajib Datta
Arrays in C.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
Exercise Arrays.
Presentation transcript:

Dr. Sajib Datta

 Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These techniques vary in performance both with runtime and usage of extra memory

 Given a sequence of numbers, it compares adjacent numbers and swap them if necessary  Repeats the above procedure until all numbers are in right place

Pick 4, compare with 10

Pick 10, compare with 9 Need to swap

Pick 10, compare with 6 Need to swap

Pick 10, compare with -1

Notice, that 10 is at the right position Now, repeat from the beginning

Compare 1 st element with 2 nd element

Compare 2nd element with 3rd element Swap needed

Compare 3 rd element with 4 th element Swap needed

Now, do we need the compare 4 th with the 5 th ? We already know 5 th element is the largest. This implies, what we have as the 4 th element, it must be the second largest, and is already in the correct place.

Now, repeat from the beginning again. But remember, we only need to go up to 3 rd this time.

 1 st iteration places the largest number in the correct place  2 nd iteration places the second largest number in the correct place  …..  ….  i-th iteration places the i-th largest number in the correct place

#include  #define ARR_SIZE 8  int main(void)  {  int intarr[ARR_SIZE] = {23, 4, 12, 8, 22, 1, 54, 9}, i, j, tmp;   for(i = 0; i<ARR_SIZE-1; i++)  {  for(j = 0; j<ARR_SIZE-i-1; j++)  { if(intarr[j]>intarr[j+1])  {  tmp = intarr[j];  intarr[j] = intarr[j+1];  intarr[j+1] = tmp;  } }  }  printf("The array sorted by Bubble Sort: ");  for(i = 0; i< ARR_SIZE; i++)  printf("%d ", intarr[i]);  return 0;  }

 A string is a series of one or more characters, terminated by a null character.  ◦Example  ◦“I am at UTA”  The double quotation marks are not part of the string.  They just inform the compiler they enclose a string,  just as single quotation marks identify a character.

 C does not have a string type.  Characters in a string are stored in adjacent memory cells, one character per cell, and an array consists of adjacent memory locations, so placing a string in an array is quite natural.  We use an array of chars for storing a string.  We can declare this just like we did with other types.  Example  char letters[12] = “I am at UTA”; IamatUTA\0

 \0 is the null character which is used by C for marking the end of a string  The null character is not the digit zero; it is the nonprinting character.  Its ASCII code value is 0.  The presence of the null character means the array must have at least one more cell than the number of the characters to be stored

 Even though we define a char array much larger than a string, the null character will tell the compiler where is the end of the string. This is useful when we call printf( ).  char letters[15] = “I am at UTA”; IamatUTA\0

 gets() – read a line from input  Reads characters from stdin and stores them as a string into str until a newline character ('\n').  The ending newline character ('\n') is not included in the string.  A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.  Notice that gets() does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid overflows.

 puts(the name of your char array) print out a string.  #include  int main(void)  {  char myString[41];  //printf("Please intput a string with characters <= 40:\n");  puts("Please intput a string with characters <= 40:\n");  gets(myString);  puts(myString);  return 0;  }  Note that all input will be regarded as one string.  However, scanf(“%s”, myString) will only read the characters until the first space as one string.

 Scan a string and print it out  #include  int main(void)  {  char name[40];  printf("What's your name?\n");  scanf("%s", name);  printf("Hello, %s.\n", name);  return 0;  }   %s is the format specifier for a string.  The name of a char array represents an address in computer memory, therefore, we don‟t need “&” when we call scanf function.

 If your input is ◦ Sajib Datta  Just calling scanf() once will only store Sajib in the char array as a string.  The remaining of the input will be ignored.  In this case, the space is used to separate strings in a sentence.

 Scan a string and print it out  #include  int main(void)  {  char name[40];  printf("What's your name?\n");  gets(name);  printf("Hello, %s.\n", name);  return 0;  }   If you input “Sajib Datta”, it will print “Hello Sajib Datta.”

1. char a[2] = {‘x’,’y’}; printf(“%s”, a); // puts(a); 2. char a[3] = {‘x’,’y’}; printf(“%s”,a); //puts(a); 3. char a[2] = “xy”; printf(“%s”,a); //puts(a); 4. char a[3] = “xy”; printf(“%s”,a); //puts(a); 5. char a[5] = {‘’x’,’y’,’z’,’\0’,’d’}; printf(“%s”,a); //puts(a);

6. char a[5] = “xyz\0d”; printf(“%s”,a); //puts(a); 7. char a[5] = “xy\\0”; printf(“%s”,a); //puts(a);