SlideShare a Scribd company logo
C_Programming
Part 4
ENG. KEROLES SHENOUDA
1
Advanced Data Types: Arrays
 Array is a single variable that able to hold several values.
Programmer can set or get specific
value in the array.
To define an array variable containing 100 (float) values:
2
Arrays
 The position of each value is called the index. The index of the first value is 0,
which means
if the array contains 10 items the index range from 0 to 9.
 Dealing with array elements is very similar to normal variables; you can assign
a value to any item or assign any item to other program variables. Following
code illustrates this idea:
3
float degrees[100];
float x=9.9;
float y;
//assign x value to the fifth item
degrees[4] = x;
//assign fifth item to y
y = degrees[4];
//scan eighth item from user
scanf("%f", &degrees[7]);
//print eighth item from user
printf("The eighth item value is %f", degrees[7]);
Lab: Store and Print 10
Students Degrees
 shows how to scan 10
students degree from user,
stores them in a single
area, and then prints them.
4
5
6
7
Arrays
 Initializing the array:
When the array is defined, there is no data assigned to their elements.
Assigning initial
values for array elements is called initializing the array. Following code section
shows how
to initialize the array:
8
Lab:Calculate Polynomial
Value for a Set of Inputs
9
the program works
correctly, however it
appears that code size
contains many
repeated sections, each
section for each value. If
the number of values
increases code size
increases.
Alternative solve it
using Arrays and
Loops
10
Lab: Calculate the Maximum of
the Array
float degrees[] = {75.5, 88.0, 89.5, 23.5,
72.0,63.5, 57.5, 62.0, 13.5, 46.5};
11
printf ("=========>>>>>> Part2 descending order n");
printf ("=========>>>>>> Part3 ascending order n");
12
13
Bubble Sort 14
Bubble Sort 15
2D Arrays 16
2D Arrays 17
Following code section shows how to
use 2D arrays:
18
LAB: Calculate and Print the Transpose
of 3x3 Matrix
19
20
Strings
 String is a set of several consecutive characters; each character can be
represented in C language by a (char) data type.
 This means that string value can be represented by an array of (char)
21
Above code shows how to store “hello” string letters in array.
The last letter is sited with zero value (null termination), this value is
important to tell the computer that the string is
terminated before this item.
Printing String Value
 “text” variable contains 100 (char) value, only
first 5 places is used to hold the word “hello”
and
 the sixth place is used to hold the null
termination.
 In printf, “%s” is used to inform the program
that it will print a string value.
 Important: printf uses the null termination to
end the printing operation. If the null
termination is not used, the program continues
printing the following memory contents until
it reach a zero.
22
23
24
25
Scanning String Value 26
 scanf is used with “%s” to scan string input
from keyboard. However there is a problem?
 scanf takes only the first word in the input
text and leave the rest.
C language provides a solution for above
problem using gets function
27
Array of Strings 28
Printing Array of Strings 29
Copy String to String 30
There is another
solution to above
problem using strcpy
function. strcpy takes
both the
destination and the
source strings and
performs the coping
operation internally.
strcpy 31
Adding String to String 32
Changing String Case
 strlwr function changes all string letters to the lower
case.
Ex: “AhMed” “ahmed”
 strupr function change all string letters to the upper
case.
Ex: “aHmeD” “AHMED
33
Finding the String Length 34
Comparing Two Strings
 strcmp function compares two strings and produces one of three results:
 if the two string are identical it gives 0
 if the first string is lower in the alphabetic order it gives -1
 if the second string is higher in the alphabetic order it gives 1
