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
TEXTUAL QUESTIONS AND ANSWERS
1. Define array. Why do we use arrays in computer programs?
Ans. An array in C is a fixed-size collection of similar data items stored in contiguous memory locations.
Arrays help maintain large sets of data under a single variable name to avoid confusion that can occur when using several variables.
To create an array, define the data type (like int) and specify the name of the array followed by square brackets [].
To insert values to it, use a comma-separated list, inside curly braces:
int myNumbers[] = {25, 50, 75, 100};
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 an array as an integer 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.
Mixing data types in a single array would lead to type mismatch errors and potential data corruption.
Code:
#include <stdio.h>
struct MixedData {
int integerData;
float floatData;
};
int main() {
struct MixedData mixedArray[5];
mixedArray[0].integerData = 10;
mixedArray[0].floatData = 3.14;
mixedArray[1].integerData = 20;
mixedArray[1].floatData = 4.56;
// ...
return 0;
}
3. Write the indices of the first and last element of the following array declaration. char city [7] = {‘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 array[7]; // Declare an integer array with capacity 7
int i;
// Input values into the array from the keyboard
printf("Enter 7 integers, one at a time:\n");
for (i = 0; i < 7; i++) {
scanf("%d", &array[i]);
}
// Display the 7th element of the array (index 6)
if (i >= 7) {
printf("The 7th element is: %d\n", array[6]);
} else {
printf("You haven't entered enough elements to display the 7th element.\n");
}
return 0;
}
Output:
Enter 7 integers, one at a time:
10
20
30
40
50
60
70
The 7th element is: 70
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 array[7]; // Declare an integer array with 7 elements
int i;
// Initialize the array with some values
for (i = 0; i < 7; i++) {
array[i] = i * 10;
}
// Display the addresses of individual elements
printf("Addresses of individual elements in the array:\n");
for (i = 0; i < 7; i++) {
printf("Element %d: Address = %p\n", i, (void *)&array[i]);
}
return 0;
}
Output:
Addresses of individual elements in the array:
Element 0: Address = 0x7ffe6a9d19d0
Element 1: Address = 0x7ffe6a9d19d4
Element 2: Address = 0x7ffe6a9d19d8
Element 3: Address = 0x7ffe6a9d19dc
Element 4: Address = 0x7ffe6a9d19e0
Element 5: Address = 0x7ffe6a9d19e4
Element 6: Address = 0x7ffe6a9d19e8
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[7], b[7], i;
printf("Enter the values of the first array: ");
for (i = 0; i < 7; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < 7; i++) {
b[i] = a[i];
}
printf("\nDisplaying the elements of the second array:\n");
for (i = 0; i < 7; i++) {
printf("%d \t", b[i]);
}
return 0;
}
Output:
Enter the values of the first array: 10 20 30 40 50 60 70
Displaying the elements of the second array:
10 20 30 40 50 60 70
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:
- Declare an integer array to store the numbers.
- Initialize a variable to hold the sum, initially set to 0.
- Use a loop to iterate through the array.
- For each element in the array, check if it’s an even number.
- If the element is even, add it to the sum.
- Continue this process for all elements in the array.
- After the loop, the sum will contain the summation of all even numbers.
Code:
#include <stdio.h>
int main() {
int array[] = {2, 5, 8, 10, 15, 6, 12, 7}; // Example array
int n = sizeof(array) / sizeof(array[0]); // Calculate the number of elements
int sum = 0;
for (int i = 0; i < n; i++) {
if (array[i] % 2 == 0) {
sum += array[i];
}
}
printf("The summation of even numbers in the array is: %d\n", sum);
return 0;
}
Output:
The summation of even numbers in the array is: 42
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:
- Declare an integer array to store the numbers.
- Initialize a variable to hold the sum, initially set to 0.
- Use a loop to iterate through the array, starting from the first even index (index 1).
- For each even-positioned element in the array, add it to the sum.
- Continue this process for all even-positioned elements in the array.
Code:
#include <stdio.h>
int main() {
int array[] = {3, 6, 9, 12, 15, 18, 21}; // Example array
int n = sizeof(array) / sizeof(array[0]); // Calculate the number of elements
int sum = 0;
for (int i = 1; i < n; i += 2) { // Start from index 1 and increment by 2 for even positions
sum += array[i];
}
printf("The summation of even-positioned numbers in the array is: %d\n", sum);
return 0;
}
Output:
The summation of even-positioned numbers in the array is: 36
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:
- Declare an integer array to store the numbers.
- Declare variables to store the element you want to replace (e.g., ‘elementToReplace’) and the replacement value (e.g., ‘replacementValue’).
- Use a loop to iterate through the array.
- When you find the first occurrence of the element you want to replace, replace it with the ‘replacementValue’.
- Exit the loop after the replacement is made.
- Continue the loop for the remaining elements in the array.
- Print the updated array.
Code:
#include <stdio.h>
int main() {
int array[] = {1, 2, 3, 4, 5, 1, 2, 3}; // Example array
int n = sizeof(array) / sizeof(array[0]); // Calculate the number of elements
int elementToReplace = 3;
int replacementValue = 0;
int i;
// Find and replace the first occurrence
for (i = 0; i < n; i++) {
if (array[i] == elementToReplace) {
array[i] = replacementValue;
break; // Exit the loop after replacement
}
}
// Print the updated array
printf("Updated array: ");
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
Output:
Updated array: 1 2 0 4 5 1 2 3
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[7], i, s, flag = -1; // Initialize flag to -1
printf("Enter the values of the array: ");
for (i = 0; i < 7; i++) {
scanf("%d", &a[i]);
}
printf("\nEnter the number to be replaced: ");
scanf("%d", &s);
for (i = 0; i < 7; i++) {
if (a[i] == s) {
flag = i; // Update the flag to store the index of the first occurrence
break; // Exit the loop after finding the first occurrence
}
}
if (flag != -1) {
a[flag] = 0;
printf("\nThe new array after replacing the first element is:\n");
for (i = 0; i < 7; i++) {
printf("%d ", a[i]);
}
printf("\n");
} else {
printf("\nThe element to be replaced was not found in the array.\n");
}
return 0;
}
Output:
Enter the values of the array: 1 2 3 4 5 1 2
Enter the number to be replaced: 3
The new array after replacing the first element is:
1 2 0 4 5 1 2
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 array[] = {1, 2, 3, 9, 5, 7, 1, 9}; // Example array
int n = sizeof(array) / sizeof(array[0]); // Calculate the number of elements
// Replace even-positioned elements with 0
for (int i = 1; i < n; i += 2) { // Start from index 1 and increment by 2 for even positions
array[i] = 0;
}
// Print the updated array
printf("Updated array: ");
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
Output:
Updated array: 1 0 3 0 5 0 7 0 9
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:
- Declare an integer array to store the numbers.
- Use a loop to iterate through the array.
- For each element in the array, check if it’s an odd number (i.e., the remainder when dividing by 2 is not 0).
- If the element is odd, replace it with 0.
- Continue this process for all elements in the array.
Code:
#include <stdio.h>
int main() {
int array[] = {1, 2, 3, 9, 5, 5, 7, 1, 9}; // Example array
int n = sizeof(array) / sizeof(array[0]); // Calculate the number of elements
// Replace all odd numbers with 0
for (int i = 0; i < n; i++) {
if (array[i] % 2 != 0) {
array[i] = 0;
}
}
// Print the updated array
printf("Updated array: ");
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
Output:
Updated array: 0 2 0 0 0 0 0 0 0
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>
#include <string.h>
int main() {
char yourName[] = "Your Name"; // Replace with your name
char motherName[] = "Mother's Name"; // Replace with your mother's name
// Display your name and your mother's name one after another
printf("Your Name: %s\n", yourName);
printf("Mother's Name: %s\n", motherName);
return 0;
}
Output:
Your Name: Your Name
Mother's Name: Mother's Name
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 numStudents = 10;
int numSubjects = 3;
int marks[numStudents][numSubjects];
int averages[numStudents];
// Input marks for each student in each subject
for (int i = 0; i < numStudents; i++) {
printf("Enter marks for student %d (Subject 1 Subject 2 Subject 3): ", i + 1);
for (int j = 0; j < numSubjects; j++) {
scanf("%d", &marks[i][j]);
}
}
// Calculate average marks for each student
for (int i = 0; i < numStudents; i++) {
int sum = 0;
for (int j = 0; j < numSubjects; j++) {
sum += marks[i][j];
}
averages[i] = sum / numSubjects;
}
// Display the average marks and pass/fail status for each student
printf("Average Marks and Pass/Fail Status:\n");
for (int i = 0; i < numStudents; i++) {
printf("Student %d: Average = %d, Status: %s\n", i + 1, averages[i], (averages[i] >= 45) ? "PASS" : "FAIL");
}
return 0;
}
Output:
Enter marks for student 1 (Subject 1 Subject 2 Subject 3): 80 75 60
Enter marks for student 2 (Subject 1 Subject 2 Subject 3): 50 45 55
Enter marks for student 3 (Subject 1 Subject 2 Subject 3): 65 70 75
Enter marks for student 4 (Subject 1 Subject 2 Subject 3): 30 40 35
Enter marks for student 5 (Subject 1 Subject 2 Subject 3): 85 90 88
Enter marks for student 6 (Subject 1 Subject 2 Subject 3): 40 35 30
Enter marks for student 7 (Subject 1 Subject 2 Subject 3): 55 60 65
Enter marks for student 8 (Subject 1 Subject 2 Subject 3): 70 75 80
Enter marks for student 9 (Subject 1 Subject 2 Subject 3): 75 65 70
Enter marks for student 10 (Subject 1 Subject 2 Subject 3): 45 48 50
Average Marks and Pass/Fail Status:
Student 1: Average = 71, Status: PASS
Student 2: Average = 50, Status: FAIL
Student 3: Average = 70, Status: PASS
Student 4: Average = 35, Status: FAIL
Student 5: Average = 87, Status: PASS
Student 6: Average = 35, Status: FAIL
Student 7: Average = 60, Status: FAIL
Student 8: Average = 75, Status: PASS
Student 9: Average = 70, Status: PASS
Student 10: Average = 47, Status: FAIL
15. Write any two limitations of arrays. Can you store your name and roll number in the same array?
Ans. The limitations of the array are –
- An array has a fixed size which means you cannot add/delete elements after creation. …
- Unlike lists in Python, cannot store values of different data types in a single array.
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.
Sooi helpful