









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










● 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. ● Eg: int num[10];
● Syntax: data_type array_name[size]; ● data_type is the type of data that the array variable can store ● array_name is an identifier for naming the array and the size is a positive integer number that specifies the number of elements in the array. ● Eg: int num[10];
●
● Giving values to the array elements at the time of array declaration is known as array initialization. ● Eg: ● int score[5] = {98, 87, 92, 79, 85}; ● char code[6] = {‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’}; ● float wgpa[7] = {9.60, 6.43, 8.50, 8.65, 5.89, 7.56, 8.22};
● Q1) How we can initialize an integer array ?Give an example. ● int score[5] = {98, 87, 92, 79, 85}; ● Q2) How we can initialize a character array ?Give an example. ● char code[6] = {‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’}; ● Q3) How we can initialize a float array ?Give an example. ● float wgpa[7] = {9.60, 6.43, 8.50, 8.65, 5.89, 7.56, 8.22};
● Q1) Find out the space allotted for char name[5] ● total_bytes= 1 x 5 =5 bytes ● Q2) Find out the space alloted for int num[10] ● total_bytes= 4 x 10 = 40 bytes ● Q3) Find out the space alloted for double num[10] ● total_bytes= 8 x 10 = 80 bytes
● The process of accessing each element of an array is called Array Traversal. ● Any element can be accessed by giving the array’s name and the element’s position. This position is called the index or subscript value.
● gets() function is used to accept a string of characters including whitespace from a standard input device(eg. Keyboard) and store it in a character array. ● cstdio header file is required. ● Syntax: gets(String_data) ● Eg: gets(str);
● puts() function is used to display a string data on a standard output device(eg. Monitor) ● cstdio header file is required. ● Syntax: puts(String_data) ● Eg: puts(“ Hello “);
● Q6) Define an array.Give an example of an integer array declaration. ● Q7) Consider the following C++ code char text[20]; cin ≫text; text; cout ≪text; text; If the input string is “Computer Programming”; what will be the output? justify your answer. ● Computer
● Q8) What is the differences in string handling using cin and gets() in C++ programs? ● cin cant read white space. gets() can read white space. ● Q9) i) Write C++ statement to declare a character array of size 20 ● char text[20]; ● (^) ii) Write C++ statement to store the string “welcome” in the same array ● char text[20]=”welcome”; ● Q10) Initialize an integer array with 5 elements ● (^) int num[5]={2,3,5,7,11}; ● (^) Q11) Write a program in C++ to accept a string with white space like “good morning” from the keyboard and display the same string