C Programming Strings.

Slides:



Advertisements
Similar presentations
Structures Often we want to be able to manipulate logical entities as a whole For example, complex numbers, dates, student records, etc Each of these must.
Advertisements

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.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Exercise 11 Dynamic allocation, structures, linked lists.
1 Strings ( מחרוזות ). 2 Agenda Definition and initialization Termination Input / Output String library.
Exercise 11 Dynamic allocation and structures. Dynamic Allocation Array variables have fixed size, used to store a fixed and known amount of variables.
1 Structures, Dynamic Memory Allocation. 2 Agenda Structures Definition & usage Pointers to structures Arrays and pointers in structures Dynamic Memory.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
Programming Structures and Dynamic Allocation. Dynamic Memory Allocation The memory requirement of our program is not always known in advance  Arrays.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
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.
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.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
1 C Programming Week 2 Variables, flow control and the Debugger.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
1 Structures. 2 What is a Structure? Used for handling a group of logically related data items  Examples: Student name, roll number, and marks Real part.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Arithmetic Expressions
Computer Organization and Design Pointers, Arrays and Strings in C
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ECE Application Programming
Strings (Continued) Chapter 13
A bit of C programming Lecture 3 Uli Raich.
Fundamentals of Characters and Strings
CS1010 Programming Methodology
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
Strings (מחרוזות).
ECE Application Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
CS1010 Programming Methodology
A First Book of ANSI C Fourth Edition
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Arrays in C.
EECE.2160 ECE Application Programming
Formatted and Unformatted Input/Output Functions
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.
Computer Science 210 Computer Organization
Structures.
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
הגדרת משתנים יום שלישי 27 נובמבר 2018
Programming Structures.
EECE.2160 ECE Application Programming
Week 2 Variables, flow control and the Debugger
Your questions from last session
Chapter 8 Character Arrays and Strings
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Exercise Arrays.
Strings in C Array of characters is called a string.
Lecture 19: Working with strings
Programming Strings.
Variables in C Topics Naming Variables Declaring Variables
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Introduction to Problem Solving and Programming
Presentation transcript:

C Programming Strings

Strings An array of characters Used to store text Two ways to initialize: char str[] = "Text"; Instead of: char str[] = {‘T’,’e’,’x’,’t’};

Where does it end? str Terminator …. 'H' 'e' 'l' 'o' ' ' 'w' 'r' 'd' 'g' '.' 'p' 'v' …. 's' '#' ' ' 'f' 'd' 'y' '4' '7' '$' '_' 'e' 'g' '.' 'p' 'v' …. 'H' 'e' 'l' 'l' 'o' ' ' 'w' 'o' 'r' 'l' 'd' '\0' 'd' '.' 'p' 'v' …. str Terminator

The Terminator Strings terminate with the '\0' character (ascii code 0) This is a convention used to know where the string ends It means that in order to hold a string of N characters we need an array of length N + 1 So: char str[] = "Text"; Is equal to: char str[] = {'T', ‘e', ‘x', ‘t', '\0'};

What does it print? int main(void) { char str[] = "I'm a full string"; printf("%s\n", str); str[7] = 'o'; str[8] = 'o'; str[10] = '\0'; str[10] = 's'; return 0; } 0 1 2 3 4 5 6 7 8 9 10 11… I’m a full string I’m a fool string I’m a fool I’m a foolsstring

Reading strings There are several ways of accepting strings as input from the user One way is to read character by character using getchar()

Example – Using getchar() #define MAX_LENGTH 20 int main(void) { char str[MAX_LENGTH + 1]; /* We need one more place for the '\0' */ char c; int i; printf("Please enter a string:\n"); i = 0; c = getchar(); while (c >= 0 && c != '\n' && i < MAX_LENGTH) str[i] = c; ++i; c = getchar(); } str[i] = '\0'; /* Terminate the string */ printf("The string you entered is: %s\n", str); return 0;

Reading strings: scanf Another way is to use scanf To read in a string to a variable str, write : scanf("%s", str); Note there is no ‘&’ sign!!!

Reading strings - scanf scanf reads in letters until a space or newline ('\n') is encountered The maximum length can be stated in the parentheses: scanf("%10s", str); This will read 10 letters, plus the '\0' sign (so str should be of size at least 11)

Example – using scanf #define MAX_LENGTH 20 int main(void) { char str[MAX_LENGTH + 1]; printf("Please enter a string:\n"); scanf("%20s", str); printf("The string you entered is: %s\n", str); return 0; }

scanf problem After using scanf the next character that will be read is the space or newline. For example: scanf("%s", str); scanf("%c", &tav); Here tav has the value ‘ ’ or newline (‘\n’).

Solving the problem We need to read and discard the unwanted newline. Either use getchar or inform scanf to expect spaces (also newline) before the next character. scanf("%s", str); scanf(" %c", &tav);

Comparing strings We cannot just compare strings’ contents by == char A[6]=“Hello”; char B[6]=“Hello”; if (A==B) { ... } Because A and B are addresses of A[0] and B[0] A==B only if A and B are the same string in memory In order to compare the contents we must scan char by char …. ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ …. …. …. ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ …. …. A B

Comparing Strings - Example int i; char A[101], B[101]; printf("Enter first string\n"); scanf("%100s",A); printf("Enter second string\n"); scanf("%100s",B); for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n");

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 1

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 1

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

Compare – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘w’ ‘\0’ i 2

Equal strings – step by step ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i

Equal strings – step by step ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i

Equal strings – step by step ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 1

Equal strings – step by step ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 1

Equal strings – step by step ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 2

Equal strings – step by step ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 2

Equal strings – step by step ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 3

Equal strings – step by step ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘s’ ‘\0’ i 3

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i 1

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i 1

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i 2

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i 2

Different length – step by step A ‘Y’ ‘e’ ‘s’ ‘\0’ for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i]) { printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); B ‘Y’ ‘e’ ‘\0’ i 2

