10th Class Computer Notes CH 4
10th Class Computer Notes CH 4
4.1.1 Array
Loop Structure
* EXERCISE 117
COMPUTER SCIENCE-10 98
Chapter– 4 Data and Repetition
SHORT QUESTIONS
Q.1 Define Data Structure. (K.B)
Ans: Data structure is a container to store collection of data items in a specific layout.
Q.2 How many forms do data structure have? (K.B)
Ans: There are generally four forms of data Structures:
Linear: arrays, lists.
MULTIPLE CHOICE QUESTIONS
1. _________ structure is a container to store data items. (K.B)
(A) Data (B) Control (C) Selection (D) Loop
2. Data structure is a container to store data in ________ layout. (K.B)
(A) Specific (B) Irregular (C) Alternative (D) None of these
3. __________ is type of data structure: (K.B)
(A) Array (B) List (C) Both A & B (D) None of these
4.1.1 ARRAY
4.1.2 ARRAY DECORATION
4.1.3 ARRAY INITIALIZATION
4.1.4 ACCESSING ARRAY ELEMENTS
4.1.5 USING VARIABLES AS ARRAY INDEXES.
LONG QUESTION
1. Briefly Explain Array in detail. (K.B+U.B)
Ans: Definition
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. An important property of array is that it stores all the values at consecutive locations
inside the computer memory.
Array Declaration:
In C language, an array can be declared as follows:
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,……, value N};
Char vowels [5] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}
If we do not initialize an array at the time of declaration. Then we need to initialize the
array elements one by one. It means that we cannot initialize all the elements of array in a
single statement.
Elements of Array:
COMPUTER SCIENCE-10 99
Chapter– 4 Data and Repetition
Each element of an array has an index that can be used with the array name as array-
name [index] to access the data stored at that particular index.
First element has the index 0, second element has the index 1 and so on. Thus height [0]
refers to the first element of array height, height [1] refers to the second element and so
on. Figure shows graphical representation of array height initialized in the last section.
Example:
Write a program that stores the ages of five persons in an array, and then displays on
screen.
Solution:
#include<stdio.h>
void main()
{
int age [5];
/* Following statements assign values at different indices of array age, we can see that the
first value is stored at index 0 and the last value is stored at index 4 */
age [0] = 25;
age [1] = 34;
age [2] = 29;
age [3] = 43;
age [4] = 19;
/* Following statement displays the ages of five persons stored in the array *\
printf(“The ages of five persons are: %d, %d, %d, %d, %d”, age [0], age [1], age
[2], age [3], age [4]);
SHORT QUESTIONS
Q.1 Define Array. (K.B)
Ans: 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. An important property of array is that it stores all the values at consecutive
locations inside the computer memory.
Data_type array_name[N] = {value1, value2, value3,……, value N};
Q.2 Draw a diagram to show array declaration. (K.B+U.B+A.B)
Ans:
Flowchart:
In order to understand the for loop structure let’s look at the following flow chart.
Using loops, we can easily take input in arrays. If we want to take input from user in an
array of size 10, we can simply use a loop as follows:
Example:
int a [10];
for (int i =0, i < 10; i++)
scanf(“%d”, &a[i]);
2. Reading values from Arrays using Loops:
Loops help us in reading the values from array. The following code can be used to
display the elements for an array having 100 elements:
Example
for (int i = 0; i < 100; i++)
printf(“%d”, a[i]);
SHORT QUESTIONS
Q.1 Define Loop. (K.B)
Ans: “Loop is a control structure that repeats a statement or set of statements up to a fix
number of times of until a specific condition is satisfied”
Q.2 Write the names of different types of loops. (K.B)
Ans: C language provides three kind of loop structure:
1. for loop
2. while loop
3. do while loop
Q.3 Define ‘for’ Loop. (K.B)
Ans: “for" loop is a type of loop which keeps on repeating a statement or a set of statements
up to a fixed number of times”
Q.4 Write down the syntax of ‘for’ loop. (K.B+U.B)
Ans: In C programming language, ‘for’ loop has the following general syntax.
for (initialization; condition; increment/decrement)
{
Code to repeat
}
Q.5 Draw flowchart to explain ‘for’ loop. (K.B+U.B+A.B)
Ans:
Q.6 Write a program to print “Pakistan” three times using ‘for’ loop. (A.B)
Ans:
for(int i = 0; i < 3; i++)
{
COMPUTER SCIENCE-10 103
Chapter– 4 Data and Repetition
printf(“Pakistan\n”);
}
Q.7 Write a program to display the values from 1 to 10 on screen. (A.B)
Ans:
for (int i = 1; i < = 10; i++)
{
printf(“%d\n”, i);
}
Q.8 Write a program that displays the table of 2. (A.B)
Ans:
#include <stdio.h>
void main()
{
int n=2,j;
for(j=1;j<=10;j++)
{
printf("%d X %d = %d \n",n,j,n*j);
}
}
Q.9 What will happen if condition don’t return false in any case of ‘for’ loop? (K.B+U.B)
Ans: If the condition in loop do not gets false at some point then the loop repeats for infinity
and never terminates
Q.10 Define iteration. (K.B)
Ans: Each run of loop is called an iteration
Q.11 Define Nested ‘for’ Loop. (K.B)
Ans: A loop in a loop is known as nested loop.
Q.12 Write down the structure of Nested ‘for’ loop. (K.B)
Ans: for (initialization; condition; increment/decrement)
{
for (initialization; condition; increment/decrement)
{
Code to repeat
}
}
Q.13 Write a program that shows the working of nested ‘for’ loop. (A.B)
Ans:
#include<stdio.h>
void main()
for(int i =1; i < = 5; i++)
{
for(int j =1; j < = i; j++)
{
printf(“*”);
}
printf(“\n”);
COMPUTER SCIENCE-10 104
Chapter– 4 Data and Repetition
}
}
Q.14 Write a program that displays the table of 2, 3, 4, 5 and 6. (A.B)
Ans:
#include <stdio.h>
void main()
{
int n=2,j;
printf(“TABLE OF 2”);
for(j=1;j<=10;j++)
{
printf("%d x %d = %d \n",n,j,n*j);
}
n=3;
printf(“\nTABLE OF 3”);
for(j=1;j<=10;j++)
{
printf("%d x %d = %d \n",n,j,n*j);
}
n=4;
printf(“\nTABLE OF 4”);
for(j=1;j<=10;j++)
{
printf("%d x %d = %d \n",n,j,n*j);
}
n=5;
printf(“\nTABLE OF 5”);
for(j=1;j<=10;j++)
{
printf("%d x %d = %d \n",n,j,n*j);
}
n=6;
printf(“\nTABLE OF 6”);
for(j=1;j<=10;j++)
{
printf("%d x %d = %d \n",n,j,n*j);
}
}
Q.15 How loops can be used to read and write values in arrays. (K.B+U.B)
Ans: Writing values in Array using Loops:
Using loops, we can easily take input in arrays. If we want to take input from user in an
array of size 10, we can simply use a loop as follows:
int a [10];
COMPUTER SCIENCE-10 105
Chapter– 4 Data and Repetition
printf(“%d”, i);
(A) 10 (B) 0123456789 (C) 0 (D) Syntax Error
11. What will be the output of following C code? (K.B+U.B+A.B)
for(i=0; i++; i<15)
printf(“%d ”, i);
(A) 10 11 12 13 14 (B) 9 10 11 12 13
(C) Infinite Loop (D) 10 11 12 13 14 15 16
12. What will be the final value of the variable ‘digit’? (U.B+A.B)
void main()
{
int digit = 0;
for( ; digit < = 9; )
digit++;
digit *= 2;
digit;
}
(A) -1 (B) 17 (C) 19 (D) 26
13. Which one of the following is not a loop statement? (K.B)
(A) for (B) if (C) while (D) do – while
14. When number of iterations of execution are known in advance, which is the best
choice of programmer? (K.B+U.B)
(A) for (B) do – while (C) while (D) None of These
15. How many expressions are used in for Loop? (K.B)
(A) 2 (B) 4 (C) 5 (D) 3
16. Which are the mandatory part of for Loop? (K.B+U.B)
(A) Initialization (B) Condition
(C) Increment/Decrement (D) All of these
17. The for loop expressions are enclosed in: (K.B+U.B)
(A) ( ) (B) { } (C) [ ] (D) None of These
18. Which of the following expression is executed only once in the for Loop? (K.B+U.B)
(A) Condition Loop Control (B) Increment/Decrement
(C) Initialization (D) None of These
}
Description:
Consider the example program given above.
First of all, the value of i is set to 1 and then condition is checked.
As the condition is true (1<= 10) so loop body executes. As in the loop body,
we are displaying the value of the counter variable, so 1 is displayed on
console.
After increment, the value of i becomes 2. The condition is again checked. It
is true as(2<=10) so this time 2 is printed.
The procedure continues till 10 is displayed and after increment the value of i
becomes 11. Condition is checked and it turns out to be false (11>10) so the
loop finally terminates after printing the numbers from 1 to 10.
Programming Time 4.4
Write a program that calculates the factorial of a number input by user.
Program Logic:
When we want to solve a problem programmatically, first we need to know exactly
what we want to achieve. In this example, we are required to find the factorial of a
given number, so first we need to know the formula to find factorial of a number.
N! = 1 * 2 * 3 * 4 * ..... * (N-1) * N
We can see the pattern that is being repeated, so we can solve the problem using for
loop.
Program:
#include<stdio.h>
void main()
{
int n, fact = 1;
printf(“Please enter a positive number whose factorial you want to find”);
scanf("%d, &n);
for(int I = 1; i <=n; i++)
{
fact = fact * i;
}
printf(“The factorial of input number %d is %d, n, fact);
}
Description:
Following table shows the working of program, if the input number is 5. It
demonstrates the changes in the values of variables at each iteration.
Iteration Value of counter Condition Loop body Result
fact=1
1 i=1 TRUE(1<=5) fact = fact*i fact=1*1=1.
2 i=2 TRUE(2<=5) fact = fact*i fact=1*2=2
3 i=3 TRUE(3<=5) fact = fact*i fact=2*3=6
}
Output:
Here is the output of above program.
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Description:
As we understand the working of inner loop, so here let's focus on outer loop.
1- For the value i = 1, condition in outer loop is checked which is true (1 < = 5),
so whole inner loop is executed and numbers from 1 – 10 are displayed.
2- When control gets out of inner loop, printf("\n”); is executed which inserts a
new line on console.
3- Then i is incremented and it becomes 2. As it is less than 5, so condition is
true. The whole inner loop is executed, and thus numbers from 1 – 10 are
again displayed on screen. Coming out of the inner loop new line is inserted
again.
4- After five times displaying the numbers from 1 – 10 on screen, the value of i
gets incremented to 6 and condition of outer loop turns false. So outer loop
also terminates.
Programming Time 4.6
Problem:
Write a program to display the following pattern of stars on screen.
*
**
***
****
*****
******
Program:
#include<stdio.h>
void main ( )
{
for(int i = 1; i <= 6; i++)
{
for(int j = 1; j <= i; j++)
printf(“*”);
printf(“\n”);
}
}
Description:
Here is the description of above code.
1- As we have to display 6 lines containing stars, so we run the outer loop from
1 to 6.
2- We can observe that in the given pattern we have 1 star on 1st line, 2 stars on
2nd line, 3 stars on 3rd line and so on. So, the inner loop is dependent on the
outer loop, i.e. if counter of outer loop is 1 then inner loop should run 1 time,
COMPUTER SCIENCE-10 112
Chapter– 4 Data and Repetition
if the counter of outer loop is 2 then inner loop should run 2 times and so on.
So, we use the counter of outer loop in the termination condition of inner loop
i.e. j <= i.
3- When outer loop counter i has value 1, inner loop only runs 1 time, so only 1
star is displayed. When outer loop counter is 2, the inner loop runs 2 times, so
2 stars are displayed and the process is repeated until six lines are complete.
Programming Time 4.7
Problem:
Write a program that counts multiples of a given number lying between two
numbers.
Program:
#include <stdio.h>
void main ()
{
int n, lower, upper, count = 0;
printf (“Enter the number: ”);
scanf (“%d", &n);
printf (“Enter the lower and upper limit of multiples:\n");
scanf (“%d%d”, &lower, &upper);
for(int i = lower; i <= upper; i++)
if(i%n == 0)
count++;
printf ("Number of multiples of %d between %d and %d are %d”, n, lower,
upper, count);
}
Programming Time 4.8
Problem:
Write a program to find even numbers in integers ranging from n1 to n2 (where nl is
greater than n2).
Program:
#include <stdio.h>
void main ( )
{
int n1, n2;
printf (“Enter the lower and upper limit of even numbers: \n”)
scanf (“%d%d", &n2, &n1);
if(n1 > n2)
{
for (int i = n1; i > = n2; i--)
{
COMPUTER SCIENCE-10 113
Chapter– 4 Data and Repetition
if(i % 2 == 0)
printf (“%d”, i);
}
}
}
Programming Time 4.9
Problem:
Write a program to determine whether a given number is prime number or not.
Program:
#include <stdio.h>
void main ( )
{
int n;
int flag = 1;
printf (“Enter a number: ”);
scanf (“%d”, &n);
for (int i = 2; i < n; i++)
{
if (n % i == 0)
flag = 0;
}
if (flag == 1)
printf (“This is a prime number”);
else
printf (“This is not a prime number”);
}
Programming Time 4.10
Problem:
Write a program to display prime numbers ranging from 2 to 100.
Program:
# include<stdio.h>
int main ()
{
int flag;
for (int j = 2; j <= 100; j++)
{
flag = 1;
{
printf("%d X %d = %d \n",n,j,n*j);
}
}
ACTIVITY 4.2
Write a program that displays the table of 2,3,4,5 and 6.
Solution:
#include <stdio.h>
void main()
{
int n=2,j;
for(j=1;j<=10;j++)
{
printf("%d x %d = %d \n",n,j,n*j);
}
n=3;
for(j=1;j<=10;j++)
{
printf("%d x %d = %d \n",n,j,n*j);
}
n=4;
for(j=1;j<=10;j++)
{
printf("%d x %d = %d \n",n,j,n*j);
}
n=5;
for(j=1;j<=10;j++)
{
printf("%d x %d = %d \n",n,j,n*j);
}
n=6;
for(j=1;j<=10;j++)
{
printf("%d x %d = %d \n",n,j,n*j);
}
}
ACTIVITY 4.3
Write a program that takes as input the marks obtained in matriculation by 30 students
of a class. The program should display the average marks of the class.
Solution:
#include<stdio.h>
void main()
{
int i;
int Avg;
int maks[30]; //Array Declaration
int sum=0;
for( i=0 ; i<=29 ; i++)
{
printf(" Enter Marks/n ");
scanf("%d" , &marks[i] ); // Store Data in Array
}
for( i=0 ; i<=29 ; i++ )
{
Sum = sum + marks[i]; // Read Data from an Array
Avg = Sum/30;
printf(" Average Marks = %d\n" , Avg);
}
}
EXERCISE
Q1. Multiple Choice Questions
1) An array is a__________structure. (K.B)
A) Loop B) Control C) Data D) Conditional
2) Array elements are stored at ________ memory locations. (K.B)
A) Contiguous B) Scattered C) Divided D) None
3) If the size of an array is 100, the range of indexes will be ________. (K.B)
A) 0-99 B) 0-100 C) 1-100 D) 2-102
4) _______ structure allows repetition of a set of instructions. (K.B+U.B)
A) Loop B) Conditional C) Control D) Data
5) _______ is the unique identifier, used to refer to the array. (K.B)
A) Data Type B) Array Name C) Array Size D) None
6) Array can be initialized ________ declaration. (K.B)
COMPUTER SCIENCE-10 117
Chapter– 4 Data and Repetition
Ans: No Loop is not a data structure. Loop is a type of control structure which repeats a
statement of set of statements. While Data Structure is a container to store collection of
data items in a specific layout.
2) What is the use of nested loops?
Ans: When we use a loop inside another loop, it is called nested loop structure. We use nested
loops to repeat a pattern multiple time.
General Structure
for (initialization; condition; increment/decrement)
{
for (initialization; condition; increment/decrement)
{
Code to repeat
}
}
3) What is the advantage of initializing an array at the time of declaration?
Ans: If we do not initialize an array at the time of declaration. Then we need to initialize the
array elements one by one. It means that we cannot initialize all the elements of array in a
single statement. This is demonstrated by the following example.
Example:
The compiler generates an error on the above example code, as we try to initialize the
whole array in one separate statement after declaring it.
4) Describe the structure of a for loop.
Ans: for (initialization; condition; increment/decrement)
{
Code to repeat
}
Step 1. Initialization is the first part to be executed in a for loop. Here we initialize our
counter variable and then move to the condition part.
Step 2. Condition is declared and checked, and if it turns out to be false, then we come out of
loop.
Step 3. If the condition is true, then body of the loop is executed.
Step 4. After executing the body of loop, the counter variable is increased or decreased
depending on the used logic, and then we again move to the step 2.
5) How can you declare an array? Briefly describe the three parts of array declaration.
Ans: In C language, an array can be declared as follows:
KLMN
(A) (B)
# include<stdio.h> #include <stdio.h>
void main ( ) int main( )
{ {
int i,j; int i,j,rows=5;
for(i=1;i<=3;i++) char alphabet='A';
{ printf("\nHere your pattern\n");
for(j=1;j<=i;j++) for(i=1; i<=rows; i++)
{ {
printf(“*****”); for(j=1; j<=i; j++)
}
{
printf(“\n”); printf(alphabet++);
}
} printf(“\n”);
} }
}
EXERCISE 2
Write a program that takes two positive integers a and b as input and displays the value of ab.
# include <stdio.h>
int main ( )
{
int a,b,result=1;
printf("Enter a:");
scanf ("%d",&a);
printf("Enter b:");
scanf ("%d",&b);
for (int i=1;i<=b;i++)
{
result=result*a;
}
printf("result=%d^%d=%d",a,b,result);
}
EXERCISE 3
Write a program that takes two numbers as input and displays their Greatest Common
Divisor (GCD) using Euclidean method.
# include <stdio.h>
void main( )
{
int m, n,r;
printf("Enter-two integer numbers: ");
scanf ("%d %d", &m, &n);
for (;n > 0;)
COMPUTER SCIENCE-10 122
Chapter– 4 Data and Repetition
{
r = m % n;
m = n;
n = r;
}
printf ("GCD = %d \n",m);
}
EXERCISE 4
Write a program to display factorials numbers from 1 to 7. (Hint: Use Nested Loops)
# include<stdio.h>
void main ( )
{
int i,j,fact=1;
for(i=1;i<=7;i++)
{
for(j=1;j<=i;j++)
{
fact=fact*j;
}
printf(“\n%d”,fact);
fact=1;
}
}
EXERCISE 5
Write a program that takes 10 numbers as input in an array and displays the product of
first and last element on console.
# include<stdio.h>
void main ( )
{
int i,arr[10],mult;
printf(“Enter 10 elements:”);
for(i=0;i<10;i++)
scanf(“%d”,&arr[i]);
mult=arr[0]*arr[9];
printf(“Result of first and last element =%d”,mult);
}
EXERCISE 6
Write a program that declares and initializes an array of 7 elements and tells how many
elements in the array are greater than 10.
# include <stdio.h>
void main ( )
{
int counter=0,arr[ ]={3,54,22,67,34,29,19};
for(int i=0;i<7;i++)
{
if(arr[i] > 10)
COMPUTER SCIENCE-10 123
Chapter– 4 Data and Repetition
counter++;
}
printf(“The total number of elements greater than 10 are %d”,counter);
}
ANSWER KEY
4.1 DATA STRUCTURE
1 2 3
A A C
4.1.1 ARRAY
4.1.2 ARRAY DECRORATION
4.1.3 ARRAY INITIALIZATION
4.1.4 ACCESSING ARRAY ELEMENTS
4.1.5 USING VARIABLES AS ARRAY INDEXES.
1 2 3 4
A A B C
4.2 LOOP STRUCTURE
4.2.2 FOR LOOP
4.2.3 NESTED LOOP
4.2.5 ARRAY AND LOOPS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
A B A A A A A A A D A A D C A
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
C B C C C C B D D C A A C A B
31 32 33
A A C