# Class 10 Computer Science Chapter 6 Arrays In C

Class 10 Computer Science Chapter 6 Arrays In C Question Answer, SEBA Class 10 Computer Science Solutions, NCERT Class 10 Computer Science Chapter 6 Arrays In C to each chapter is provided in the list so that you can easily browse throughout different chapters SCERT Class 10 Computer Science Chapter 6 Arrays In C and select needs one.

## Class 10 Computer Science Chapter 6 Arrays In C

Also, you can read the SCERT book online in these sections Solutions by Expert Teachers as per SCERT (CBSE) Book guidelines. NCERT Solution of Class 10 Computer Science Chapter 6 Arrays In C is part of AHSEC All Subject Solutions. Here we have given Class 10 Computer Science Chapter 6 Arrays In C Notes for All Subjects, You can practice these here in Class 10 Computer Science.

### ARRAYS IN C

Chapter – 6

COMPUTER SCIENCE

Key Terms (Basic Concepts)

● Array: Arrays a kind of data structure that can store a fixed-size sequential Collection of elements of the same type.

● Index: The index indicates the position of the element within the array.

● Element: Each item in an array is called an element.

EXERCISE

1. Define array. Why do we use arrays in computer programs?

Ans. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

For example, if you want to store 10 integers, you can create an array for it.

int data;

Arrays are used to store multiple values of the same data type.

For example,

int a;

In the above, variable a can store only one value of only integer type.

But in an array, if we declare variable a as integer, then we can store n number of integer type of value where n is the index of array.

int a;

In the above, we can store 10 integer values in the variable a.

2. Can we store both integer and float types of data in a single array? Demonstrate this by writing a simple C program.

Ans. If we declare array as an integer the we can store the values of integer type only not of character or float type. So, we cannot store both integer and float types in a single array.

For example, if we want to declare array of integer and float type, then we have to declare two variables with different data type as follows:

int a;

float b;

3. Write the indices of the first and last element of the following array declaration. char city  = {‘S’, ‘T’, ‘L’, ‘C’, ‘H’, ‘A’, ‘R’, };

Ans. Indices of the first element of the array is 0.

Indices of the last element of the array is 6.

4. Write a C program and declare an integer type array with capacity 7. Take input to the array from the keyboard. Display the 7ᵗʰ element of the array.

Solution:

Code:

#include<stdio.h>

int main()

