Professional Documents
Culture Documents
1X
Practice Problems on Arrays, Set - 1
Instructions: Study the following problems and also study the sample programs
written by our course staff to solve these. Visit the course home page. Locate the
appropriate link for program files (.cpp), and download these. Compile and execute
the programs. Think of modifications, which you would like to make. Edit the
programs, recompile and execute each one again. Test each program with different
values of input. You should note down the execution results
Problem-01: Write a C++ program that reverses the elements of the array. Take the
size of array as input from the user. e.g.
Original Array
0 1 2 3 4 5 6 7 8 9
12 6 5 1 8 15 11 19 3 4
0 1 2 3 4 5 6 7 8 9
4 3 19 11 15 8 1 5 6 12
//Program array_set1_q1.cpp
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the size of the array : ";
cin >> n;
int A[n];
for (int i = 0 ; i < n ; i++)
{
cout << "Enter element no " << i+1 <<" : ";
cin >> A[i];
}
return 0;
}
Problem-02: The following C++ program reads and stores 10 numbers taken from the
user, in an array. It then checks whether a particular number, given separately, is
present in these 10 numbers or not. It uses a search technique known as linear
search, so called because it sequentially compares the given number, with each
element of the array.
//Program array_set1_q2.cpp
#include <iostream>
using namespace std;
int main ()
{
int array[10], element=0, i=0;
Page 2 of 4
cout<<"Program to find an element in the array using linear search
technique"<<endl;
cout<<"Enter 10 elements present in the array:"<<endl;
for (i=0;i<10;i++)
cin>>array[i];
cout<<"Enter the element to be searched : ";
cin>>element;
cout<<endl;
int answer=linear_search(array, element);
if (answer<0)
cout<<"The element is not present in the array."<<endl;
else
cout<<"The element exists at array index "<<answer<<endl;
return 0;
}
What happens if a number is present more than once? Does the program report the
last occurrence of such duplicate number? If not, modify it to report the last
occurrence.
------------------------------------------------------------------------------------------------------------------------
Problem-03: Write a C++ program that finds the class average of marks of all
students, in a subject.
//Program array_set1_q3.cpp
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i,no_students;
cout<<"Enter the number of students : ";
cin>>no_students;
float marks[no_students];
float sum = 0;
cout<<"Enter the marks of students one by one : "<<endl;
for(i=0;i<no_students;++i) //reading the no.s and finding sum..
{
cin>>marks[i];
sum=sum+marks[i];
}
cout << "The average of entire class is : "<<sum/no_students<<endl;
return 0 ;
Page 3 of 4
}
----------------------------------------------------------------------------------------------------------------
Problem-04: Write a C++ program that capitalizes the text given to it as input.
//Program array_set1_q4.cpp
#include <iostream>
using namespace std;
int main()
{
char array[100];
int i;
cout<<"Enter a text string : ";
cin>>array;
i=0;
while(array[i]!='\0')
{
if(array[i]>=97 && array[i]<=122)
{
array[i]=array[i]-32;
}
i++;
}
cout<<"Upper case string is : "<<array<<endl;
return 0;
}
What happens respectively when you give a string with spaces, and without spaces?
---------------------------------------------------------------------------------------------------------------
Page 4 of 4