arrays & string
arrays & string
4.1 Introduction
• Arrays
• An array is a collection of data elements that are of the
same type and share a common name
• (e.g., a collection of integers, collection of characters,
collection of doubles).
• Array Applications:
• Given a list of test scores, determine the maximum and
minimum scores.
• Read in a list of student names and rearrange them in
alphabetical order (sorting).
• Syntax:
<type> <arrayName>[<array_size>]
• The array elements are all values of the type <type>.
• The size of the array is indicated by <array_size>, the
number of elements in the array.
• <array_size> must be an int constant or a constant
expression. Note that an array can have multiple dimensions.
-- -- --
0 1 2 3 4 5 6 7 8 9
Ar -- -- -- 1 -- -- -- -- -- --
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9]
4.2 Arrays
Name of array (Note
that all elements of
this array have the
same name, c)
c[0] -45
c[1] 6
c[2] 0
c[3] 72
c[4] 1543
c[5] -89
c[6] 0
c[7] 62
c[8] -3
c[9] 1
c[10] 6453
c[11] 78
• Initializing arrays
– For loop
• Set each element
for (i=0; i<10; i++)
{ scanf(“%d”, &marks[i]);}
– Initializer list
• Specify each element when array declared
int n[ 5 ] = { 1, 2, 3, 4, 5 };
• If not enough initializers, rightmost elements 0
• If too many syntax error
– To set every element to same value
int n[ 5 ] = { 0 };
– If array size omitted, initializers determine size
int n[] = { 1, 2, 3, 4, 5 };
• 5 initializers, therefore 5 element array
2003 Prentice Hall, Inc. All rights reserved.
11
• Array size
– Can be specified with constant variable (const)
• const int size = 5;
#define size 5
– Constants cannot be changed
– Constants must be initialized when declared
– Also called named constants or read-only variables
– Array DECLARATION:
– int marks[size];
Or
int marks [size ]= {1, 3, 2, 48, 34)
main()
{
int num[5];
int sum, i;
float avg;
sum = 0;
for(i =0; i< 5;i++)
{
scanf(“%d”, &num[i]);
sum = sum + num[i];
}
avg = sum/5;
printf(“The average of numbers is %7.2’’, avg);
}
2003 Prentice Hall, Inc. All rights reserved.
14
To find the smallest number in an array
#define MAX 20
main()
{
int i, n, min, num[MAX];
printf(“\nHow many elements do you wish to enter ? :”);
scanf(“%d”, &n);
printf(“Enter elements of array :\n”);
for(i=0; i < n; i++)
scanf(“%d”, &num[i]);
min = num[0];
for (i = 1; i < n; i++)
{
if(num[i] <min)
min = num[i];
}
printf(“\nThe smallest element of the array is : %d”, min); }
2003 Prentice Hall, Inc. All rights reserved.
15
Program to input numbers into array a and then
copy them to array b
main()
{
int i, a[10], b[10];
for (i = 0; i< 10; i++)
{
printf(“Enter element:);
scanf(“%d”, & a[i]);
printf(“\n”);
}
for (i = 0; i< 10; i++)
b[i] = a[i];
for (i = 0; i< 10; i++)
printf(“%d\t%d\n”, a[i], b[i]);
}
• To sort n numbers
main()
{
int i, j, temp;
float num[50]; printf(“\nThe sorted array :\n”);
printf(“\nEnter elements of array :”); for(i=0;i<5;i++)
for (i=0; i<5; i++) printf(“%6.2f\n”, num[i]);
scanf(“%f”, &num[i]); }
for(i = 0; i< 4; i++)
{
for(j = i+1; j<5; j++)
{
if(num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;}
}
}
}
2003 Prentice Hall, Inc. All rights reserved.
18
Homework
Multidimensional arrays.
Strings
• Strings
- Arrays of characters enclosed within double
quotes
– All strings end with null ('\0')
– Examples
• char string1[] = "hello";
– Null character implicitly added
– string1 has 6 elements
• char string1[] = { 'h', 'e', 'l', 'l',
'o', '\0’ };
– Subscripting is the same
String1[ 0 ] is 'h'
string1[ 2 ] is 'l'
Which is correct?Why?
• 1 can be corrected as
• char color[4] = "RED";
main()
{
char name[15];
printf(“Enter your name :”);
scanf(“%s”, name);
printf(“Good Morning %s”, name);
}
char name[ 80 ] ;
scanf (“ %[ ^\n ] “ , name ) ;
4.strcpy()
char str1[10]= "awesome";
char str2[10]; char str3[10];
strcpy(str2, str1);
5.strcmp()
myFunction( myArray, 24 );
– Array size usually passed, but not required
Sorting Arrays
• Sorting data
– Important computing application
– Virtually every organization must sort some data
• Massive amounts must be sorted
• Bubble sort (sinking sort)
– Several passes through the array
– Successive pairs of elements are compared
• If increasing order (or identical), no change
• If decreasing order, elements exchanged
– Repeat these steps for every element
#define MAX 20
main()
{
int i, n, min, num[MAX];
printf(“\nHow many elements do you wish to enter ?:”);
scanf(“%d”, &n);
printf(“Enter elements of array :\n”);
for(i=0; i < n; i++)
scanf(“%d”, &num[i]);
bubbleSort(num, n);
printf(“Sorted array :\n”);
for(i=0; i < n; i++)
printf(“\n %d”, num[i]); }
• Mean
– Average (sum/number of elements)
• Median
– Number in middle of sorted list
– 1, 2, 3, 4, 5 (3 is median)
– If even number of elements, take average of middle two
• Mode
– Number that occurs most often
– 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)
Selection sort