Vishwa Bharati Public School ,Noida

Total Page:16

File Type:pdf, Size:1020Kb

Vishwa Bharati Public School ,Noida

Vishwa Bharati Public School ,Noida Computer Science , Class XI Arrays Short questions: 1) What are arrays? Give one example situation in which you may use arrays?

2) How is one-dimensional and 2-dimensional arrays represented in memory? Explain with suitable examples

3) How are the elements of a 1-D and a 2-D array referred ? Give examples.

4) What do you understand by the base type of an array?

5) Strings are treated as 1-D arrays in C++. Comment with examples

6) How are character arrays different from strings, if so?

7) Declare an array to store names of first 10 roll numbers of your class. Assume the names to be of 25 characters at the most.

8) Which of the following declarations will produce error and why-

a. int a[9]; b. char x[5]={‘a’,’b’,’c’,’d’,’e’,’f’}; c. float marks[2]; d. char [3][]={“Programs”,”in”,”C++”); e. int d[]={1,2,3};

9. Identify the error in following code segments:

a. #include void main()

{

int I;

int a[6]={0,1,8,7,6,5};

for (I=0;I<15;I++)

{cout<

b. #include void main() {

int sum[6,3];

int i,j;

sum[1,1]=0;

cout<

}

10. What will be the output of the following program? #include void main() { int A[5] ={0, 3, 4, 2, 1}; int b[5]= {0}; int n =4; for ( int i=0; i<5;++i) b[a[i]]=a[i]; cout<< b[0]<

11. Consider the following declarations and identify the errors in the following and also write the equivalent code to perform the equivalent function:

a. char s1[5]=”Abc”, s2[5];

a. s2=s1;

cout<

b. if(s1==s2)

cout<<”equal”;

c. char s3=s1+s2;

cout<

12. Write the output of the following program:

#include #include #include void convert (char str[], int len) { for (int count =0; count

13. Give the output of the following program :

#include #include main( ) { int number[10],a,b,c,d; clrscr( ); for(int i=0;i<10;i++) { number[i]=i+i; } clrscr( ); for(int j=0;j<9;j++) { for(int k=j+1;k<10;k++) { if (number[j]>number[k]) { a=number[j]; number[j]=number[k]; number[k]=a; } } } cout<

cout<

getch( );

return 0;

}

14. Give the output of the following program :

#include #include main( ) { int a=0; clrscr( ); char name[20] = “Internet”; for(a=0;a<=8;a++) cout<

15. Write a program to calculate the length of a string without using strlen function.

16. Write a program to find out the number of occurrences of a character in a string Eg : if user inputs string as “Science” and character as ‘e’ The output should be: 2

17. Write a program to compare 2 strings without using strcmp function

18. Write a program in C++ to count the number of uppercase alphabets present in a given string.

19. Write a program in C++ to count the number of lowercase alphabets present in a given string. 20. Write a program in C++ to count the number of alphabets present in a text file “XY.TXT”. 21. Write a program in C++ to count and display the number of blank spaces present in astring” “ I Like C++”; 22. Write a function in C++ to count and display the number of lines starting with alphabet ‘A’ in a given lines of text:For example : output 2 in the given lines . A fox is in the jungle. the tiger is in the den. A roses is red sky is blue. 30. Write a function in C++ to search for given character in the string entered for example : Seacrh for character ‘d’ in “ Delhi daytimes”.

Also tell the position of the given character.

Long questions:

1. Write a function in C++ which will accept a 2 D Array of integer and return the sum of all the elements divisible by 5 that lie on the even row number. 2. Write a program to multiply two given 2D Matrices using functions. 3. Write a user defined function named upperhalf( ) which takes a 2D array A, with size n rows and n cols as arguments and print the upper half of the matrix 1 2 3 output 1 2 3

6 7 8 7 8

2 3 4 4 4. Write a user defined function named lowerhalf () which takes a 2D array, with size n, n rows and n cols as arguments and prints the lower half of the matrix. Eg;

1 2 3 output 1

5 6 7 5 6

9 1 2 9 1 2

5. Write a function in C++ which accepts an integer array and its size as arguments and replaces elements having even values with its half and elements having odd values with twice its value . eg:

if the array contains

3, 4, 5, 16, 9

then the function should be rearranged as

6, 2,10,8, 18

6. Write a function in C++ which accepts an integer array and its size as argument / parameters and assign the elements into a two dimensional array of integers in the following format. If the array is 1 2 3 4 5 6

1 2 3 4 5 6

1 2 3 4 5 0

1 2 3 4 0 0

1 2 3 0 0 0

1 2 0 0 0 0

1 0 0 0 0 0

7. Write a function in C++ which accepts a 2D array of integers and ts size as arguments and displays the elements of middle row and the elements of middle column. Assuming the 2D array to be a square matrix with odd dimensions i.e., 3x3, 5x5, 7x7 eg., 4 5 6

7 8 9

3 2 1 output through the function should be middle row 7 8 9 middle col. 5 8 2

Recommended publications