Chapter 7 - Array I
Chapter 7 - Array I
CSC099:
FOUNDATION COMPUTING II
CHAPTER 7 -
ARRAY
PART
Declaring Arrays
Concept of Arrays
I
Input, Output of an array
Exchanging elements of an array
Application in Array
String
#include<stdio.h>
int main() {
int mark1, mark2, mark3, sum, The inputs are
average; all marks.
An array:
is a sequenced collection of
elements/related data items of the same
name and the same type.
serves as an example of structured data types
- they are effectively just lists of variables all of
the same data type ("int", "char" or whatever).
Arrays are “static” entities in that
they remain the same size throughout
program execution.
Array Structures
6
Difference between:
a. 7th element
b. Array element 7
8
Read or Print
using index
input,
displaying
content and
Syntax: Array Declaration
10
Syntax
component_type identifier_name[size];
Example
float studentsCGPA[40];
Syntax
component_type identifier
name[sizeOfTheArray] = {list_of_values;
equals_to_the_size_of_the_array,
separated_by_comma}
Example
Array Initialization: Different
13
Ways
Basic Initialization
int number[5] = {3, 6, 9, without Size
12, 15}; int number[ ] = {3, 6, 9};
int score[3];
for (i=0; i<3; i++)
score[i]=0;
int mark[3];
for (i=0; i<3; i++)
{
printf("Enter mark [%d] : ",i);
scanf("%d", &mark[i]);
** NOTE :
The & is used to provide scanf with a variable’s
location in memory so that a value can be stored
there.
How to display the content of an array?
15
** NOTE :
Usually done by using a for loop.
How to assign values into
16
array?
Wrong
int mark[3] = {10,20,30}, new_mark[3]; Way !!!
new_mark = mark; // copy all elements from mark to mark_new
** NOTE :
values can be assigned by using assignment
operator
Correct Way
int mark[3] = {10,20,30}, new_mark[3];
int i;
for(i=0; i<3; i++)
Test Your Understanding 1
17
and 21
3. Display the content of the array age
String
String (Characters in Array)
19
Example,
char ayat[ 20 ];
Input Statement
printf(“Enter a string : ”);
scanf( "%s", ayat);
return 0;
}
#include <stdio.h>
#define SIZE 20
int main()
{
char message[SIZE];
printf("Enter a string : ");
return 0;
}
String Data Manipulation
28
Manipulate Data
Moving a character – use assignment
Example: name[0]=’J’; name[1]=’o’;
name[2]=’h’; name[3]=’n’;
Moving a string – use function call to read
string.
Remember! Below is error if you are using
string to copy to a variable.
Mystring = “Hello”;
String Function
29
2) strcpy(CarName1, CarName2);
printf("Car Name: %19s“, CarName1) ;
Output :
String Function - strlen
31
length = strlen(CarName);
printf("Car Name: %d“, length);
Output :
String Function - strcat
32
strcat(faculty, university);
printf("\n\nAfter concatenating,
%19s\n",faculty);
Output:
String Function - strcmp
33
0 : if str1 == str2
if(i == 0)
{
printf(“You are from ASASI”);
}
String Comparison (strcmp)
34
Examples of
Array
Application
Example 1: To Add Values in
Array
#include
41 <stdio.h>
int main(void)
{
int i,
//array initialization and declaration
int susun1[5]={2,4,6,8,10}, susun2[5], susun3[5];
Output :
printf(“Enter 5 numbers: ”); Enter 5 numbers:1 2 3 4 5
}
}
Example 2: To Determine the
Minimum value
42 #include <stdio.h>
int main(void)
{
int i, score[5], min; Output :
Enter 5 scores:
printf(“Enter 5 scores:\n”);
min = score[0];
for(i=1;i<5;i++)
{
if(score[i]< min ) The minimum score is
min = score[i];
}
printf(“\nThe lowest score is %d”, min);
}
Example 3: To Swap Values in
Array
43
WRONG !
!!
Example 3: To Swap Values in Array (cont’d)
44
CORREC
T
Example 3: To Swap Values in Array (cont’d)
45
temp = numbers[3];
numbers[3] = numbers[1];
numbers[1] = temp;
printf(“After exchange the value :\n“);
for(i=0; i<5; i++)
printf(“%d”, ‘ ’, numbers[i]);
Example 4: To Produce Frequency Distribution
and Histogram
46
Very Difficult :*
Difficult :*
.. frequency[4
]
Moderate :***
.. frequency[3 Not Difficult: *
.. ]
frequency[2 Very Not Difficult :**
.. ] **
frequency[1
]
frequency[0
response[1
]
]
response[0 frequency
]
response
Example 4: To Produce Frequency Distribution
and Histogram (cont’d)
48
#include <stdio.h>
#define SIZE 10 // set the size of frequency
#define SCALE 6 // set the size of scale
int main()
{
int response[SIZE] = {1,2,1,1,2,1,3,5,4,4}; //initial values
int frequency[SCALE] = {0}; // set frequency to all zeros
int answer;
for(answer=0;answer<10;answer++) // looping for frequency
++frequency[response[answer]];// increase answer by 1 and increase response by 1
int rating;
for(rating=1; rating<=SCALE-1;rating++)
printf("\n%d %10d \n", rating, frequency[rating]);
// to display the current value of rating
// and to display the value of current frequency
}
Test Your Understanding 3
49
temp = numbers[3];
numbers[3] = numbers[1];
numbers[1] = temp;
printf("After exchange the value :\n");
int j;
for(j=0; j<5; j++)
printf("\nElement [%d] : %d",i, numbers[j]);
printf("\n");
}
Conclusion
51