SlideShare a Scribd company logo
1 of 19
Download to read offline
CS110: Arrays in C
Lecture 9
V. Kamakoti
21st January 2008
Arrays
• String is array of characters
• char a[10];
• strcpy(a,”kamakoti”);
• equivalent to
• a[0] = ‘k’; a[1] = ‘a’; … a[7] = ‘i’;
a[8] = ‘0’
Arrays
• You can’t assign
– a = “kamakoti”;
– Except at time of declaration
• char a[] = kamakoti

• When you would like to use string commands
inside the program
– #include <string.h>

• Other commands
– strlen() - Number of characters preceding NULL
– strcat(a,b) - Concats the string “b” to “a”.
Example
#include <stdio.h>
#include <string.h>
main() {
char a[40],b[20];
strcpy(a,”kama”);
strcpy(b,”koti”);
strcat(a,b);
printf(“%sn”,a);
printf(“%d %dn”,strlen(a),strlen(b));
}
Example
• kamakoti
• 84
Other Arrays
•
•
•
•
•
•
•
•

int A[100];
Can store 100 integers in sequence.
Let int be 4 bytes
A[0] is stored in 100 - 103
A[1] is stored in 104 - 107
A[2] is stored in 108 - 111
….
A[i] is stored in (100 + i*4) - (100 + i*4 + 3)
Other Arrays
• This implies given where A[0] is stored
say B, you can compute where A[j] is
stored.
– (B + i*sizeof(int)) - (B+ i*sizeof(int) + sizeof(int)-1)

• Extend it for double A[100]
– (B + i*sizeof(double)) - (B+ i*sizeof(double) + sizeof(double)-1)
Example
#include <stdio.h>
main() {
int A[100];
A[0] = 5;
A[1] = 10;
printf(“%d %dn”,A[0],A[1]);
printf(“%u %u %u %u”,A,&A[0],&A[2],&A[5]);
}
Output
• 5 10
• 3221224144 3221224144 3221224148
3221224164
• scanf(“%d”,&A[3]); to input an element
of an array
Matrix
• int a[5][4] is a 5X4 matrix and you can
store upto 5 X 4 dimension.
• You can use the above for 3 X 3 also.
• It is maximum dimension
Example
#include <stdio.h>
main() {
int A[10][10];
A[0][0] = 5;
A[1][0] = 10;
printf(“%d %dn”,A[0][0],A[1][0]);
}
Output is
5 10
Creative Exercise – 5
The Celebrity problem

0
0
0
0
0
0
0
0
Celebrity is a person who is known to everyone but
does not know anyone. To find a celebrity among “n”
people by questioning them. You can ask only one
type of question. You can ask person X whether he
knows Y.
Solution
• Take any two person A and B
• “Ask A if he knows B” - no one lies
– If yes then A is not a celebrity else B is not a
celebrity
– Every question eliminates one
– (n-1) questions eliminates n-1 person
– The left out person CAN be a Celebrity
– You need 2(n-1) questions more
– Total 3(n -1) questions.
Creative Problem
• Suppose you are using a program that
reads in a large English text file as input
and processes it. The program for some
reason does not like a set of words and
whenever it sees the same in your text
file it halts and outputs “Error”. It is so
angry that it does not say what the word
is and which line it occurred or any
other info.
Creative Problem
• You do not have a list of offending
words. Devise a strategy to identify the
first offending word in your text. Express
the efficiency of your strategy in terms
of the number of words in the input text
file.
Solution
• The input file T is indeed erroneous. The
program gives error on input file T
• Suppose the text file T has n = 2k words
• Split the text file T into two parts T1 and T2,
containing the first and the last n/2 words
respectively
• Give T1 to program (not T2 - why?)
– If error then the offending word is in T1; else in T2
Solution
• Assume offending word in T1
– Split T1 into T11 and T12 of n/4 words
each
– Give T11 to program
• If error then offending word in T11 else in T12

• Finally you will land up with a file with 2
words. Split them as one word each and
give them in order to the program to find
out the first offending word.
Solution
• T(n) = T(n/2) + 1
•
= T(n/4) + 1 + 1
•
= T(n/2k) + k
•
= T(n/2logn) + log2n
•
= T(1) + log2n
•
= 1 + log2n
Thank You

More Related Content

What's hot