Exercise implement the function replace void replace(char str[], char what, char with); The function scans the string and replaces every occurrence of the first char with the second one. write a program to test the above function the program should read a string from the user (no spaces) and two characters, then call the function with the input, and print the result. example Please enter a string (no spaces) papa Letter to replace: p Letter to replace with: m The result: mama

Solution void replace(char str[], char replace_what, char replace_with) { int i; for (i = 0; str[i] != '\0'; ++i) if (str[i] == replace_what) str[i] = replace_with; }

Solution #define STRING_LEN 100 int main(void) { char str[STRING_LEN + 1]; char replace_what, replace_with; printf("Please enter a string (no spaces)\n"); scanf("%100s", str); printf("Letter to replace: "); scanf(" %c", &replace_what); printf("Letter to replace with: "); scanf(" %c", &replace_with); replace(str, replace_what, replace_with); printf("The result: %s\n", str); return 0; }

String library Like in the case of stdio.h and math.h, we have a special library for handling strings We should #include <string.h>

String library All functions assume that a string ends with ‘\0’. Useful functions: int strlen(const char s[]) returns the length of s int strcmp(const char s1[], const char s2[]) compares s1 with s2 Returns 0 if they are equal, -1 if s1<s2, 1 if s1>s2 strcpy(char s1[], const char s2[]) copies to contents of s2 to s1 and more…

ctype library #include <ctype.h> Useful operation on single characters int isalpha(char c); int islower(char c); int isupper(char c); int isdigit(char c); char tolower(char c); char toupper(char c);

Exercise Implement the following function: int evaluate(char expr[]); ‘evaluate’ calculate the value of a mathematical expression comprised of positive numbers and the operations ‘+’ and ‘-’ For example: Input: “7+2-3+5” Output: 11 You may assume that the string is valid and doesn’t contain spaces. Also, that all numbers are single digit Test your function!

Guidance Read a number and according to the operation add or subtract from the current result. Use function int char2int(char c) in ctype.h How do we handle the first number? last one?

