Week 10
Week 10
Introduction
week 10
10:: Arrays
Lecturer : Cao Tuan Dung
Dept of Software Engineering
Hanoi University of
Technology
For HEDSPI Project
Arrays
• A block of many variables of the same
type
• Array can be declared for any type
– E.g. int Arr[10] is an array of 10 integers.
• Examples:
– list of students’ marks
– series of numbers entered by user
– vectors
– matrices
Arrays in Memory
• Sequence of variables of specified type
• The array variable itself holds the address
in memory of beginning of sequence
• Example:
int arr[10]; 0 1 2 3 4 5 6 7 8 9
arr
int main()
{
int s[ arraySize ]; // array S has 10 elements
int i;
for ( i = 0; i < arraySize; i++ )
s[ i ] = 2 + 2 * i;
printf("Element \t Value\n");
for ( i = 0; i < arraySize; i++ )
printf("%d\t%d\n", i, s[i]);
return 0;
}
Data input and output for
array
• Running a for loop, month rainfall (in mm)
1 40
in each loop: 2 45
– use data input 3 95
functions like scanf 4 130
5 220
• or 6 210
7 185
– use data output 8 135
functions like printf 9 80
10 40
11 45
12 30
table of rainfall
Example
#include <stdio.h>
#define MONTHS 12
int main()
{
int rainfall[MONTHS];
int i;
int main()
{
int a[MAX];
int i,s,min;
for ( i = 0; i < MAX; i++ ){
printf("a[%d]=",i);scanf("%d", &a[i]);
}
s=0; min=a[0];
for ( i = 0; i < MAX; i++ ){
if (a[i] < min) min = a[i];
if (a[i]%2==1) s= s+ a[i];
}
printf("\nMinimum value in the array: %d",min);
printf("Sum of all odd numbers: %d",s);
return 0;
}
Exercise 10
10..2
• Given an array of which elements are
the numbers inputed by user. Find
the sum of the local maximum
numbers in this array (local
maximum element is the element
that greater than its two neighbours)
Solution
.. // Array data input
sum=0;
for (i=1; i<size-1; i++)
if (a[i]>=a[i-1] && a[i]>=a[i+1])
sum +=a[i];
Arrays as function
arguments
• Functions can accept arrays as
arguments
• Usually the array’s size also needs to
be passed (why?)
Arrays as function
arguments
• For example:
int calc_sum(int arr[], int size);
• Within the function, arr is accessed
in the usual way
• Changes in the function affect the
original array!!
Example
int calc_sum(int arr[], int size)
{
int i = 0;
int sum = 0;
return sum;
}
Exercise 10.3
• Implement a function that accepts
two integer arrays and returns 1 if
they are equal, 0 otherwise
• Write a program that accepts two
arrays of integers from the user and
checks for equality
Solution
int compare_arrays(const int arr1[], const int
arr2[], int size)
{
int i = 0;
count=0;
for (i=1; i<n; i++)
if (a[i] ==0)
count++;
Solution for question b)
int max=0,temp=0;
for(i=0;i<n;i++)
{
if(a[i]==0)
temp=temp+1;
if(a[i]!=0){
if(temp>max){
max=temp;
temp=0;
}
}
}
if (temp>max) max=temp;
printf("Do dai day con bang 0 lon nhat
la:", max);
Solution for question c)
Firstly we sort the array, then count the
number of equal elements just after
each element.
#include<stdio.h>
#define SIZE 3
int main()
{
int A[][SIZE] = {{1,2,3}, {4,5,6}, {7,8,9}};
int B[][SIZE] = {{1,1,1}, {2,2,2}, {3,3,3}};
int C[SIZE][SIZE];
int i = 0, j = 0;
return 0;
}
Exercise 10.6
• Write a program that defines 3
matrices A,B,C of size 3x3 with int
elements; initialize the first two
matrices (A and B)
• Compute the matrix multiplication of
A and B and store it in C (i.e. C =
A*B)
• Print all the matrices on the screen
Solution
#include <stdio.h>
void main() {
float a[3][3], b[3][3], c[3][3];
int m,n,p;
int i,j,k;
float temp;
for(i=0; i<3; i++){
for (j=0; j<3; j++) {
printf("a[%d][%d]=", i+1, j+1);
scanf("%f", &temp);
a[i][j]= temp;
printf("b[%d][%d]=", i+1, j+1);
scanf("%f", &temp);
b[i][j]= temp;
}
}
...
Solution
for(i=0; i<3; i++){
for (j=0; j<3; j++) {
c[i][j]=0;
for (k=0; k<n; k++)
c[i][j]= c[i][j]+ a[i][k]*b[k][j];
}
}
printf("\n Matrix A:");
for(i=0; i<3; i++){
printf("\n");
for (j=0; j<3; j++) {
printf("%2.2f\t", a[i][j]);
}
}
...
Solution
printf("\n Matrix b:");
for(i=0; i<3; i++){
printf("\n");
for (j=0; j<3; j++) {
printf("%2.2f\t", b[i][j]);
}
}
printf("\n Matrix c:");
for(i=0; i<3; i++){
printf("\n");
for (j=0; j<3; j++) {
printf("%2.2f\t", c[i][j]);
}
}
}
Exercise 10
10..7
• Input an array with the number of
element n asked from user. Check to
see whether the array is symmetric.
Solution
#include <stdio.h>
3. main()
4. {
5. int i;
6. int d1, d2;
7. int a[13]; /* uses [2..12] */