+1 and +2 Computer Science

Previous Question paper answered , and Questions from Textbook

+1 and +2 Computer Application

Previous Question paper answered , and Questions from Textbook

Diploma in Computer Application (DCA)

Previous Question paper answered , and Questions from Textbook.

Master of Computer Applications (MCA)

Previous Question paper answered , and Questions from Textbook

True IQ Computer Online Academy

Contact : +91 9036433020 , mail2trueiq@gmail.com

Pageviews

Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

Tuesday, 28 July 2020

+1].8- Arrays Solved Questions chapter wise



                                       PLUS ONE COMPUTER SCIENCE


                           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>
#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
 C++ program subtract two matrices

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
Enter matrix:
3 5 9
15 6 0
12 4 7
Highest Element:15
Lowest Element:0




Saturday, 2 November 2019

+1] 8 - Arrays - Previous Questions Chapter wise



                                 PLUS ONE COMPUTER SCIENCE

            First Year Computer Science Previous Questions Chapter wise ..
                                                      
                                                          Chapter 8. Arrays


1. An array is declared as follows: int a[5] = {1, 2, 3, 4, 5}; What will be the value of a[2] + a[3]?

 a) 7

 b) 5

c) 6

d) 4

 Ans. a

2. Write C++ statements to double each element of the array int A[100].

 3. Consider an array A with the following elements sorted in ascending order. 5, 9, 10, 13, 16, 17, 19, 20, 25, 37 Explain the working of binary search algorithm to search 25. How many comparisons are required for this searching?

 4. Find the value of score[4] based on the following declaration statement:

 int score[5] = {98, 87, 92, 79, 85};

     Ans. 85

5. Suppose M[5][5] is a 2D array that contains the elements of a square matrix. Write C++ statements to find the sum of the diagonal elements.

     #include <iostream>
using namespace std;
int main()
{ int mat[5][5], n, i, j, s=0;
cout<<"Enter the rows/columns of square matrix: ";
cin>>n;
cout<<"Enter the elements\n";
for(i=0; i<n; i++)
for(j=0; j<n; j++)
cin>>mat[i][j];
cout<<"Major diagonal elements are\n";
for(i=0; i<n; i++)
{
cout<<mat[i][i]<<"\t";
s = s + mat[i][i];
}
cout<<"\nSum of major diagonal elements is: ";
cout<<s;
return 0;
}

6. Write an algorithm for arranging elements of an array in ascending order using bubble sort.

       Step 1. Start

      Step 2. Accept a value in N as the number of elements of the array

      Step 3. Accept N elements into the array AR

     Step 4. Repeat Steps 5 to 7, (N - 1) times

      Step 5. Repeat Step 6 until the second last element of the list

        Step 6. Starting from the first position, compare two adjacent elements in the list. If they are not in proper order, swap the elements.

      Step 7. Revise the list by excluding the last element in the current list.

     Step 8. Print the sorted array AR

        Step 9. Stop

 7. ………………. search method is an example for ‘divide and conquer method’.

   Ans. Binary search

8. What is an array? Write C++ program to declare and use a single dimensional array for storing the computer science marks of all students in your class.

       An array is a collection of elements of the same type placed in contiguous memory locations. Arrays are used to store a set of values of the same type under a single variable name. Each element in an array can be accessed using its position in the list called index number or subscript.

9. a) Write C++ program for sorting a list of numbers using Bubble sort method.

    b) Write different passes of sorting the following numbers using Bubble sort:

         32, 21, 9, 17, 5

    Ans. a.

  #include <iostream>
using namespace std;
int main()
{
int AR[25],N;
int I, J, TEMP;
cout<<"How many elements? ";
cin>>N;
cout<<"Enter the array elements: ";
for(I=0; I<N; I++)
cin>>AR[I];
for(I=1; I<N; I++)
for(J=0; J<N–I; J++)
if(AR[J] > AR[J+1])
{
TEMP = AR[J];
AR[J] = AR[J+1];
AR[J+1] = TEMP;
}
cout<<"Sorted array is: ";
for(I=0; I<N; I++)
cout<<AR[I]<<"\t";
}

10. Declare a two dimensional array to store the elements of a matrix with order 3 x 5.

    Ans. int M[3][5];

11. Define an array. Also write an algorithm for searching an element in the array using any one method that you are familiar with.

        An array is a collection of elements of the same type placed in contiguous memory locations. Arrays are used to store a set of values of the same type under a single variable name. Each element in an array can be accessed using its position in the list called index number or subscript.

      Algorithm for Linear Search

 Step 1. Start

Step 2. Accept a value in N as the number of elements of the array

 Step 3. Accept N elements into the array AR

Step 4. Accept the value to be searched in the variable ITEM

 Step 5. Set LOC = -1

 Step 6. Starting from the first position, repeat Step 7 until the last element

Step 7. Check whether the value in ITEM is found in the current position. If found then store the position in LOC and Go to Step 8, else move to the next position.

 Step 8. If the value of LOC is less than 0 then display "Not Found", else display the value of LOC + 1 as the position of the search value.

 Step 9. Stop

12. int num[10];

The above C++ statement declares an array named num that can store maximum …. integer location.

 a) 9

b) 10

c) N

d) none of these

 Ans. b

 13. Explain the memory allocation for the following declaration statement. int A[10][10];

 14. Write a C++ program to illustrate array traversal.

         #include<iostream>

      using namespace std; 

      int main() 

      { 

          int a[10], i; 

      cout<<"Enter the elements of the array :";

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

       cin >> a[i];

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

     a[i] = a[i] + 1;

     cout<<"\nEntered elements of the array are...\n"; 

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

     cout<< a[i]<< "\t";

    return 0;

  }

