“Explore comprehensive question and answer solutions for Class 10 Computer Science Chapter 6. Enhance your understanding with detailed explanations and step-by-step guidance for all key topics covered in the chapter.”
1. Define array. Why do we use arrays in computer programs?
Ans: An array is a data structure that can hold multiple values of the same type in a single variable. Arrays allow programmers to store and manipulate data collection in an organized manner. We use arrays in computer programs to efficiently manage and process large sets of related data, enabling easy access and modification through indexing.
2. Can we store both integer and float types of data in a single array? Demonstrate this by writing a simple C program.
Ans: No, we cannot store both integer and float types of data in a single array as arrays in C are homogenous, meaning all elements in an array must be of the same data type.
Example:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5}; // Integer array
// Attempting to store float values in the same array will result in a compile-time error.
return 0;
}
3. Write the indices of the first and last element of the following array declaration.
The given array is char city[7] = {‘S’, ‘T’, ‘L’, ‘C’, ‘H’, ‘A’, ‘R’};.
The index of the first element (‘S’) is 0.
The index of the last element (‘R’) is 6.
4. Write a C program and declare an integer type array with a capacity of 7. Take input to the array from the keyboard. Display the 8th element of the array. Hint: display num[7] if num is the name of the array. Analyze the output.
Ans:
#include <stdio.h>
int main() {
int num[7];
for(int i = 0; i < 7; i++) {
printf(“Enter element %d: “, i + 1);
scanf(“%d“, &num[i]);
}
printf(“The 8th element (num[7]) is: %d\n“, num[7]);
return 0;
}
Analysis: The array num has a capacity of 7, so accessing num[7] will go out of bounds, leading to undefined behavior. The program may display a garbage value or cause a runtime error.
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.
Ans:
#include <stdio.h>
int main() {
int arr[7];
for(int i = 0; i < 7; i++) {
printf(“Address of arr[%d]: %p\n”, i, (void*)&arr[i]);
}
return 0;
}
6. Write a C program and declare two integer type arrays, each with a capacity of 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.
Ans:
#include <stdio.h>
int main() {
int arr1[7], arr2[7];
for(int i = 0; i < 7; i++) {
printf(“Enter element %d for arr1: “, i + 1);
scanf(“%d”, &arr1[i]);
}
for(int i = 0; i < 7; i++) {
arr2[i] = arr1[i];
}
printf(“Elements of arr2: “);
for(int i = 0; i < 7; i++) {
printf(“%d “, arr2[i]);
}
printf(“\n”);
return 0;
}
7. Write a strategy to find the summation of all the even numbers stored in an array. Write a C program for the same. If the array elements are {1, 2, 4, 3, 5, 6, 7, 8}, the output of the program will be 20.
Ans:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 4, 3, 5, 6, 7, 8};
int sum = 0;
for(int i = 0; i < 8; i++) {
if(arr[i] % 2 == 0) {
sum += arr[i];
}
}
printf(“Sum of even numbers: %d\n”, sum);
return 0;
}
8. Write a strategy to find the summation of all the even positioned numbers stored in an array. Write a C program for the same. If the array elements are {1, 2, 4, 3, 5, 6, 7, 8}, the output of the program will be 18.
Ans:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 4, 3, 5, 6, 7, 8};
int sum = 0;
for(int i = 1; i < 8; i += 2) { // Even positioned indices: 1, 3, 5, 7
sum += arr[i];
}
printf(“Sum of even positioned numbers: %d\n”, sum);
return 0;
}
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 a complete C program incorporating the logic.
Ans:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5, 1, 2, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int element_to_replace = 3;
int replacement = 0;
for(int i = 0; i < n; i++) {
if(arr[i] == element_to_replace) {
arr[i] = replacement;
break;
}
}
printf(“Updated array: “);
for(int i = 0; i < n; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
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 a complete C program incorporating the logic.
Ans:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5, 1, 2, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int element_to_replace = 3;
int replacement = 0;
int index = -1;
for(int i = 0; i < n; i++) {
if(arr[i] == element_to_replace) {
index = i;
}
}
if(index != –1) {
arr[index] = replacement;
}
printf(“Updated array: “);
for(int i = 0; i < n; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
11. Write a C program to replace all the even-positioned elements in an integer array by 0.
Ans:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 9, 5, 5, 7, 1, 9};
int size = sizeof(arr) / sizeof(arr[0]);
for (int i = 1; i < size; i += 2) {
arr[i] = 0;
}
printf(“Array after replacing even-positioned elements with 0:\n“);
for (int i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
return 0;
}
12. Write a strategy to replace all the odd numbers in an integer array by 0.
Ans:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 9, 5, 5, 7, 1, 9};
int size = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < size; i++) {
if (arr[i] % 2 != 0) {
arr[i] = 0;
}
}
printf(“Array after replacing odd numbers with 0:\n“);
for (int i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
return 0;
}
13. Write a C program to store your name and your mother’s name in two different strings. Display them one after another.
Ans:
#include <stdio.h>
int main() {
char name[50];
char motherName[50];
printf(“Enter your name: “);
gets(name);
printf(“Enter your mother’s name: “);
gets(motherName);
printf(“Your name is: %s\n“, name);
printf(“Your mother’s name is: %s\n“, motherName);
return 0;
}
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 program should display the average marks of the students and whether they “PASS” (average >= 45) or “FAIL”.
Ans:
#include <stdio.h>
int main() {
int marks1[10], marks2[10], marks3[10], average[10];
printf(“Enter the marks for 10 students in 3 subjects:\n“);
for (int i = 0; i < 10; i++) {
printf(“Student %d:\n“, i + 1);
printf(“Subject 1: “);
scanf(“%d“, &marks1[i]);
printf(“Subject 2: “);
scanf(“%d“, &marks2[i]);
printf(“Subject 3: “);
scanf(“%d“, &marks3[i]);
average[i] = (marks1[i] + marks2[i] + marks3[i]) / 3;
}
printf(“\nAverage marks and result (PASS/FAIL):\n“);
for (int i = 0; i < 10; i++) {
printf(“Student %d: Average = %d – %s\n“, i + 1, average[i], average[i] >= 45 ? “PASS” : “FAIL”);
}
return 0;
}
15. Write any two limitations of arrays. Can you store your name and roll number in the same array?
Ans: Limitations of Arrays:
Fixed Size: Once an array is declared, its size cannot be changed. This can lead to either wastage of memory if the array size is too large or lack of sufficient memory if the array size is too small.
Homogeneous Data: Arrays can store only elements of the same data type. You cannot store different data types in a single array.
Can You Store Your Name and Roll Number in the Same Array?
Ans: No, you cannot store both your name (which is a string) and your roll number (which is an integer) in the same array because arrays in C are homogeneous, meaning they can only store elements of the same data type.