{

int a, i;

printf(“Enter the values of array : “);

for(i=0; i<7; i++)

{

scanf(“ %d”, &a[i];

}

printf(“The 7th element is %d”, a;

return 0;

}

Output:

5. Write a C program and declare an integer type array with 7 elements in it. Display the address of the individual elements in the array.

Solution:

Code:

#include<stdio.h>

int main()

{

int a, i;

printf(“Enter the values of array : “);

for(i=0; i<7; i++)

for(i=0; i<7; i++)

{

scanf(“ %d”, &a[i];

}

for(i=0; i<7; i++)

{

printf(“\n The address of element %d is %p”, (i+1), a[i];

}

return 0;

}

NOTE: To print the memory address, we use ‘%p’ format specifier in C

6. Write a C program and declare two integer type arrays, each with capacity 7. Take input only to the first array. Write a loop to copy the elements of the first array to the second one. Display the elements of the second array.

Solution:

Code:

#include<stdio.h>

int main()

{

int a, b, i, j;

printf(“Enter the values of first array : “);

for(i=0; i<7; i++)

{

scanf(“ %d”, &a[i];

}

for(i=0; i<7; i++)

{

b[i] = a[i];

}

printf (“\n Displaying the elements of second array \n “);

for(i=0; i<7; i++)

{

printf(“%d \t”, b[i];

}

return 0;

}

Output:

7. Write a strategy to find the summation of all the even numbers stored in an array. Write a C program for the same.

Solution:

Strategy:

● Create a new array that will store numbers in the array.

● Use the for loop to iterate over the array and on each iteration check that the element in the array has a remainder when divided by 2.

● If the remainder is 0, add the value of the element with a declared variable, which is initialized to 0.

Code:

#include<stdio.h>

int main()

{

int a, i, sum = 0;

printf(“Enter the values of array : “);

for(i=0; i<7; i++)

{

scanf(“ %d”, &a[i];

}

for(i=0; i<7; i++)

{

if (a[i]%2 ==0)

{

sum = sum + a[i];

}

}

printf (“\n The sum of the even numbers stored in the array is %d “, sum);

return 0;

}

Output:

8. Write a strategy to the summation of all the even positioned numbers stored in an array. Write a C program for the same.

Solution:

Strategy:

● Create a new array that will store numbers in an array.

● Use the for loop to iterate over the array and on each iteration check that the counter in the array has a remainder when divided by 2.

● If the remainder is 0, add the value of the element with a declared variable, which is initialized to 0.

Code :

#include<stdio.h>

int main()

{

int a, i, sum = 0;

printf(“Enter the values of array : “);

for(i=0; i<7; i++)

{

scanf(“ %d”, &a[i];

}

for(i=0; i<7; i++)

{

if (i%2 ==0)

{

sum = sum + a[i];

}

}

printf (“\n The sum of the even positioned numbers stored in the array is %d “, sum);

return 0;

}

Output:

9. Write the logic to replace only the first occurrence of an element in an array. For example, if the array elements are { 1, 2, 3, 4, 5, 1, 2, 3} and we want to replace element 3 by 0, the array content becomes { 1, 2, 0, 4, 5, 1, 2, 3}. Write the complete C program incorporating the logic.

Solution:

Logic:

● Create a new array that will store numbers in a array.

● Use the for loop to iterate over the array.

● Enter the number to be searched and replaced.

● Use the for loop to search for the number, if it encountered the number, replace the number with 0 and break the loop.

● Display the changed array.

Code:

#include<stdio.h>

int main()

{

int a, i, s;

printf(“Enter the values of array : “);

for(i=0; i<7; i++)

{

scanf(“ %d”, &a[i]);

}

printf(“\n Enter the number to be replaced : “);

scanf(“%d”, &s);

for(i=0; i<7; i++)

{

if (a[i] == s)

{

a[i] = 0;

break;

}

}

printf(“\n The new array after replacing the first element are : \n”);

for(i=0; i<7; i++)

{

printf(“ %d”, a[i];

}

return 0;

}

Output:

10. Write the logic to replace only the last occurrence of an element in an array. For example, if the array elements are {1, 2, 3, 4, 5, 1, 2, 3} and we want to replace element 3 by 0, the array content becomes {1, 2, 3, 4, 5, 1, 2, 0}. Write the complete C program incorporating the logic.

Solution:

Logic:

● Create a new array that will store numbers in a array.

● Use the for loop to iterate over the array.

● Enter the number to be searched and replaced.

● Use the for loop to search for the number, if it encountered the number, store the index of the number to a variable.

● So, the variable will store the last index number of searched array.

● Replace the last index number in the variable with 0.

● Display the changed array.

Code:

#include<stdio.h>

int main()

{

int a, i, s, flag=0;

printf(“Enter the values of array : “);

for(i=0; i<7; i++)

{

scanf(“ %d”, &a[i];

}

printf(“\n Enter the number to be replaced : “);

scanf(“%d”, &s);

for(i=0; i<7; i++)

{

if (a[i] == s)

{

flag = i;

}

}

a[flag] = 0;

Printf(“\n The new array after replacing the first element are : \n”);

for(i=0; i<7; i++)

{

printf(“ %d”, a[i]);

}

return 0;

}

Output:

11. Write a C program to replace all the even positioned elements in an integer array by 0.

For example, if the array elements are {1, 2, 3, 9, 5, 7, 1, 9}, it becomes {1, 0, 3, 0, 5, 0, 7, 0, 9}.

Solution:

Code:

#include<stdio.h>

int main()

{

int a, i, sum=0;

printf(“Enter the values of array : “);

for(i=0; i<7; i++)

{

scanf(“ %d”, &a[i];

}

for(i=0; i<7; i++)

{

if (i % 2 == 0)

{

sum = sum + a[i];

}

}

printf(“\n The summation of even positioned elements in the array is %d “,sum);

return 0;

}

Output:

12. Write the strategy to replace all the odd numbers in an integer array by 0. For example, if the array elements are {1, 2, 3, 9, 5, 5, 7, 1, 9}, it becomes {0, 2, 0, 0, 0, 0, 0, 0, 0}. Write the C program for the same.

Solution:

Strategy:

● Create a new array that will store numbers in an array.

● Use the for loop to iterate over the array and on each iteration check that the element in the array has a remainder when divided by 2.

● If the remainder is not 0, replace the element with 0.

● Display the changed array.

Code:

#include<stdio.h>

int main()

{

int a, i, sum=0;

printf(“Enter the values of array : “);

for(i=0; i<7; i++)

{

scanf(“ %d”, &a[i]);

}

for(i=0; i<7; i++)

{

if (i % 2 != 0)

{

sum = sum + a[i];

}

}

printf(“\n The summation of even positioned elements in the array is %d “, sum);

return 0;

}

Output:

13. Write a C program to store your name and your mother’s name in two different strings. Display them one after another.

Solution:

Code:

#include<stdio.h>

int main()

{

char n, m;

int i;

for(i=0; i<5; i++)

{

scanf(“%c”, &n[i];

}

printf(“Enter your mother’s name : \n”);

for(i=0; i<5; i++)

{

scanf(“%c”, &m[i]);

}

for(i=0; i<5; i++)

{

printf(“%c”, n[i]);

}

printf(“\n Your mother’s name : “);

for(i=0; i<5, i++)

{

printf(“%c”, m[i]);

}

return 0;

}

Output:

14. Write a C program to declare 3 integer type arrays to store the marks of 10 students scored in 3 different subjects. Take another integer to store the average marks of the students. The average mark of a student is the average of the marks scored by the student in 3 subjects. This should be calculated and inserted by the program. Then you should display the average marks of the students. The program should also display “PASS” or “FAIL” along with the average as per the rule: PASS if average > = 45, FAIL otherwise.

Solution:

Code:

#include<stdio.h>

int main()

{

int a , c, avg, i;

printf(“Enter details of 10 students :\n”);

printf(“Enter the marks of 10 students in English :\n”);

for(i=0; i<10; i++)

{

scanf(“%d”, &a[i];

}

printf(“\n Enter the marks of 10 students in Mathematics :\n”);

for(i=0; i<10; i++)

{

scanf(“%d”, &b[i];

}

printf(“\n Enter the marks of 10 students in Science :\n”);

for(i=0; i<10; i++)

{

scanf(“%d”, &c[i];

}

//Calculating average of 10 students in each subjects for(i=0; i<10; i++)

{

avg[i]=(a[i] + b[i] + c[i]/3;

if (avg[i]>45)

{

printf(“\n Student %d is PASS”, (i+1));

}

else

{

printf(“\n Student %d is FAIL”, (i+1));

}

}

return 0;

}

Output:

15. Write any two limitations of arrays. Can you store your name and roll number in the same array?

Ans. The limitations of array are –

● When we declare array as an integer the we can store the values of integer type only not of character or float type.

● We cannot store the values more than array limit. for example if we declare a then we can’t store values more than 10.

No, we cannot store name and roll number in the same array as both are of different data type. So, when we declare an array of character type, we cannot store element of integer type.

● we can store multiple values of same data type.

● When we declare array as an integer the we can store the values of integer type only not of character or float type.

● We cannot store the values more than array limit. for example if we declare a  then we can’t store values more than 10.

2. How do we create an array?

Ans. To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows –

type arrayName [ arraySize ];

Example:

int arr;

3. Can we declare without assigning the size of an array?

Ans. No. It is not possible to declare an array without specifying the size.

4. How to find the largest number in an array?

Ans. To find the largest element from the array, a simple way is to arrange the elements in ascending order. After sorting, the first element will represent the smallest element, the next element will be the second smallest, and going on, the last element will be the largest element of the array.

5. Does the array have a fixed length?

Ans. The length of an array is established when the array is created. After creation, its length is fixed.

6. What is the maximum array size in C?

Ans. There is no fixed limit to the size of an array in C.

7. What is the difference between the number of elements in an array and its highest index number?

Ans. The highest element index is one less than the total number of elements in the array.

8. What is the lowest index of an array?

Ans. The lowest index would always be 0.

9. What is the role of indexing in array?

Ans. Indexing is an operation that pulls out a select set of values from an array. The index of a value in an array is the value’s location within the array.

10. How to declare by assigning values to the elements?

Ans. To declare by assigning values to the elements, we may use the following declaration :

int num = {23, 45, 65, 85, 12}; (Type – integer).

char state = {‘A’, ;S’, ‘S’, ‘A’, ‘M’}; (Type – character).

11. Write a C program to sort the given data in ascending order.

Solution:

Code:

#include<stdio.h>

int main()

{

int a, i, j, temp;

printf(“Enter the values of arrays : “);

for(i=0; i<10; i++)

{

scanf(scanf(“%d”, &a[i]);

}

//loop to sort the elements

for(i=0; i<10; i++)

{

for(j=j+1; j<10; j++)

{

if(a[i]>a[j])

{

temp=a[i];

a[i]=a[j];

a[j]=temp;

}

}

}

//displaying the sorted array

printf(“The sorted array is \n”);

for(i=0; i<10; i++)

{

printf(“%d\t”, a[i]);

}

return 0;

}

Output:

Scroll to Top