Q.1 Multiple choice questions.
- An array is a ____________________ structure.
a) loop
b) control
c) data (Correct Answer)
d) conditional
2. Array elements are stored at ____________________ memory locations.
a) contiguous (Correct Answer)
b) scattered
c) divided
d) none
3. If the size of an array is 100, the range of indexes will be ____________________.
a) 0-99 (Correct Answer)
b) 0-100
c) 1-100
d) 2-102
4. ____________________ Structure allows repetition of a set of instructions.
a) Loop (Correct Answer)
b) Conditional
c) Control
d) Data
5. ____________________ is the unique identifier, used to refer to the array.
a) Data type
b) Array name (Correct Answer)
c) Array size
d) None
6. Array can be initialized ____________________ declaration.
a) at the time of (Correct Answer)
b) after
c) before
d) both a & b
7. Using loops inside loops is called ____________________ loops.
a) For
b) While
c) Do-While
d) Nested (Correct Answer)
8. ____________________ Part of for loop is executed first.
a) Condition
b) Body
c) Initialization (Correct Answer)
d) Increment/Decrement
9. ____________________ make it easier to read and write values in array.
a) Loops (Correct Answer)
b) Conditions
c) Expressions
d) Functions
10. To initialize the array in a single statement, initialize it ____________________ declaration.
a) at the time of (Correct Answer)
b) after
c) before
d) both a & b
Q.2 Define the following.
1) Data Structure
Data structure is a container to store collection of data items in a specific layout.
2) Array
An array is a data structure that can hold multiple values of same data type, e.g. an int array can hold multiple integer values, a float array can hold multiple real values and so on.
3) Array Initialization
Assigning values to an array for the first time, is called array initialization. An array can be initialized at the time of its declaration, or later. Array initialization at the time of declaration can be done in the following manner.
Data_type array_name[N] ={value1, value2, value3, ………, valueN};
Float height[5] = {5.7, 6.2, 5.9, 5.0,6.2};
4) Loop Structure
If we need to repeat one or more statements, then we use loops. For example, if we need to write Pakistan thousand times on the screen, then instead of writing printf(“Pakistan”); a thousand time, we use loops. We write only 3 or 4 lines of code and we get thousand time Pakistan printed on the screen.
5) Nested Loops
A loop inside another loop is called nested loop.
Q.3 Briefly answer the following questions.
1) Is loop a data structure? Justify your answer.
Answer:
No, loop is not a data structure. Loop is programming construct which runs until and the provided condition is true.
2) What is the use of nested loops?
Answer: Use of Nested Loops
When we want to repeat a pattern for multiple times, we use nested loops. For example, if we want to display the word “Pakistan” 10 times then we can do this by writing the code of displaying the word “Pakistan” in loop that runs 10 times.
3) What is the advantage of initializing an array at the time of declaration?
Answer: Advantages of initialization of an array
The main advantage of array initialization at the time of declaration is to reduce the line of codes, memory, and time. Array initialization can be done on the same line at the time of declaration, but if we did not initialize it at declaration time then we initialize each element of array one by one.
4) Describe the structure of for loop.
Answer: Structure of for loop
In C programming language, we can see the structure of for loop as below.
for(initialization; condition; increment/decrement)
{
Code to repeat
}
In the above syntax,
- Initialization is the first part to be executed in for loop. Here we initialize our counter variable and then move to the condition part.
- Condition is checked, and if it turns out to be false, then we come out of the loop.
- If condition is true, then body of the loop is executed.
After executing body of the loop, the counter variable increases or decreases depending on the logic used and again we move to the step 2.
5) How can you declare an array? Briefly describe the three parts of array declaration.
Answer: Array Declaration
In C language, an array can be declared as follows:
data_type array_name[array_size]
Where
- Data_type: type of data to be stored in array
- Array_name: identifier of array
- Array_size: maximum number of elements that can be stored
Example: following is the declaration of a float type array that holds marks of 20 students.
float marks[20];
Programming Exercises
Exercise 1 Part a: Use loops to print the following output on console.
*****
*****
*****

Output

Exercise 1 Part b: Use loops to print the following output on console.


Output

Exercise 2: Write a program that takes two positive integers a and b as input and displays the value of ab.

Output

Exercise 3: Write a program that takes two numbers as input displays their Greatest Common Divisor(GCD) using Euclidean method.

Output

Exercise 4: Write a program to display factorial numbers from 1 to 7. (Hint: Use nested loops)

Output

Exercise 6: Write a program that declares and initializes an array of 7 elements and tell how many elements in the array are greater than 10.

Output


10th Class Computer Unit-05: Functions
10th Computer New Course Conditional Logic Unit-03
10th Class Computer New Course User Interaction Unit-02
10th Class Computer New Course ITP Unit-01
9th Computer New Course Designing Website Unit-05
9th Computer New Course Data and Privacy Unit-04
9th Computer New Course Networks Unit-03
9th Computer New Course Binary System Unit-02
9th Computer New Course Problem Solving Unit-01
Buy Best Stationery Products in Pakistan
For more stationery items please visit : https://stationerystore.pk