Get answers, questions, notes, textbook solutions, extras, pdf, mcqs for Computer Chapter 6 Arrays in C of class 10 (HSLC/Madhyamik) for students studying under the Board of Secondary Education, Assam (SEBA). These notes/answers, however, should only be used for references and modifications/changes can be made wherever possible.
Summary
The chapter provides an understanding of arrays in C programming. It begins by defining an array as a collection of variables stored in contiguous memory locations. Arrays are used in computer programs to store multiple data items of the same type together, allowing efficient access and manipulation of data. However, arrays have limitations, such as a fixed size at the time of declaration and the ability to store only one type of data.
It delves into various operations on arrays. It explains how to declare an array, take input to the array elements, and display the elements of the array. It also discusses how to calculate the summation of array elements, replace all occurrences of a number in an array, search for a number in an array, and count the number of occurrences of a given number in an array. Each operation is demonstrated with a complete C program.
The chapter emphasizes that trying to access an array element beyond its declared size leads to undefined behavior. It also points out that you cannot store different types of data, such as a name (string) and a roll number (integer), in the same array due to the homogeneous data property of arrays. The chapter concludes by encouraging readers to write and execute programs using the concepts learned.
Video tutorial
Textual questions and answers
1. Define array. Why do we use arrays in computer programs?
Answer: An array is a collection of variables that are stored in contiguous memory locations in our computer.
We use arrays in computer programs because they allow us to store multiple data items of the same type together. This makes an array a convenient way to group related data. Arrays are particularly useful when we want to store and manipulate a set of items of the same data type. The elements in an array are stored in contiguous memory locations one after another, which allows efficient access and manipulation of data because the position of each element can be calculated by adding an offset to the base address of the array.
2. Can we store both integer and float types of data in a single array? Demonstrate this by writing a simple C program.
Answer: In C programming, an array can only store data of the same type. This means you cannot directly store both integer and float types of data in a single array. However, you can create an array of a structure that contains both integer and float fields. Here’s an example:
#include <stdio.h>
typedef struct {
int intValue;
float floatValue;
} IntAndFloat;
int main() {
IntAndFloat array[2];
array[0].intValue = 1;
array[0].floatValue = 1.1;
array[1].intValue = 2;
array[1].floatValue = 2.2;
printf(“Integer: %d, Float: %f\n”, array[0].intValue, array[0].floatValue);
printf(“Integer: %d, Float: %f\n”, array[1].intValue, array[1].floatValue);
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’, };
Answer: The index of the first element of the array is 0 and the index of the last element of the array is 6. This is because array indices in C start at 0 and end at n-1, where n is the size of the array. In this case, the size of the array is 7, so the indices range from 0 to 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 8th element of the array. Hint: display num[7] if num is the name of the array. Analyze the output.
Answer: This program will take input for an array of size 7 and try to display the 8th element. However, as arrays in C are 0-indexed, there is no 8th element in an array of size 7, so this will likely result in undefined behavior.
#include <stdio.h>
int main() {
int num[7];
for(int i = 0; i < 7; i++) {
printf(“Enter a number: “);
scanf(“%d”, &num[i]);
}
printf(“The 8th element is: %d\n”, num[7]);
return 0;
}
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.
Answer:
#include <stdio.h>
int main() {
int num[7];
for(int i = 0; i < 7; i++) {
printf(“Address of element %d: %p\n”, i, &num[i]);
}
return 0;
}
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.
Answer:
#include <stdio.h>
int main() {
int num1[7], num2[7];
for(int i = 0; i < 7; i++) {
printf(“Enter a number: “);
scanf(“%d”, &num1[i]);
num2[i] = num1[i];
}
printf(“Elements of the second array: “);
for(int i = 0; i < 7; i++) {
printf(“%d “, num2[i]);
}
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, 7, 8), the output of the program will be 20.
Answer:
#include <stdio.h>
int main() {
int num[] = {1, 2, 4, 3, 5, 6, 7, 7, 8};
int sum = 0;
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
if(num[i] % 2 == 0) {
sum += num[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, 7, 8), the output of the program will be 18.
Answer:
#include <stdio.h>
int main() {
int num[] = {1, 2, 4, 3, 5, 6, 7, 7, 8};
int sum = 0;
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
if(i % 2 == 0) {
sum += num[i];
}
}
printf(“Sum of numbers at even positions: %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.
Answer:
#include <stdio.h>
int main() {
int num[] = {1, 2, 3, 4, 5, 1, 2, 3};
int toReplace = 3;
int replacement = 0;
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
if(num[i] == toReplace) {
num[i] = replacement;
break;
}
}
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
printf(“%d “, num[i]);
}
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 program incorporating the logic.
Answer:
#include <stdio.h>
int main() {
int num[] = {1, 2, 3, 4, 5, 1, 2, 3};
int toReplace = 3;
int replacement = 0;
int lastIndex = -1;
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
if(num[i] == toReplace) {
lastIndex = i;
}
}
if(lastIndex != -1) {
num[lastIndex] = replacement;
}
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
printf(“%d “, num[i]);
}
return 0;
}
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, 5, 7, 1, 9), it becomes {1, 0, 3, 0, 5, 0, 7, 0, 9}.
Answer:
#include <stdio.h>
int main() {
int num[] = {1, 2, 3, 9, 5, 5, 7, 1, 9};
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
if(i % 2 != 0) {
num[i] = 0;
}
}
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
printf(“%d “, num[i]);
}
return 0;
}
12. Write a 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 a C program for the same.
Answer:
#include <stdio.h>
int main() {
int num[] = {1, 2, 3, 9, 5, 5, 7, 1, 9};
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
if(num[i] % 2 != 0) {
num[i] = 0;
}
}
for(int i = 0; i < sizeof(num)/sizeof(num[0]); i++) {
printf(“%d “, num[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.
Answer:
#include <stdio.h>
int main() {
char myName[] = “Your Name”;
char mothersName[] = “Your Mother’s Name”;
printf(“My name is: %s\n”, myName);
printf(“My mother’s name is: %s\n”, mothersName);
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 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.
Answer:
#include <stdio.h>
int main() {
int marks1[10], marks2[10], marks3[10], average[10];
for(int i = 0; i < 10; i++) {
printf(“Enter marks for student %d in 3 subjects: “, i+1);
scanf(“%d %d %d”, &marks1[i], &marks2[i], &marks3[i]);
average[i] = (marks1[i] + marks2[i] + marks3[i]) / 3;
printf(“Average marks for student %d: %d “, i+1, average[i]);
if(average[i] >= 45) {
printf(“(PASS)\n”);
} else {
printf(“(FAIL)\n”);
}
}
return 0;
}
15. Write any two limitations of arrays. Can you store your name and roll number in the same aray?
Answer: Two limitations of arrays are:
The size of an array is fixed at the time of declaration and cannot be changed during runtime. This means that if you declare an array to hold 10 elements, it can only hold 10 elements. If you need to store more elements, you would have to declare a new array with a larger size.
Arrays can only store data of the same type. This means that you cannot store different types of data in the same array. For example, you cannot store both integers and strings in the same array.
No, you cannot store your name (a string) and roll number (an integer) in the same array because arrays in C can only hold elements of the same data type.
Ron’e Dutta is a journalist, teacher, aspiring novelist, and blogger. He manages Online Free Notes and reads Victorian literature. His favourite book is Wuthering Heights by Emily Bronte and he hopes to travel the world. Get in touch with him by sending him a friend request.
Get notes of other boards, classes, and subjects
I need notes