Coding Test Review1
Coding Test Review1Coding Test Review1
Coding Test Review1SEMINARGROOT
 
Scheme Programming Language
Scheme Programming LanguageScheme Programming Language
Scheme Programming LanguageReham AlBlehid
 
Coding test review 2
Coding test review 2Coding test review 2
Coding test review 2SEMINARGROOT
 
Functional programming with F#
Functional programming with F#Functional programming with F#
Functional programming with F#Remik Koczapski
 
Python data type
Python data typePython data type
Python data typenuripatidar
 
Modules 17 12_2020
Modules 17 12_2020Modules 17 12_2020
Modules 17 12_2020Sugnan M
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPKamal Acharya
 

What's hot (10)

Coding Test Review1
Coding Test Review1Coding Test Review1
Coding Test Review1
 
Scheme Programming Language
Scheme Programming LanguageScheme Programming Language
Scheme Programming Language
 
Coding test review 2
Coding test review 2Coding test review 2
Coding test review 2
 
Functional programming with F#
Functional programming with F#Functional programming with F#
Functional programming with F#
 
Python data type
Python data typePython data type
Python data type
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Modules 17 12_2020
Modules 17 12_2020Modules 17 12_2020
Modules 17 12_2020
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
C programming , array 2020
C programming , array 2020C programming , array 2020
C programming , array 2020
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 

Viewers also liked

Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringSri Harsha Pamu
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringSri Harsha Pamu
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringSri Harsha Pamu
 
Lec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringLec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringSri Harsha Pamu
 
Ubuntu+python+selenium=легкий старт
Ubuntu+python+selenium=легкий стартUbuntu+python+selenium=легкий старт
Ubuntu+python+selenium=легкий стартAndrey Matukhno
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringSri Harsha Pamu
 
Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringSri Harsha Pamu
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringSri Harsha Pamu
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringSri Harsha Pamu
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringSri Harsha Pamu
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringSri Harsha Pamu
 

Viewers also liked (14)

Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational Engineering
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational Engineering
 
Android..imp google
Android..imp googleAndroid..imp google
Android..imp google
 
Android gui framework
Android gui frameworkAndroid gui framework
Android gui framework
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational Engineering
 
Hackernote on gsoc
Hackernote on gsocHackernote on gsoc
Hackernote on gsoc
 
Lec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringLec19-CS110 Computational Engineering
Lec19-CS110 Computational Engineering
 
Ubuntu+python+selenium=легкий старт
Ubuntu+python+selenium=легкий стартUbuntu+python+selenium=легкий старт
Ubuntu+python+selenium=легкий старт
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational Engineering
 
Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational Engineering
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational Engineering
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational Engineering
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational Engineering
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational Engineering
 

Similar to Lec09-CS110 Computational Engineering

Similar to Lec09-CS110 Computational Engineering (20)

SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
 
Core Concept_Python.pptx
Core Concept_Python.pptxCore Concept_Python.pptx
Core Concept_Python.pptx
 
Ry pyconjp2015 turtle
Ry pyconjp2015 turtleRy pyconjp2015 turtle
Ry pyconjp2015 turtle
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Array
ArrayArray
Array
 
C language first program
C language first programC language first program
C language first program
 
Python-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptxPython-CH01L04-Presentation.pptx
Python-CH01L04-Presentation.pptx
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Arrays
ArraysArrays
Arrays
 
P3 2018 python_regexes
P3 2018 python_regexesP3 2018 python_regexes
P3 2018 python_regexes
 
L5 array
L5 arrayL5 array
L5 array
 
paython practical
paython practical paython practical
paython practical
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Basics of Dynamic programming
Basics of Dynamic programming Basics of Dynamic programming
Basics of Dynamic programming
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt
 
Scala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To KnowScala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To Know
 

More from Sri Harsha Pamu

Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 
Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringSri Harsha Pamu
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringSri Harsha Pamu
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringSri Harsha Pamu
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringSri Harsha Pamu
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringSri Harsha Pamu
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringSri Harsha Pamu
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability studySri Harsha Pamu
 

More from Sri Harsha Pamu (10)

Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Lec13
Lec13Lec13
Lec13
 
Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational Engineering
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational Engineering
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational Engineering
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational Engineering
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational Engineering
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational Engineering
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability study
 