“ahmed” and “ahmed” 0
“ahmed” and “amgad” -1 because the second letter „h‟ is less than „m‟
“maged” and “aya” 1 because the first letter „m‟ is greater than „a‟
strcmp differentiate between capital and small letters which means “MAGED” is
less than “ahmed”, because the ASCII code of the capital letters is lower than the ASCII
code of the small letters. To solve this problem you can change the case of both strings
to the same case then use strcmp function. Alternatively you can use stricmp
function which performs the
comparison independent on the string case
35
36
Converting String to Integer Value 37
The variable text contains 4 ASCII letters „1‟, „0‟, „2‟, „5‟. The statement (x + text) is
completely wrong, because text is string and not a number, computer cannot understand
string contents directly.
atoi function helps computer to convert string to a number of type (int) if it is applicable
otherwise it gives zero, for example:
“1025”  1025
“120KG”  120
“The Cost is 30”  0
Converting String to Integer Value 38
Converting String to Real Value
 atof function helps computer to convert string to a number of type (float) if it
is applicable
otherwise it gives zero, for example:
“10.25” > 10.25
“1.2KG” > 1.2
“The Cost is 3.5” > 0
39
40
Follow Chapter 4/5:
Controlling Program
Flow
C PROGRAMMING FOR ENGINEERS, DR. MOHAMED SOBH
41The next Part we will talk about Variables Scope
References 42
 The Case for Learning C as Your First Programming Language
 A Tutorial on Data Representation
 std::printf, std::fprintf, std::sprintf, std::snprintf…..
 C Programming for Engineers, Dr. Mohamed Sobh
 C programming expert.
 fresh2refresh.com/c-programming
Ad

Recommended

C programming part4
C programming part4
Keroles karam khalil
 
Session 5-exersice
Session 5-exersice
Keroles karam khalil
 
C programming session8
C programming session8
Keroles karam khalil
 
C programming session5
C programming session5
Keroles karam khalil
 
C programming session7
C programming session7
Keroles karam khalil
 
C language
C language
Mohamed Bedair
 
C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
Anurag University Hyderabad
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]
ecko_disasterz
 
C programming session3
C programming session3
Keroles karam khalil
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
SowmyaJyothi3
 
C programming session6
C programming session6
Keroles karam khalil
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
Rc Os
 
Ch7 Basic Types
Ch7 Basic Types
SzeChingChen
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Ch3 Formatted Input/Output
Ch3 Formatted Input/Output
SzeChingChen
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Assignment12
Assignment12
Sunita Milind Dol
 
C language basics
C language basics
Nikshithas R
 
CP Handout#8
CP Handout#8
trupti1976
 
Ocs752 unit 1
Ocs752 unit 1
mgrameshmail
 
C programming part2
C programming part2
Keroles karam khalil
 
Type Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic Expressions
PVS-Studio
 
Assignment8
Assignment8
Sunita Milind Dol
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output Commands
Shameer Ahmed Koya
 
Ocs752 unit 3
Ocs752 unit 3
mgrameshmail
 
Ch8 Arrays
Ch8 Arrays
SzeChingChen
 
CP Handout#2
CP Handout#2
trupti1976
 
Notes part3
Notes part3
Keroles karam khalil
 
Homework 3
Homework 3
Keroles karam khalil
 

More Related Content

What's hot (20)

C programming session3
C programming session3
Keroles karam khalil
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
SowmyaJyothi3
 
C programming session6
C programming session6
Keroles karam khalil
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
Rc Os
 
Ch7 Basic Types
Ch7 Basic Types
SzeChingChen
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Ch3 Formatted Input/Output
Ch3 Formatted Input/Output
SzeChingChen
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Assignment12
Assignment12
Sunita Milind Dol
 
C language basics
C language basics
Nikshithas R
 
CP Handout#8
CP Handout#8
trupti1976
 
Ocs752 unit 1
Ocs752 unit 1
mgrameshmail
 
C programming part2
C programming part2
Keroles karam khalil
 
Type Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic Expressions
PVS-Studio
 
Assignment8
Assignment8
Sunita Milind Dol
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output Commands
Shameer Ahmed Koya
 
Ocs752 unit 3
Ocs752 unit 3
mgrameshmail
 
Ch8 Arrays
Ch8 Arrays
SzeChingChen
 