15. Read the following C++ statement: int MAT[5][4];

(a) How many bytes will be allocated for this array?

(b) Suppose MAT[4][4] is a 2D array that contains the elements of a square matrix. Write C++ statements to find the sum of all the elements in the array.

    Ans. 
            a. 4*5*4=80 bytes

16. Write the names of two searching methods in arrays. Prepare a chart that shows the comparison of two searching methods.

        Linear search and Binary search.

         Linear search method                                                 Binary search method 

  • The elements need not be in any order                           • The elements should be in sorted order

 • Takes more time for the process                                     • Takes very less time for the process

 • May need to visit all the elements                                   • All the elements are never visited

 • Suitable when the array is small                                      • Suitable when the array is large


Friday, 5 July 2019

+2 ] 2. Arrays - Solved Questions from Text Book

Chapter 2: Array
(+2 Computer Application , Text book Questions and Answers)



1. Write array declarations for the following:

a. Scores of 100 students : int score[100];

b. English letters : char a[10];

c. A list of 10 years : int year[12];

d. A list of 30 real numbers : float n[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

Ans: int score[10] = {89, 75, 82, 93, 78, 95, 81, 88, 77, 82};


b. A list of five amounts: 10.62, 13.98, 18.45, 12.68, and 14.76

Ans: float amt[5] = { 10.62, 13.98, 18.45, 12.68, 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.

Ans: float rate[100] = { 6.29, 6.95, 7.25, 7.35, 7.40, 7.42};


d. An array of 10 marks with value 0.

Ans: int mark[10]={NULL}; or int mark[10]={0,0,0,0,0,0,0,0,0,0};


e. An array with the letters of VIBGYOR.

Ans: char c[7]={'V','I','B','G','Y','O','R'};


f. An array with number of days in each month.

Ans: int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};

  • 3. Write a C++ code to input values into the array: int ar[50];
int i , ar[50];

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

{

cout << ”Enter value”;

cin << ar[i];

}

  • 4. Write a C++ code fragment to display the elements in the even positions of the array
float val[100];

int i;

float val[100];

for (i = 0; i <100; i += 2)

{

cout << ”Enter value”;

cin << ar[i];

}

  • 5. The elements of an array with ten elements are numbered from ____ to ____.
 Ans. 0 to 9 

  • 6. An array element is accessed using _____.

Ans. Index value

  • 7. If AR is an array, which element will be referenced using AR[7]?

  Ans.  Element at the position 8.

  • 8. Consider the array declaration int a[3]={2,3,4}; What is the value of a[1]?

     Ans. 3

  • 9. Consider the array declaration int a[]={1,2,4}; What is the value of a[1]?

   Ans. 2

  • 10. Printing all the elements of an array is an example for _____ operation.

 Ans. Array traversal

  • 11. What does the declaration int studlist[1000] mean?

studlist is an array which can hold 1000 integer values.

  • 12. How is memory allocated for a single dimensional array?

The elements of a single dimensional array will store in continues memory locations according

to its data type

Eg. int n[5]; Here consider n[0] is in the memory location of address 1000. An integer data

need 2 bytes of memory. So n[1] will be at 1002, n[2] at 1004 and so on.

  • 13. Read the following statements:

char name[20];

cin>>name;

cout<<name;

What will be the output if you input the string “Sachin Tendulkar”? Justify your answer.

Sachin. The letters of the word Sachin is stores in the array name as first 6 char elements.

The input is completed due to the blank space and the word Tendulkar is skipped.

  • 14. Write C++ statements to accept two single dimensional arrays of equal length

and find the difference between corresponding elements.

#include<iostream>

using namespace std;

int main()

{

int n[10],m[10],i;

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

{

cout <<"Enter elements for First Array ";

cin>>n[i];

}

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

{

cout <<"Enter elements for Second Array ";

cin>>m[i];

}

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

{

if(n[i] |= m[i])

cout<<endl<<" At the possition "<< i+1<< " First Array

elementis :" << n[i]<<"and the Second Array Element is :"<<m[i]<<endl;

}

return 0;

}

  • 15. Write a program to check whether a string is a palindrome or not.

#include<iostream>

#include <cstring>

using namespace std;

int main()

{

int n,i,flag=0 ;

char s[25];

cout <<"Enter the string for palindrome checking ";

cin>>s;

n=strlen(s);

for ( i = 0 ; i <= n/2 ; i++ )

{

If ( s[i] != s[ n-(i+1)] )

{

cout<<" The given string is not a palindrome " <<endl;

flag=1;

break;

}

}

if (flag==0)

cout <<"\nThe given string is a palindrome\n";

}

}

if (flag==0)

cout <<"\nThe given string is a palindrome\n";

return 0;

}

  • 16. Write C++ statements to accept an array of 10 elements and display the count

of even and odd numbers in it.

#include<iostream>

using namespace std;

int main()

{

int i,r,even=0,odd=0,no[10];

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

{

{

cout<<"Enter no ";

cin>>no[i];

}

}

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

{

r=no[i]%2;

if(r==0)

even++;

else

odd++;

}

cout<<" \n No of even numbers among the given numbers :"<<even<<endl;

cout<<" \n No of even numbers among the given numbers :"<<odd<<endl;

return 0;

}

  • 17. Write down the output of the following code segment:

puts(“hello”);

puts(“friends”);

output:

hello

friends

  • 18. Write the initialisation statement to store the string "GCC".

Ans. char[3]={'G','C'.'C'};

  • 19. Define an array?

  Arrays are used to store a set of values of the same type in contiguous memory locations under

a single variable name. Each element in an array can be accessed using its position in the list,

called index number or subscript.

Eg. int a[10];