Solution int evaluate(char expr[]) { char op = '+'; int i, res = 0; while (expr[i] != '\0') if (op == '+') { res += char2int(expr[i]); } else { res -= char2int(expr[i]); } ++i; op = expr[i]; if (op != '\0') return res;

C Programming - Structures

Structures Often we want to be able to manipulate ‘logical entities’ as a whole For example, complex numbers, dates, student records, etc. A logical entity is often composed of more than one variable

Structures A struct (short for structure) is a collection of variables of different types, gathered into one super-variable It is used to define more complex data types Variables in a struct are called members or fields

Example – complex numbers. The following is the definition of a new ‘variable’ of type complex number: struct complex { int real; int img; }; Once we define a structure, we can treat it as any type. In a program, we can then write: struct complex num1, num2, num3;

Access structure members If A is a variable of some structure type with a member named x, then A.x is that member of A struct complex C; C.real = 0;

A more convenient usage - typedef An alternative definition: typedef struct complex_t { int real; int img; } complex; Now the program has a new variable type - “complex”. This way we don’t have to write “struct complex” every time! For example, we can define two complex numbers in the following line: complex num1, num2;

Example - AddComplex complex a, b, c; printf(“…"); real img a … real img b complex a, b, c; printf(“…"); scanf("%lf%lf",&(a.real),&(a.img)); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(“result = %g+%gi\n",c.real,c.img); return 0; … real img c

AddComplex – step by step … real img a … real img b complex a, b, c; printf(“…"); scanf("%lf%lf",&(a.real),&(a.img)); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(“result = %g+%gi\n",c.real,c.img); return 0; … real img c

AddComplex – step by step 1.0 2.0 real img a … real img b complex a, b, c; printf(“…"); scanf("%lf%lf",&(a.real),&(a.img)); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(“result = %g+%gi\n",c.real,c.img); return 0; … real img c

AddComplex – step by step 1.0 2.0 real img a … real img b complex a, b, c; printf(“…"); scanf("%lf%lf",&(a.real),&(a.img)); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(“result = %g+%gi\n",c.real,c.img); return 0; … real img c

AddComplex – step by step 1.0 2.0 real img a 3.0 4.0 real img b complex a, b, c; printf(“…"); scanf("%lf%lf",&(a.real),&(a.img)); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(“result = %g+%gi\n",c.real,c.img); return 0; … real img c

AddComplex – step by step 1.0 2.0 real img a 3.0 4.0 real img b complex a, b, c; printf(“…"); scanf("%lf%lf",&(a.real),&(a.img)); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(“result = %g+%gi\n",c.real,c.img); return 0; … real img c

AddComplex – step by step 1.0 2.0 real img x complex AddComp(complex x, complex y) { complex z; z.real = x.real + y.real; z.img = x.img + y.img; return z; } 3.0 4.0 real img y … real img z

AddComplex – step by step 1.0 2.0 real img x complex AddComp(complex x, complex y) { complex z; z.real = x.real + y.real; z.img = x.img + y.img; return z; } 3.0 4.0 real img y … 6.0 real img z

AddComplex – step by step 1.0 2.0 real img x complex AddComp(complex x, complex y) { complex z; z.real = x.real + y.real; z.img = x.img + y.img; return z; } 3.0 4.0 real img y 4.0 6.0 real img z

AddComplex – step by step 1.0 2.0 real img x complex AddComp(complex x, complex y) { complex z; z.real = x.real + y.real; z.img = x.img + y.img; return z; } 3.0 4.0 real img y 4.0 6.0 real img z

AddComplex – step by step 1.0 2.0 real img a 3.0 4.0 real img b complex a, b, c; printf(“…"); scanf("%lf%lf",&(a.real),&(a.img)); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(“result = %g+%gi\n",c.real,c.img); return 0; 4.0 6.0 real img c

AddComplex – step by step 1.0 2.0 real img a 3.0 4.0 real img b complex a, b, c; printf(“…"); scanf("%lf%lf",&(a.real),&(a.img)); scanf("%lf%lf",&(b.real),&(b.img)); c = AddComp(a,b); printf(“result = %g+%gi\n",c.real,c.img); return 0; 4.0 6.0 real img c

Miscellaneous structure trivia Structure members may be ordinary variable types, but also other structures and even arrays! Structures can therefore be rather large and take up a lot of space Many times we prefer to pass structures to functions by address, and not by value Just like arrays – we will discuss this further when talking about pointers

More trivia Structures cannot be compared using the == operator They must be compared member by member Usually this will be done in a separate function Structures can be copied using the = operator Member-wise copy

Example – Is in Circle We wish to write a program that checks whether a dot is inside a circle or not typedef struct dot_t { double x; double y; } dot; typedef struct circle_t dot center; double radius; } circle; Define two structs: Define two variables: dot d; circle c;

Is_in_circle – step by step … y x d (dot) printf(“Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(d, c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); c (circle) … y x center (dot) radius

Is_in_circle – step by step … y x d (dot) printf(“Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(d, c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); c (circle) … y x center (dot) radius

Is_in_circle – step by step 1.0 2.0 y x d (dot) printf(“Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(d, c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); c (circle) … y x center (dot) radius

Is_in_circle – step by step 1.0 2.0 y x d (dot) printf(“Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(d, c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); c (circle) … y x center (dot) radius

Is_in_circle – step by step 1.0 2.0 y x d (dot) printf(“Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(d, c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); c (circle) 0.0 y x center (dot) radius …

Is_in_circle – step by step 1.0 2.0 y x d (dot) printf(“Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(d, c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); c (circle) 0.0 y x center (dot) radius …

Is_in_circle – step by step 1.0 2.0 y x d (dot) printf(“Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(d, c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); c (circle) 0.0 y x center (dot) radius 5

Is_in_circle – step by step 1.0 2.0 y x d (dot) printf(“Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(d, c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); c (circle) 0.0 y x center (dot) radius 5

Is_in_circle – step by step int IsInCircle(dot d, circle c) { double x_dist,y_dist; x_dist = d.x – c.center.x; y_dist = d.y – c.center.y; if (x_dist*x_dist + y_dist*y_dist <= c.radius*c.radius) return 1; return 0; } x_dist y_dist … … &d &c 756 1024 c(circle) 0.0 y x center (dot) radius 5 1.0 2.0 y x d(dot)

Is_in_circle – step by step int IsInCircle(dot d, circle c) { double x_dist,y_dist; x_dist = d.x – c.center.x; y_dist = d.y – c.center.y; if (x_dist*x_dist + y_dist*y_dist <= c.radius*c.radius) return 1; return 0; } x_dist y_dist 1.0 … &d &c 756 1024 c(circle) 0.0 y x center (dot) radius 5 1.0 2.0 y x d(dot)

Is_in_circle – step by step int IsInCircle(dot d, circle c) { double x_dist,y_dist; x_dist = d.x – c.center.x; y_dist = d.y – c.center.y; if (x_dist*x_dist + y_dist*y_dist <= c.radius*c.radius) return 1; return 0; } x_dist y_dist 1.0 2.0 &d &c 756 1024 c(circle) 0.0 y x center (dot) radius 5 1.0 2.0 y x d(dot)

Is_in_circle – step by step int IsInCircle(dot d, circle c) { double x_dist,y_dist; x_dist =d.x – c.center.x; y_dist = c.y – c.center.y; if (x_dist*x_dist + y_dist*y_dist <= c.radius*c.radius) return 1; return 0; } x_dist y_dist 1.0 2.0 &d &c 756 1024 c(circle) 0.0 y x center (dot) radius 5 1.0 2.0 y x d(dot)

Is_in_circle – step by step int IsInCircle(dot d, circle c) { double x_dist,y_dist; x_dist = d.x – c.center.x; y_dist = d.y – c.center.y; if (x_dist*x_dist + y_dist*y_dist <= c.radius*c.radius) return 1; return 0; } x_dist y_dist 1.0 2.0 &d &c 756 1024 c (circle) 0.0 y x center (dot) radius 5 1.0 2.0 y x d (dot)

Is_in_circle – step by step 1.0 2.0 y x d (dot) printf(“Enter dot\n"); scanf("%lf%lf",&d.x,&d.y); printf("Enter circle center\n"); scanf("%lf%lf",&c.center.x,&c.center.y); printf("Enter circle radius\n"); scanf("%lf",&c.radius); if (IsInCircle(d, c)) printf("dot is in circle\n"); else printf("dot is out of circle\n"); c (circle) 0.0 y x center (dot) radius 5

Exercise Implement the MultComplex function: Input – two complex numbers Output – their multiplication Note: if x=a+ib and y=c+id then: z = xy = (ac-bd)+i(ad+bc) Write a program that uses the above function to multiply two complex numbers given by the user

Solution typedef struct complex { double real; double img; int main(){ comp a, b, c; printf("Enter first num real and imaginary parts\n"); scanf("%lf%lf", &(a.real), &(a.img)); printf("Enter second num real and imaginary parts\n"); scanf("%lf%lf", &(b.real), &(b.img)); c = MultiplyComp(a, b); printf("first number * second number = %g+%gi\n", c.real, c.img); }

Solution comp MultiplyComp(comp a, comp b) { comp c; c.real = a.real*b.real - a.img*b.img; c.img = a.real*b.img + a.img*b.real; return c; }

Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure that contains a member which is an array, the array is copied element by element Not just the address gets copied For example - array_member.c Reminder – ordinary arrays can’t be copied simply by using the ‘=‘ operator They must be copied using a loop

Structures containing arrays When passing the structure to a function by value Changing the array field inside the function won’t change it in the calling function Reminder – when passing an ordinary array to a function, all that gets passed is the address of its first element Hence every change to the array within the function, changes the array in the calling function