Académique Documents
Professionnel Documents
Culture Documents
UNIT 6
elf © 2015
Topics
• Syntax Declaration:
<data_type> array_name[size_of_array] = {optional
initialization data};
1 /* Filename: SumArray.c
Program Description: Ask 5 integers and compute the sum.
Programmer: Pedro de la Cruz
Date Written: May 29,2005
*/ Sample Output:
2 #include<stdio.h> for(x=0; x<5; x++) 11
Enter a number: 3
3 int main(void) sum += num[x]; 12
4 { int num[5]; 13
Enter a number: 1
5 int x, sum=0; printf(“\nSum=%d”, sum); 14 Enter a number: 2
6 15 Enter a number: 5
7 for(x=0; x,5; x++) } 16 Enter a number: 2
8 { printf(“Enter a number: “);
Sum = 13
9 scanf(“%d”, &num[0]);
10 }
One-Dimensional Array
• Example
1) Make a program that will ask 10 integers and
determine how many of the inputs are odd or
even numbers.
• Seatwork
1) Make a program that will ask 10 integers and
determine how many numbers are positive,
negative or zero.
1 /* Filename: Function1.c
Program Description: Ask 4 characters, then make it a string.
Programmer: Pedro de la Cruz Sample Output:
Date Written: May 29,2005
Enter a character: a
*/
Enter a character: b
2 #include<stdio.h> str[x]= ‘\0’; 11 Enter a character: c
3 int main(void) 12 Enter a character: d
4 { char str[5], ch; printf(“\nString: %s”, str); 13 String: abcd
5 int x; } 14
6 for(x=0; x<5; x++) { 15
7 printf(“\nEnter a character:”); 16
8 ch=getche( ); 17
9 str[x]=ch; 18
10 } 19
Strings
Sample Program : Ask A String Using A Standard Function
1 /* Filename: Function1.c
Program Description: Ask a string using scanf or gets.
Programmer: Pedro de la Cruz
Sample Output:
Date Written: May 29,2005
*/
1 /* Filename: StringCopy.c
Program Description: Example of strcpy and strncpy.
Programmer: Pedro de la Cruz
Date Written: May 29,2005 Output:
*/
1 /* Filename: StringLength.c
Program Description: Example of strlen
Programmer: Pedro de la Cruz
Date Written: May 29,2005 Sample Output:
*/
• Seatwork
• Ask a string and print it backwards.
2 9 3 1 0
Two-Dimensional Array
Sample Program : Fill a Two-dimensional Array
1 /* Filename: Function1.c
Program Description: Ask 6 integers and place it in a 2x3 array.
Programmer: Pedro de la Cruz
Date Written: May 29,2005 Sample Output:
*/
Enter a number: 1
2 #include<stdio.h> for(row=0; row<3; row++) 11
Enter a number: 10
3 int main(void) { 12
Enter a number: 2
4 { int x[2][3]; for(col=0; col<2; col++) 13
Enter a number: 20
5 int row, col; { printf(“Enter a number: “); 14
Enter a number: 3
6 scanf(“%d”, &num[row][col]); 15
Enter a number: 30
7 } 16
8 } 17
9 } 18
10 19
Two-Dimensional Array
Sample Program : Array of Strings
1 /* Filename: StrArray.c
Program Description: Array of Strings
Programmer: Pedro de la Cruz
Date Written: May 29,2005 Sample Output:
*/
2 #include<stdio.h>
3 main() 0 1 2 3
4 { int table[3][3][4]= { { /*Plane 0*/ {0,1,2,3}, {1,2,3,4}, {2,3,4,5} } }; 1 2 3 4
5 int plane, row, col; 2 3 4 5
6
7 for(plane=0;plane<1; plane++)
8 {
9 for(row=0; row<3; row++)
10 {
11 for(col=0; col<4; col++)
12 printf(“\t%d”, table[plane][row][col};
13 printf(“\n”);
14 }
15 }
16 }
References
Computer Science: A Structure Programming Approach in C ,
Behrouz A. Fourozan and Richard F. Gilberg