C7 - Array I (Students)
C7 - Array I (Students)
CSC099:
FOUNDATION COMPUTING II
CHAPTER 7 - ARRAY
PART I
Declaring Arrays
Concept of Arrays
Input, Output of an array
Exchanging elements of an array
Application in Array
String
#include<stdio.h>
int main() {
int mark1, mark2, mark3, sum, average; The inputs are
all marks.
printf("Enter mark 1: ");
scanf("%d", &mark1);
printf("Enter mark 2: "); Can be associated
scanf("%d", &mark2); in one group of
printf("Enter mark 3: "); name marks
scanf("%d", &mark3);
return 0;
}
Using Array…..
4
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
Syntax
component_type identifier_name[size];
Declaration and definition tells the compiler the name of the array, type of
each element and size or number of element in the array to be reserved in
the memory.
Example
float studentsCGPA[40];
Let say we are going to keep CGPA of 40 students in our class.
Thus, we need an array of the mentioned size (i.e: 40) to store the value of
CGPA.
Example: Array Declaration
11
Syntax
component_type identifier name[sizeOfTheArray] =
{list_of_values; equals_to_the_size_of_the_array,
separated_by_comma}
Example
Array Initialization: Different Ways
13
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.
Usually done by using a for loop.
How to display the content of an
15
array?
for (i=0; i<3; i++)
printf("\nElement [%d], the value is %d ",i, score[i]);
** NOTE :
Usually done by using a for loop.
How to assign values into array?
16
** 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
String
String (Characters in Array)
19
Example
Example,
char ayat[ 20 ];
Input Statement
printf(“Enter a string : ”);
scanf( "%s", ayat);
By using the conversion specifier %s , it reads a string from the
keyboard.
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 )
min = score[i]; The minimum score is
}
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
CORRECT
Example 3: To Swap Values in Array (cont’d)
45
temp = numbers[3];
numbers[3] = numbers[1];
numbers[1] = temp;
Very Difficult :*
Difficult :*
.. frequency[4]
Moderate :***
.. frequency[3] Not Difficult :*
.. frequency[2] Very Not Difficult :****
..
frequency[1]
response[1] frequency[0]
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