CP Handout#2
CP Handout#2
trupti1976
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
SowmyaJyothi3
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
CLASS VIII COMPUTERS FLOW CHART AND ALGORITHM
Rc Os
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Ch3 Formatted Input/Output
Ch3 Formatted Input/Output
SzeChingChen
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Type Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic Expressions
PVS-Studio
 
MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output Commands
Shameer Ahmed Koya
 

Viewers also liked (20)

Notes part3
Notes part3
Keroles karam khalil
 
Homework 3
Homework 3
Keroles karam khalil
 
Homework 2 solution
Homework 2 solution
Keroles karam khalil
 
Microcontroller part 1
Microcontroller part 1
Keroles karam khalil
 
Microcontroller part 2
Microcontroller part 2
Keroles karam khalil
 
Microcontroller part 4
Microcontroller part 4
Keroles karam khalil
 
Microcontroller part 3
Microcontroller part 3
Keroles karam khalil
 
K vector embedded_linux_workshop
K vector embedded_linux_workshop
Keroles karam khalil
 
Embedded C programming session10
Embedded C programming session10
Keroles karam khalil
 
Automative basics v3
Automative basics v3
Keroles karam khalil
 
C programming first_session
C programming first_session
Keroles karam khalil
 
Microcontroller part 5
Microcontroller part 5
Keroles karam khalil
 
C programming part2
C programming part2
Keroles karam khalil
 
Microcontroller part 4
Microcontroller part 4
Keroles karam khalil
 
Microcontroller part 2
Microcontroller part 2
Keroles karam khalil
 
Microcontroller part 6_v1
Microcontroller part 6_v1
Keroles karam khalil
 
Microcontroller part 3
Microcontroller part 3
Keroles karam khalil
 
Microcontroller part 7_v1
Microcontroller part 7_v1
Keroles karam khalil
 
Microcontroller part 8_v1
Microcontroller part 8_v1
Keroles karam khalil
 
Microcontroller part 1
Microcontroller part 1
Keroles karam khalil
 
Ad

Similar to C programming part4 (20)

VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
Suraj Kumar
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
Rai University
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
Rai University
 
Strings
Strings
Mitali Chugh
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
Rai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-
aneebkmct
 
C programming session 04
C programming session 04
Dushmanta Nath
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
function, storage class and array and strings
function, storage class and array and strings
Rai University
 
unit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentation
MukeshTheLioner
 
Lec 25 - arrays-strings
Lec 25 - arrays-strings
Princess Sam
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 
Array and string
Array and string
prashant chelani
 
Matlab strings
Matlab strings
pramodkumar1804
 
MATLAB
MATLAB
svati sharma
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
Rajeshkumar Reddy
 
Lecture 05 2017
Lecture 05 2017
Jesmin Akhter
 
Pythonintro
Pythonintro
Hardik Malhotra
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
Suraj Kumar
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
Rai University
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
Rai University
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
Rai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-
aneebkmct
 
C programming session 04
C programming session 04
Dushmanta Nath
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Kurmendra Singh
 
function, storage class and array and strings
function, storage class and array and strings
Rai University
 
unit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentation
MukeshTheLioner
 
Lec 25 - arrays-strings
Lec 25 - arrays-strings
Princess Sam
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
Rai University
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
Rajeshkumar Reddy
 
Ad

More from Keroles karam khalil (20)

C basics quiz part 1_solution
C basics quiz part 1_solution
Keroles karam khalil
 
Autosar Basics hand book_v1
Autosar Basics hand book_v1
Keroles karam khalil
 
Automotive embedded systems part6 v2
Automotive embedded systems part6 v2
Keroles karam khalil
 
Automotive embedded systems part5 v2
Automotive embedded systems part5 v2
Keroles karam khalil
 
EMBEDDED C
EMBEDDED C
Keroles karam khalil
 
Automotive embedded systems part7 v1
Automotive embedded systems part7 v1
Keroles karam khalil
 
Automotive embedded systems part6 v1
Automotive embedded systems part6 v1
Keroles karam khalil
 
Automotive embedded systems part5 v1
Automotive embedded systems part5 v1
Keroles karam khalil
 
