Arrays
Arrays
1
Eg:- char name[20]; 6. Printing all the elements of an array is an
cout<< “Enter a name :”; example for _____ operation.
gets(name); 7. Write down the output of the following code
Here the first statement declares a string segment.
variable „name‟ which can store 19 valid puts(“hello”);
characters (the remaining one character is „\0‟ puts(“friends”);
with which the string is terminated). 8. Write the initialisation statement to store the
puts( ): It is a console output function used to string "GCC".
write or display a string on the screen. The 9. Define an Array.
puts() function advances the cursor to the new 10. What does the declaration
line. int studlist[1000]; mean?
Syntax: puts(string_data); 11. How is memory allocated for a single
Eg:- char name[20]; dimensional array?
cout<< “Enter Your name :”; 12. Write C++ statements to accept an array
gets(name); of 10 elements and display the count of even
puts(“Your name is :”); and odd numbers in it.
puts(name); 13. Read the following statements:
Note: (1) gets() function can read strings char name[20];
containing white spaces. This is not possible cin>>name;
with >> operator. cout<<name;
(2) To use gets() and puts(), we have to What will be the output, if you input the
include the header file cstdio string “Sachin Tendulkar”? Justify your
(3) getline( ) is also used to read a string from answer.
the keyboard. It is used with the input file 14. Write C++ statements to accept two single
stream object cin and its header file is dimensional arrays of equal length and find
iostream. the difference between corresponding
elements.
Eg:- char name[20]; 15. Write a program to check whether a string
cin.getline(name,19); is a palindrome or not.
Sample Questions
1. The elements of an array with ten elements
are numbered from ____ to ____.
2. An array element is accessed using _____.
3. If AR is an array, which element will be
referenced using AR[7]?
4. Consider the array declaration
int a[3]={2,3,4};
What is the value of a[1]?
5. Consider the array declaration
int a[]={1,2,4};
What is the value of a[1]?