week10
week10
Advantages of Arrays?
The size of the above arrays is 5 which is automatically deduced by the compiler.
Task-2
Scenario: You are working on a program that allows the user to enter 6
temperatures for each day of the week. The user should also have the ability to
update a specific day’s temperature. After updating, display the entire week's
temperature.
Task-3
Scenario: Create a C program that accepts 6 integers from the user. The
program should allow the user to input an index and then display the value
stored at that index in the array.
Strings
■ Strings are the form of data used in programming
languages for storing an manipulating text such as
words, names, and sentences.
■ In C, a string is not a formal data type as it is in some
languages (e.g., Pascal, BASIC, VB, etc).
■ String is an array of type char.
■ All strings must end with a null character, ‘\0’, which
has a numerical value of 0.
String Variable
Example:
main()
{
char name[81];
printf(“ Enter your name: “);
scanf(“%s”,name);
printf(“Hello, %s. “,name);
getch();
}
Note:
Since a string name is an address, no address operator need
precede it in a scanf() function.
The String I/O Functions
There are many C functions whose purpose is to
manipulate strings. puts() and gets() are one of
them.
Example:
main()
{ char name[20];
puts(“ Enter your name: “);
gets(name);
puts(“Hello, “);
puts(name);
getch();
}
Using Character Arrays to Store and
Manipulate Strings
● We’ve discussed only integer arrays.
● However, arrays are capable of holding data of any type.
● We now discuss storing strings in character arrays. So
far, the only string-processing.
● A string such as "hello" is really an array of individual
characters in C.
■ Initializing a Character Array with a String
Initializing a Character Array with an
Initializer List of Characters
■ Character arrays also can be initialized with
individual character constants in an initializer
list, but this can be tedious. The preceding
definition is equivalent to
Arrays (Cont’d)
Characters Array or Strings
Column subscript
Array name
Row subscript
Multiple-Subscripted Arrays (cont’d)
■ Initialization
1 2
❑ int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
3 4
❑ Initializers grouped by row in braces
❑ If not enough, unspecified elements set to zero
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
■ Referencing elements 1 0
3 4
❑ Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );
Multiple-Subscripted Arrays (cont’d)
Here,
size1: Size of the first dimension.
size2: Size of the second dimension.
2-D Array Example
Multiple-Subscripted Arrays (cont’d)
Task-4
1st Column
■ Write a program to change the case of all the
alphabets in an array of strings.
2nd Column
■ Write a program that counts the no. of upper
and lower case letters in an array of strings
Matrix Addition
Scenario: You are given two matrices
representing the sales data of two different
products over the same time period. Each
matrix has dimensions 3 X 3. Write a C
program to perform the addition of these two
matrices and display the resulting matrix.
Task5 - Matrix Multiplication
How to store user input data into 1D array
How to store user input data into 2D array
Important
Increment operator (++) - Array
It is used to increment the value of a variable by 1
There two types of increment operators − pre increment
and post increment.
Increment operator is placed before the operand in
preincrement and the value is first incremented and then
operation is performed on it.
Exercise