Automotive embedded systems part4 v1
Automotive embedded systems part4 v1
Keroles karam khalil
 
Automotive embedded systems part3 v1
Automotive embedded systems part3 v1
Keroles karam khalil
 
Automotive embedded systems part2 v1
Automotive embedded systems part2 v1
Keroles karam khalil
 
Automotive embedded systems part1 v1
Automotive embedded systems part1 v1
Keroles karam khalil
 
Automotive embedded systems part8 v1
Automotive embedded systems part8 v1
Keroles karam khalil
 
Quiz 9
Quiz 9
Keroles karam khalil
 
C programming session10
C programming session10
Keroles karam khalil
 
C programming session9 -
C programming session9 -
Keroles karam khalil
 
Quiz 10
Quiz 10
Keroles karam khalil
 
Homework 6
Homework 6
Keroles karam khalil
 
Homework 5 solution
Homework 5 solution
Keroles karam khalil
 
C programming session8
C programming session8
Keroles karam khalil
 
Automotive embedded systems part6 v2
Automotive embedded systems part6 v2
Keroles karam khalil
 
Automotive embedded systems part5 v2
Automotive embedded systems part5 v2
Keroles karam khalil
 
Automotive embedded systems part7 v1
Automotive embedded systems part7 v1
Keroles karam khalil
 
Automotive embedded systems part6 v1
Automotive embedded systems part6 v1
Keroles karam khalil
 
Automotive embedded systems part5 v1
Automotive embedded systems part5 v1
Keroles karam khalil
 
Automotive embedded systems part4 v1
Automotive embedded systems part4 v1
Keroles karam khalil
 
Automotive embedded systems part3 v1
Automotive embedded systems part3 v1
Keroles karam khalil
 
Automotive embedded systems part2 v1
Automotive embedded systems part2 v1
Keroles karam khalil
 
Automotive embedded systems part1 v1
Automotive embedded systems part1 v1
Keroles karam khalil
 
Automotive embedded systems part8 v1
Automotive embedded systems part8 v1
Keroles karam khalil
 

Recently uploaded (20)

Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
jutaydeonne
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Paper 106 | Ambition and Corruption: A Comparative Analysis of ‘The Great Gat...
Paper 106 | Ambition and Corruption: A Comparative Analysis of ‘The Great Gat...
Rajdeep Bavaliya
 
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
June 2025 Progress Update With Board Call_In process.pptx
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
jutaydeonne
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
How to use search fetch method in Odoo 18
How to use search fetch method in Odoo 18
Celine George
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Paper 106 | Ambition and Corruption: A Comparative Analysis of ‘The Great Gat...
Paper 106 | Ambition and Corruption: A Comparative Analysis of ‘The Great Gat...
Rajdeep Bavaliya
 
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 

