PLUS ONE COMPUTER SCIENCE
(a) Scores of 100 students
(b) English letters
(c) A list of 10 years
(d) A list of 30 real numbers
2. Write array initialization statements for the following:
(b) A list of five amounts: 10.62, 13.98, 18.45, 12.68, and 14.76
(c) A list of 100 interest rates, with the first six rates being 6.29, 6.95, 7.25, 7.35, 7.40 and 7.42.
(d) An array of 10 marks with value 0.
(e) An array with the letters of VIBGYOR.
(f) An array with number of days in each month.
3. Write C++ code segment to input values into the array: int ar[50]
Ans.
for( int i=0;i<50;i++)
{
cin>>ar[i];
}
4. Write C++ code fragment to display the elements in the even positions of the array: float val[100];
Ans.
for( int i=0;i<100;i=i+2)
{
cin>>val[i];
}
First year computer science Solved Questions and answers
8- Arrays
1. Write array declarations for the following:
(a) Scores of 100 students
(b) English letters
(c) A list of 10 years
(d) A list of 30 real numbers
Ans. (a) int Scores[100];
(b) char letters[26];
(c) int years[10];
(d) float no[30];
2. Write array initialization statements for the following:
(a) An array of 10 scores: 89, 75, 82, 93, 78, 95, 81, 88, 77, and 82
(b) A list of five amounts: 10.62, 13.98, 18.45, 12.68, and 14.76
(c) A list of 100 interest rates, with the first six rates being 6.29, 6.95, 7.25, 7.35, 7.40 and 7.42.
(d) An array of 10 marks with value 0.
(e) An array with the letters of VIBGYOR.
(f) An array with number of days in each month.
Ans. (a) int scores[10] = {89,75,82,93,78,95,81,88,77,82};
(b) float amounts[5] = {10.62,13.98,18.45,12.68,14.76};
(c) float rates [100] = {6.29,6.95,7.25,7.35,7.40,7.42};
(d) int mark[10] = {0};
(e) char colors[7] = {'V','I','B','G','Y','O','R'};
(f) int days[12] = {31,28,31,30,31,30,31,30,31,30,31,31};
3. Write C++ code segment to input values into the array: int ar[50]
Ans.
for( int i=0;i<50;i++)
{
cin>>ar[i];
}
4. Write C++ code fragment to display the elements in the even positions of the array: float val[100];
Ans.
for( int i=0;i<100;i=i+2)
{
cin>>val[i];
}
5. All the elements in an array must be ----------- datatype.
Ans. Same
6. The elements of an array with ten elements are numbered from ---------to---------
Ans. 0 to 9
7. An array eiement is accessed using--------
Ans. Script or index
8. If AR is an array, which element will be referenced using AR[7]?
Ans. 8th element
9. Consider the array declaration int a[3]={2,3,4}; What is the value of a[1]?
Ans. 3
10. Consider the array declaration int a[ ]={1,2,4}; What is the value of a[1]?
Ans. 2
11. Consider the array declaration int a[5]={1,2,4}; What is the value of a[4]?
Ans. 0
12. Write array initialization statements for an array of 6 scores: 89, 75, 82, 93, 78, 95. 9. Printing all the elements of an array is an example for _____ operation.
Ans. Traversal
13. How many bytes are required to store the array int a[2][3];?
Ans. 2*2*3=12bytes
14. In an array of m elements, Binary search required maximum of n search for finding an element. How many search required if the number of elements is doubled?
Ans. n+1
15.Write the statement to initialize an array of 10 marks with value 0.
Ans. int Marks[10]={0,0,0,0,0,0,0,0,0,0};
16. State True or false: The compiler will complain if you try to access array element 16 in a ten-element array.
Ans. False
17. Define an Array.
Ans. An array is a collection of same type elements placed in a contigous memory locations.
18. What does the declaration int studlist[1000]; mean?
Ans. It is an integer type array with name studlist it can hold 1000 numbers.index start from 0 to 999.
19. How is memory allocated for a single dimensional array?
Ans. For single dimentional array total size allocated can be computed as total size of the bytes=size of the (array_ type)*array size.
20. Write C++ statements to accept an array of 10 elements and display the count of even and odd numbers in it.
21. Write the initialization statement for an array num with values 2, 3, 4, 5. 6. What is meant by traversal?
22. Define sorting.
Ans. Sorting is the process of arranging the elements of the array in some logical order.
23. What is searching?
Ans. Searching is a process of locating a particular element present in a given set of elements. The element may be a record, a table, or a file. A search algorithm is an algorithm that accepts an argument 'a' and tries to find an element whose value is 'a'. ... There are number of techniques available for searching.
24. What is bubble sort?
Ans. Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
25. What is binary search?
Ans. Binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array.
26. Define a 2D array.
Ans. Two-dimensional Arrays. ... A 2D array has a type such as int[][] or String[][], with two pairs of square brackets. The elements of a 2D array are arranged in rows and columns, and the new operator for 2D arrays specifies both the number of rows and the number of columns.
27. How is memory allocated for a 2D array?
Ans. The amount of storage required to holda 2 dimentional array depends upon its base type,no of rows and no.of columns.Total no.of bytes required to hold 2D array is
total bytes= size of(base type)*no.of rows*no.of columns.
28. An array AR contains the elements 25, 81, 36, 15, 45, 58, 70. Illustrate the working of binary search technique for searching an element 45.
Ref 8th chapter.
29. Write C++ statements to accept two single dimensional array of equal length and find the difference between corresponding elements.
30. Illustrate the working of bubble sort method for sorting the elements 32, 25, 44, 16, 37, 12.
Ref. the same type of bubble sort sorting method in this chapter.
31. If 24, 45, 98, 56, 76, 24, 15 are the elements of an array, illustrate the working of selection sort for sorting.
Ref this chapter.Detailed explanation and working method illustrated 8th chapter.
32. Write a program to find the difference between two matrices.
#include<iostream.h>
33. Write a program to find the sum and average of elements in a 2D array.
34. Write a program to find the largest element in a 2D array
#include<iostream>
8. If AR is an array, which element will be referenced using AR[7]?
Ans. 8th element
9. Consider the array declaration int a[3]={2,3,4}; What is the value of a[1]?
Ans. 3
10. Consider the array declaration int a[ ]={1,2,4}; What is the value of a[1]?
Ans. 2
11. Consider the array declaration int a[5]={1,2,4}; What is the value of a[4]?
Ans. 0
12. Write array initialization statements for an array of 6 scores: 89, 75, 82, 93, 78, 95. 9. Printing all the elements of an array is an example for _____ operation.
Ans. Traversal
13. How many bytes are required to store the array int a[2][3];?
Ans. 2*2*3=12bytes
14. In an array of m elements, Binary search required maximum of n search for finding an element. How many search required if the number of elements is doubled?
Ans. n+1
15.Write the statement to initialize an array of 10 marks with value 0.
Ans. int Marks[10]={0,0,0,0,0,0,0,0,0,0};
16. State True or false: The compiler will complain if you try to access array element 16 in a ten-element array.
Ans. False
17. Define an Array.
Ans. An array is a collection of same type elements placed in a contigous memory locations.
18. What does the declaration int studlist[1000]; mean?
Ans. It is an integer type array with name studlist it can hold 1000 numbers.index start from 0 to 999.
19. How is memory allocated for a single dimensional array?
Ans. For single dimentional array total size allocated can be computed as total size of the bytes=size of the (array_ type)*array size.
20. Write C++ statements to accept an array of 10 elements and display the count of even and odd numbers in it.
21. Write the initialization statement for an array num with values 2, 3, 4, 5. 6. What is meant by traversal?
22. Define sorting.
Ans. Sorting is the process of arranging the elements of the array in some logical order.
23. What is searching?
Ans. Searching is a process of locating a particular element present in a given set of elements. The element may be a record, a table, or a file. A search algorithm is an algorithm that accepts an argument 'a' and tries to find an element whose value is 'a'. ... There are number of techniques available for searching.
24. What is bubble sort?
Ans. Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
25. What is binary search?
Ans. Binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array.
26. Define a 2D array.
Ans. Two-dimensional Arrays. ... A 2D array has a type such as int[][] or String[][], with two pairs of square brackets. The elements of a 2D array are arranged in rows and columns, and the new operator for 2D arrays specifies both the number of rows and the number of columns.
27. How is memory allocated for a 2D array?
Ans. The amount of storage required to holda 2 dimentional array depends upon its base type,no of rows and no.of columns.Total no.of bytes required to hold 2D array is
total bytes= size of(base type)*no.of rows*no.of columns.
28. An array AR contains the elements 25, 81, 36, 15, 45, 58, 70. Illustrate the working of binary search technique for searching an element 45.
Ref 8th chapter.
29. Write C++ statements to accept two single dimensional array of equal length and find the difference between corresponding elements.
30. Illustrate the working of bubble sort method for sorting the elements 32, 25, 44, 16, 37, 12.
Ref. the same type of bubble sort sorting method in this chapter.
31. If 24, 45, 98, 56, 76, 24, 15 are the elements of an array, illustrate the working of selection sort for sorting.
Ref this chapter.Detailed explanation and working method illustrated 8th chapter.
32. Write a program to find the difference between two matrices.
#include<iostream.h>
#include<conio.h> void main() { clrscr(); int arr1[3][3], arr2[3][3], arr3[3][3], sub, i, j; cout<<"Enter 3*3 Array 1 Elements : "; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>arr1[i][j]; } } cout<<"Enter 3*3 Array 2 Elements : "; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>arr2[i][j]; } } cout<<"Subtracting array (array1-array2) ... \n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr3[i][j]=arr1[i][j]-arr2[i][j]; } } cout<<"Result of Array1 - Array2 is :\n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cout<<arr3[i][j]<<" "; } cout<<"\n"; } getch(); }
Result
33. Write a program to find the sum and average of elements in a 2D array.
34. Write a program to find the largest element in a 2D array
#include<iostream>
using namespace std;
int main()
{
int m,n,a[10][10],i,j,high,low;
cout<<"Enter no. of rows and coloumns:";
cin>>m>>n;
cout<<"\nEnter matrix:\n";
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
cin>>a[i][j];
}
high=a[0][0];
low=a[0][0];
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
if(a[i][j]>high)
high=a[i][j];
else
if(a[i][j]<low)
low=a[i][j];
}
}
cout<<"\nHeighst Element:"<<high<<"\nLowest Element:"<<low<<"\n";
return 0;
}
Output
Enter no. of rows and columns:3
3
3
Enter matrix:
3 5 9
15 6 0
12 4 7
3 5 9
15 6 0
12 4 7
Highest Element:15
Lowest Element:0
Lowest Element:0