[go: up one dir, main page]

0% found this document useful (0 votes)
93 views9 pages

Arrays

The document provides a comprehensive overview of arrays in C++, including definitions, declarations, initializations, and various operations such as traversal and input/output methods. It includes example C++ code snippets for array manipulation, as well as explanations of related concepts like memory allocation and string handling. Additionally, it covers practical exercises for finding differences between arrays, checking for palindromes, counting even and odd numbers, and finding the largest and smallest elements in an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views9 pages

Arrays

The document provides a comprehensive overview of arrays in C++, including definitions, declarations, initializations, and various operations such as traversal and input/output methods. It includes example C++ code snippets for array manipulation, as well as explanations of related concepts like memory allocation and string handling. Additionally, it covers practical exercises for finding differences between arrays, checking for palindromes, counting even and odd numbers, and finding the largest and smallest elements in an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1

2. ARRAYS
1. What is 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];
2. 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];
3. What is array initialization?

Giving values to the array elements at the time of array declaration is known as array initialization
4. 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};
5. Write a C++ code to input values into the array: int ar[50];

Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
2

int i;
for (i=0;i<50;i++)
{
cout << ”Enter value”;
cin << ar[i];
}
6. Write a C++ code fragment to display the elements in the even positions of the array
float val[100];

int i;
for (i = 0; i <100; i += 2)
{
cout << ”Enter value”;
cin << ar[i];
}
7. What is array traversal?

Accessing each element of an array at least once to perform any operation


is known as traversal operation. Eg. Displaying all elements of an array.
8. The elements of an array with ten elements are numbered from ____ to ____.

0 to 9

9. An array element is accessed using _____.

Index value

10. What is the use of gets() and puts() functions?

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.

Syntax: gets(String_data)

Eg. gets(str);

puts() function is used to display a string data on a standard output device(eg. Monitor)

Syntax: puts(String_data)

Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
3

Eg. puts(“ Hello “);

11. Which header file in C++ is need for gets() and puts() function?

cstdio

12. If AR is an array, which element will be referenced using AR[7]?

Element at the position 8.

13. Consider the array declaration int a[3]={2,3,4}; What is the value of a[1]?

14. Consider the array declaration int a[]={1,2,4}; What is the value of a[1]?

15. Printing all the elements of an array is an example for _____ operation.

Array traversal

16. What does the declaration int studlist[1000] mean?

studlist is an array which can hold 1000 integer values.

17. 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.

18. 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.
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
4

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.

19. A string can be considered as an array of ------.

Characters.

20. A ---------- is stored at the end of the string.

null character '\0'

21. 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

Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
5

elementis :" << n[i]<<"and the Second Array Element is :"<<m[i]<<endl;


}
return 0;
}

22. 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";
return 0;
23. Write C++ statements to accept an array of 10 elements and display the count
Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
6

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;
}
24. Write a C++ program to find the largest and smallest numbers in an array: int n[20]

#include <iostream>

using namespace std;

Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
7

int main()
{
int a[5], i, big,small;

cout<<"Enter the elements of the array :";

for(i=0; i<5; i++)

cin >> a[i];

big = a[0];

small=a[0];

for(i=1; i<5; i++)


{
if (a[i] > big)

big = a[i];
if (a[i]< small
small=a[i];
}
cout<<"\n The biggest element is " << big;
cout<<"\n The smallest element is " << small;
return 0;
}
25. Write a C++ program to find the number of vowels in a given string.

#include <iostream>
#include<cstdio>

using namespace std;

int main()
{
int n = 0;
char str[];

cout<<"Enter the string : ";

gets(str);
for ( i = 1; str[i] != ’\0’; i++)

Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
8

switch ( str[i] )
{
case ‘A’ :
case ‘a’ :

case ‘E’ :

case ‘e’ :

case ‘I’ :

case ‘i’ :

case ‘O’ :

case ‘o’ :
case ‘U’ :

case ‘u’ : n++;

cout<<" The no of vowels in the string “ << str << “ is " << n;
return 0;
}

Previous Questions:
1. How memory is allocated for a float array.

2. How we can initialize an integer array ?Give an example.

3.Write a C++ program to accept a string and find its length without using built in
function. Use a character array to store the string .

4.Write a program to input ‘N’ numbers into an integer array and find the largest
number in the array.

5.Define an array.Give an example of an integer array declaration.

6. Consider the following C++ code

char text[20];

cin≫text;

Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]
9

cout≪text;

If the input string is “Computer Programming”; what will be the output ?justify your
answer.

Second Year Computer Application (Commerce) Study Notes by Anil Kumar [HSSLiVE.IN]

You might also like