Arrays in C++: Declaration, Initialization, and Accessing Elements, Study notes of Computer Vision

An introduction to arrays in C++ programming, explaining what arrays are, how to declare them, and how to initialize and access their elements. It covers integer, float, and character arrays, as well as array initialization and memory allocation. It also discusses string handling using arrays and the disadvantages of using cin for string input.

Typology: Study notes

2019/2020

Uploaded on 09/21/2021

syray-byla
syray-byla 🇵🇰

1 document

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 2
Arrays
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Arrays in C++: Declaration, Initialization, and Accessing Elements and more Study notes Computer Vision in PDF only on Docsity!

Chapter 2

Arrays

What is an array?

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

How to declare an array (array

declaration)?

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

  1. How to declare integer array? ● int num[10]; ●
  2. How to declare float array? ● float num[10]; ●
  3. How to declare character array? ● char name[10];

What is array initialization?

● 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

Accessing elements of arrays

● 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

● 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

● 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 “);

Previous Questions

● 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

Previous Questions

● 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