Boot2Gecko Hackernote
Boot2Gecko HackernoteBoot2Gecko Hackernote
Boot2Gecko Hackernote
 

Recently uploaded

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 

Recently uploaded (20)

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 

Lec09-CS110 Computational Engineering

  • 1. CS110: Arrays in C Lecture 9 V. Kamakoti 21st January 2008
  • 2. Arrays • String is array of characters • char a[10]; • strcpy(a,”kamakoti”); • equivalent to • a[0] = ‘k’; a[1] = ‘a’; … a[7] = ‘i’; a[8] = ‘0’
  • 3. Arrays • You can’t assign – a = “kamakoti”; – Except at time of declaration • char a[] = kamakoti • When you would like to use string commands inside the program – #include <string.h> • Other commands – strlen() - Number of characters preceding NULL – strcat(a,b) - Concats the string “b” to “a”.
  • 4. Example #include <stdio.h> #include <string.h> main() { char a[40],b[20]; strcpy(a,”kama”); strcpy(b,”koti”); strcat(a,b); printf(“%sn”,a); printf(“%d %dn”,strlen(a),strlen(b)); }
  • 6. Other Arrays • • • • • • • • int A[100]; Can store 100 integers in sequence. Let int be 4 bytes A[0] is stored in 100 - 103 A[1] is stored in 104 - 107 A[2] is stored in 108 - 111 …. A[i] is stored in (100 + i*4) - (100 + i*4 + 3)
  • 7. Other Arrays • This implies given where A[0] is stored say B, you can compute where A[j] is stored. – (B + i*sizeof(int)) - (B+ i*sizeof(int) + sizeof(int)-1) • Extend it for double A[100] – (B + i*sizeof(double)) - (B+ i*sizeof(double) + sizeof(double)-1)
  • 8. Example #include <stdio.h> main() { int A[100]; A[0] = 5; A[1] = 10; printf(“%d %dn”,A[0],A[1]); printf(“%u %u %u %u”,A,&A[0],&A[2],&A[5]); }
  • 9. Output • 5 10 • 3221224144 3221224144 3221224148 3221224164 • scanf(“%d”,&A[3]); to input an element of an array
  • 10. Matrix • int a[5][4] is a 5X4 matrix and you can store upto 5 X 4 dimension. • You can use the above for 3 X 3 also. • It is maximum dimension
  • 11. Example #include <stdio.h> main() { int A[10][10]; A[0][0] = 5; A[1][0] = 10; printf(“%d %dn”,A[0][0],A[1][0]); } Output is 5 10
  • 12. Creative Exercise – 5 The Celebrity problem 0 0 0 0 0 0 0 0 Celebrity is a person who is known to everyone but does not know anyone. To find a celebrity among “n” people by questioning them. You can ask only one type of question. You can ask person X whether he knows Y.
  • 13. Solution • Take any two person A and B • “Ask A if he knows B” - no one lies – If yes then A is not a celebrity else B is not a celebrity – Every question eliminates one – (n-1) questions eliminates n-1 person – The left out person CAN be a Celebrity – You need 2(n-1) questions more – Total 3(n -1) questions.
  • 14. Creative Problem • Suppose you are using a program that reads in a large English text file as input and processes it. The program for some reason does not like a set of words and whenever it sees the same in your text file it halts and outputs “Error”. It is so angry that it does not say what the word is and which line it occurred or any other info.
  • 15. Creative Problem • You do not have a list of offending words. Devise a strategy to identify the first offending word in your text. Express the efficiency of your strategy in terms of the number of words in the input text file.
  • 16. Solution • The input file T is indeed erroneous. The program gives error on input file T • Suppose the text file T has n = 2k words • Split the text file T into two parts T1 and T2, containing the first and the last n/2 words respectively • Give T1 to program (not T2 - why?) – If error then the offending word is in T1; else in T2
  • 17. Solution • Assume offending word in T1 – Split T1 into T11 and T12 of n/4 words each – Give T11 to program • If error then offending word in T11 else in T12 • Finally you will land up with a file with 2 words. Split them as one word each and give them in order to the program to find out the first offending word.
  • 18. Solution • T(n) = T(n/2) + 1 • = T(n/4) + 1 + 1 • = T(n/2k) + k • = T(n/2logn) + log2n • = T(1) + log2n • = 1 + log2n