5
5
Computer Programming
(CSC425)
Chapter 5: Array
For example:
• float temp[5] = to store 5 float value in temp.
• int max[4] = to store 4 integer value in max
• char y[20] = to store a string of value
One Dimensional Array (continued)
//using looping:
for(int i=0; i<5; i++)
cout << num[i];
One Dimensional Array (continued)
char codes[6]=“sample”;
Note : a string is terminated with a special sentinel,“\0”
the null character.
One Dimensional Array (continued)
NOTE:
• If what we declare doesn’t fulfill what the size of the array wants, for example:
int max[6]={1,2,3}
• we only declare three but the array wants 6; the compiler will assume the next
three integer are zero :
“ 1,2,3,0,0,0 “
• If what we declare exceeds what the array wants, for example:
int max[3]={1,2,3,4,5,6,7,8}
• the array wants three but we declare 8” the compiler will only take the first
three integer :
“1,2,3”
One Dimensional Array (continued)
List[3] = 10;
List[6] = 35;
List[5] = list[3] + list[6];
One Dimensional Array (continued)
• This for loop steps-through each element of the array list starting at the
first element
• The symbol & is not used when declaring an array as a formal parameter.
• The reserved word const in the declaration of the formal parameter can
prevent the function from changing the actual parameter
One Dimensional Array (continued)
Example :
void fillArray(int list[], int listSize) //Function to find and return the index of the first largest element in an int array.
{ //The parameter listSize specifies the number of elements in the array.
int index;
for (index = 0; index < listSize; index++) int indexLargestElement(const int list[], int listSize)
cin >> list[index]; {
} int index;
int maxIndex = 0; //assume the first element is the largest
//Function to print the elements of an int array. The array to be printed and 520 | Chapter 8: Arrays and Strings
//the number of elements are passed as parameters. The parameter listSize for (index = 1; index < listSize; index++)
//specifies the number of elements to be printed. if (list[maxIndex] < list[index])
maxIndex = index;
void printArray(const int list[], int listSize)
{ return maxIndex;
int index; }
for (index = 0; index < listSize; index++)
cout << list[index] << " ";
//Function to copy some or all of the elements list2[], int tar, int numOfElements)
//of one array into another array. Starting at {
//the position specified by src, the elements for (int index = src; index < src +
//of list1 are copied into list2 starting at numOfElements; index++)
//the position specified by tar. The {
//parameter numOfElements specifies the list2[index] = list1[tar];
//number of elements of list1 to be copied into tar++;
//list2. Starting at the position specified by }
//tar, the list2 must have enough components }
//to copy the elements of list1. The following
//call copies all of the elements of list1
//into the corresponding positions in list2:
• The statement
char name[16] = "John";
declares a string variable name of length 16 and stores
"John" in it
• The statement
char name[] = "John";
declares a string variable name of length 5 and stores "John"
in it
C Strings (Character Arrays) (continued)
String Comparison
1. C-strings are compared character by character using the
collating sequence of the system.
2. If we are using the ASCII character set
1. The string "Air" is smaller than the string "Boat“.
2. The string "Air" is smaller than the string "An“.
3. The string "Bill" is smaller than the string "Billy“.
4. The string "Hello" is smaller than "hello“.
char studentName[21];
char myName[16];
char yourName[16];
Statement Effect
To read strings with blanks, use the get function with an input
stream variable, such as cin:
cin.get(str, m+1);
• Stores the next m characters into str but the newline character is not
stored in str.
• If the input string has fewer than m characters, the reading stops at the
newline character
Reading and Writing Strings (continued)
String Output
strcpy(str2, str1);
int main()
cout<<"str2 consist of: "<<str2;
{
char str1[30];
return 0;
char str2[15];
}
cin>>str1;
cout<<str1[15];
cout<<"Length of str1 is:
"<<strlen(str1)<<endl;
References
• C++ Programming Program Design Including Data Structures,
Eighth Edition, D.S.Malik
• Jeri R. hanly, Essential C++ for Engineers and Scientists, 2nd
Edition, Addison Wesley
• J. Glenn Brookshear, Computer Science An Overview, 8th
Edition, Pearson Addison Wesley