C programming part4

  • 2. Advanced Data Types: Arrays  Array is a single variable that able to hold several values. Programmer can set or get specific value in the array. To define an array variable containing 100 (float) values: 2
  • 3. Arrays  The position of each value is called the index. The index of the first value is 0, which means if the array contains 10 items the index range from 0 to 9.  Dealing with array elements is very similar to normal variables; you can assign a value to any item or assign any item to other program variables. Following code illustrates this idea: 3 float degrees[100]; float x=9.9; float y; //assign x value to the fifth item degrees[4] = x; //assign fifth item to y y = degrees[4]; //scan eighth item from user scanf("%f", &degrees[7]); //print eighth item from user printf("The eighth item value is %f", degrees[7]);
  • 4. Lab: Store and Print 10 Students Degrees  shows how to scan 10 students degree from user, stores them in a single area, and then prints them. 4
  • 5. 5
  • 6. 6
  • 7. 7
  • 8. Arrays  Initializing the array: When the array is defined, there is no data assigned to their elements. Assigning initial values for array elements is called initializing the array. Following code section shows how to initialize the array: 8
  • 9. Lab:Calculate Polynomial Value for a Set of Inputs 9 the program works correctly, however it appears that code size contains many repeated sections, each section for each value. If the number of values increases code size increases. Alternative solve it using Arrays and Loops
  • 10. 10
  • 11. Lab: Calculate the Maximum of the Array float degrees[] = {75.5, 88.0, 89.5, 23.5, 72.0,63.5, 57.5, 62.0, 13.5, 46.5}; 11 printf ("=========>>>>>> Part2 descending order n"); printf ("=========>>>>>> Part3 ascending order n");
  • 12. 12
  • 13. 13
  • 18. Following code section shows how to use 2D arrays: 18
  • 19. LAB: Calculate and Print the Transpose of 3x3 Matrix 19
  • 20. 20
  • 21. Strings  String is a set of several consecutive characters; each character can be represented in C language by a (char) data type.  This means that string value can be represented by an array of (char) 21 Above code shows how to store “hello” string letters in array. The last letter is sited with zero value (null termination), this value is important to tell the computer that the string is terminated before this item.
  • 22. Printing String Value  “text” variable contains 100 (char) value, only first 5 places is used to hold the word “hello” and  the sixth place is used to hold the null termination.  In printf, “%s” is used to inform the program that it will print a string value.  Important: printf uses the null termination to end the printing operation. If the null termination is not used, the program continues printing the following memory contents until it reach a zero. 22
  • 23. 23
  • 24. 24
  • 25. 25
  • 26. Scanning String Value 26  scanf is used with “%s” to scan string input from keyboard. However there is a problem?  scanf takes only the first word in the input text and leave the rest.
  • 27. C language provides a solution for above problem using gets function 27
  • 29. Printing Array of Strings 29
  • 30. Copy String to String 30 There is another solution to above problem using strcpy function. strcpy takes both the destination and the source strings and performs the coping operation internally.
  • 32. Adding String to String 32
  • 33. Changing String Case  strlwr function changes all string letters to the lower case. Ex: “AhMed” “ahmed”  strupr function change all string letters to the upper case. Ex: “aHmeD” “AHMED 33
  • 34. Finding the String Length 34
  • 35. Comparing Two Strings  strcmp function compares two strings and produces one of three results:  if the two string are identical it gives 0  if the first string is lower in the alphabetic order it gives -1  if the second string is higher in the alphabetic order it gives 1 “ahmed” and “ahmed” 0 “ahmed” and “amgad” -1 because the second letter „h‟ is less than „m‟ “maged” and “aya” 1 because the first letter „m‟ is greater than „a‟ strcmp differentiate between capital and small letters which means “MAGED” is less than “ahmed”, because the ASCII code of the capital letters is lower than the ASCII code of the small letters. To solve this problem you can change the case of both strings to the same case then use strcmp function. Alternatively you can use stricmp function which performs the comparison independent on the string case 35
  • 36. 36
  • 37. Converting String to Integer Value 37 The variable text contains 4 ASCII letters „1‟, „0‟, „2‟, „5‟. The statement (x + text) is completely wrong, because text is string and not a number, computer cannot understand string contents directly. atoi function helps computer to convert string to a number of type (int) if it is applicable otherwise it gives zero, for example: “1025”  1025 “120KG”  120 “The Cost is 30”  0
  • 38. Converting String to Integer Value 38
  • 39. Converting String to Real Value  atof function helps computer to convert string to a number of type (float) if it is applicable otherwise it gives zero, for example: “10.25” > 10.25 “1.2KG” > 1.2 “The Cost is 3.5” > 0 39
  • 40. 40
  • 41. Follow Chapter 4/5: Controlling Program Flow C PROGRAMMING FOR ENGINEERS, DR. MOHAMED SOBH 41The next Part we will talk about Variables Scope
  • 42. References 42  The Case for Learning C as Your First Programming Language  A Tutorial on Data Representation  std::printf, std::fprintf, std::sprintf, std::snprintf…..  C Programming for Engineers, Dr. Mohamed Sobh  C programming expert.  fresh2refresh.com/c-programming