Sie sind auf Seite 1von 8

Lab 12

ARRAYS
(Part I)
Learning Outcomes
After completing this lab, you will be able to:
o know how to create arrays
o know how to initialize array elements and to use arrays

A. PRE-LAB ACTIVITIES
Question 1
Find the error(s), if any in each of these statements:

a. float a, b(4);
b. int s1234n, 88abc;
c. void student;
d. double number[-20];
e. int 4[a];

Question 2
Suppose array a is int a[] = {1, 2, 3}, what is the result of a - a?

Question 3
An array can be used in which of the following situations?

SITUATION YES NO
As a local variable
As a parameter of a function
As a return value of a function

Question 4
Which of the following are valid array declaration statements?

STATEMENT YES NO
char charArray;
int words;
char charArray[] = "Computer Science";
float nums[] = {‘3.5’, 35.1, 32.0};

Question 5
Given the following statement

int list;

What is the length of the array?

Question 6
Find the error(s), if any in each of this program.

main()
{
const double N = 2;
double array1[N], array2;
array1 = N;
N = 99;
array2 = N;
}

Question 7
Find the error(s), if any in each of these statements:

a. float numberA = {23, 34}, number;

b. int number = {1,2,3,5,8};
c. double numberD(3) = (4, 10, 55);
d. numberD = {43 3 23 12};
e. int numberA[]= {12, 34, 22};

B. LAB ACTIVITIES
Exercise 1
Write a program based on the following requirements.

a. Declare an array to store CGPA for 5 students.

b. Write the statement to initialize array elements all students’ CGPA.
c. Write the statement to print all elements in the array.

d. Write code to get the address of the first element of the array into the integer.
Use address (&) operator.

Exercise 2

Use the following declaration statement and requirements below. Write a program to
accept ‘N’ student details such as student id and current semester ant to count the
number of student with particular semester.

#define ARRAYSIZE 20

main()
{
int studId[ARRAYSIZE];
char studCurSem[ARRAYSIZE];
int numStud;
char cCurSem;
}

Program 12.1

a. accept the numStud from the user.

b. use a for loop to accept the arrays studId and studCurSem.
c. accept cCurSem.
d. Use for loop and check whether any current semester in the array is equal to
cCurSem.
e. If so, increment the count of number of student in cCurSem.
f. display the number of cCurSem.
Exercise 3
Write a program based on the following requirements.

a. Write the declaration an integer array to store the department codes. Make the
size of the array as 10 to accommodate maximum of the departments.
b. Populate the array with sample data.
c. Maintain another integer array to store telephone extensions. Populate the array
with sample data. There is a one to one relationship between two arrays.
d. Write a function that accepts the department code. check whether that is a valid
department code. If it is valid then display the corresponding telephone extension.
If invalid, display the corresponding error message.
e. Write the function prototype for the function. Include it before function main().
g. Call the function.
h. Write the code in main() the user should give a choice asking whether he wants
to continue.
Name :

Student ID :

Date :
Name :

Student ID :

Date :

C. POST-LAB ACTIVITIES
Question 1

Write a program that uses arrays to store the following data:

1 30.0
2 45.7
3 60.0
4 30.5

and then process the data and generate the following output:

1 30.0 900.0
2 45.7 2088. 49
3 60.0 4200.0
4 30.5 930.25