78 68

advertisement
Atılım University
Computer Engineering Department
COMPE 112
Computer Programming in C
Lab – 10
There are 5 exercises in this laboratory session. The activities cover the following:





Declaring two-dimensional array
Manipulation of a 2D array
Input and output of the two-dimensional arrays
Passing two-dimensional arrays to functions
String processing
***************************************************************************
Activity 1:
Write a C program that
 Declares a two-dimensional integer array A of size 4x4 (i.e. int A[4][4]; ).
 Sets
o First column entries to 1,
o Second column entries to 2,
o Third column entries to 3,
o Fourth column entries to 4,
 Output this array A as row-wise.
The Generated array A will be;
1
2
3
4
1
2
3
4
1
2
3
4
1
2
3
4
Sample Run:
1234
1234
1234
1234
Press any key to continue . . .
**************************************************************************
1
Activity 2: Rewrite the program of Activity 1, so that to create an identity matrix from matrix
A, that is;
 All entries on the main diagonal will be 1
 All other entries will be 0.
 Output as column-wise
The Generated array A will be;
1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1
Sample Run:
1000
0100
0010
0001
**************************************************************************
Activity 3: Write a C program that declares a two-dimensional integer array with size 5x5 i.e.
int arr[5][5].

Input the numbers (i.e. positive and negative integers) for this array

Count the number of positive and negative number

Output the whole array in matrix view and the number of positive and negative
integers.
Sample Run:
Enter 25 integers: 1 -5 6 9 -7 65 -98 25 63 74 -98 -78 -68 32 52 48 68 25 12 47 35 -6 -21 -36
58
The content of the array
1
-5
6
9 -7
65 -98 25 63 74
-98 -78 -68 32 52
48 68
25 12 47
35 -6 -21 -36 58
2
There are 16 positive integers
There are 9 negative integers
***************************************************************************
Activity 4: Rewrite the program of activity 3. Write a function count() that receives the
arr[5][5] array to count the number of positive and negative number. The counted numbers
will be returned to the calling function. (i.e. use pointers)

Input the numbers (i.e. positive and negative integers) for this array in the main
function

Call the function count()

Output the whole array in matrix view and the returned values from the function
count() that is the number of positive and negative integers.
Sample Run:
Enter 25 integers: 1 -5 6 9 -7 65 -98 25 63 74 -98 -78 -68 32 52 48 68 25 12 47 35 -6 -21 -36
58
The content of the array
1
-5
6
9 -7
65 -98 25 63 74
-98 -78 -68 32 52
48 68
25 12 47
35 -6 -21 -36 58
There are 16 positive integers
There are 9 negative integers
**************************************************************************
3
Activity 5: Write a C program that

Declares two character array strA and strB (i.e. string) with size 20,

Assigns the string “Hello world!” to strB at compile-time,
Using string library functions

copy the content of strB to strA

output the content and the length of strA

input a new string to strB using scanf() function

output the content and the length of strB

input a new string to strB using gets() function

output the content and the length of strB

copy the first 5 characters of strA to strB

output the content and the length of strB

concatenate the srtA to strB

output the content and the length of strB
Sample Run:
strA has Hello world! with lentgh 12
Enter a string for strB: seker 12345
strB has seker with lentgh 5
Enter a string for strB: seker 12345
strB has seker 12345 with lentgh 11
strB has Hello 12345 with lentgh 11
strB has Hello 12345Hello world! with lentgh 22
***************************************************************************
4
Download