Pageviews

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];