You are on page 1of 6

Book Questions

Question 2 page 97
write a program that take input of ID,age, name & marks of candidates only
store the record of those candidates whose age is less than 20 also display the
candidate's serial number where total number of candidates are 10 using
Arrays in C++

Solution

#include <iostream.h>

main()

int
i=0,ageTemp=0,marksTemp=0,ageR=20,candidateIDTemp=0;

int age[100],marks[100],candidateID[100];

while(i<10)

cout << " Enter Your ID = ";

cin >> candidateIDTemp;

cout << " Enter Your Age = ";

cin >> ageTemp;

cout << " Enter Your Marks = ";

cin >> marksTemp;

if(ageTemp<=ageR)

{
candidateID[i]=candidateIDTemp;

age[i]=ageTemp;

marks[i]=marksTemp;

i++;

cout <<"Accepted List \n";

cout<< "candidateID\t\tage\tMark\n";

for(int j=0;j<i;j++)

cout<<
candidateID[j]<<"\t"<<age[j]<<"\t"<<marks[j]<<"\n";

return 0;

Question 3 page 97

Write a recursive function that finds and returns the minimum element in
an array, where the array and its size are given as parameters.
#include <iostream.h>

int min(int *arr,int size)

{
if (size == 0)

return arr[0];

if(arr[size-1] < min(arr , size -1))

return arr[size -1];

else

return (min(arr , size -1));

int main(){

int size;

cout<<"enter size of arr : \n";

cin>>size;

cout<<"enter elements ";

int * arr = new int[size];

for(int i=0; i < size ; i++){

cin>>arr[i];

}
cout<<"\nMinimum number is : "<<min(arr,size)<<endl;

return 0;

Question 23 page 132


Write a program that accept numbers as an input from user and to sort
them in ascending order.
#include <iostream.h>
int main()
{
int i, j,k,temp,count, number[100];
cout<<"How many numbers you are gonna enter:\n";
cin>>count;
cout<<"\nEnter the numbers one by one:";
for (i = 0; i < count; ++i)
cin>>number[i];
for (j = 0; j < count; ++j)
{
for (k = j + 1; k < count; ++k)
{
if (number[j] > number[k])
{
temp = number[j];
number[j] = number[k];
number[k] = temp;
}
}
}
cout<<"Numbers in ascending order:\n";
for (i = 0; i < count; ++i)
cout<< number[i]<<"\t";
return 0;}

Question 13 page 124


Write a function that displays at the left margin of the screen a solid square
of asterisks whose side is specified in integer parameter side. For example, if
side is 4, the function displays
Solution
#include <iostream.h>
void square( int s )
{
for ( int row = 1; row <= s; ++row ) {
for ( int col = 1; col <= s; ++col )
cout << '*';
cout << '\n';
}
}
int main()
{
int side;
cout << "Enter side: ";
cin >> side;
cout << '\n';
square( side );
cout << endl;
return 0;
}

You might also like