You are on page 1of 4

CS101.

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

Array, after getting reversed

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

// reversing the array :

for (int i =0; i<n/2; i++)


{
int temp;
Page 1 of 4
temp = A[i];
A[i] = A[n-i-1];
A[n-i-1] = temp;
}

// printing the array after reversed

cout << "Printing the reversed array : " << endl;


for (int i = 0;i < n ; i++)
{
cout << A[i] << " " ;
}
cout << endl;

return 0;
}

Does this program handle arrays of even as well as odd lengths?


------------------------------------------------------------------------------------------------------------------------

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.

Input: 10 integers followed by another integer


Ouput: Display the index at which the number was found in the array.

//Program array_set1_q2.cpp
#include <iostream>
using namespace std;

int linear_search (int array[10], int element)


{
int i=0;
for (i=0;i<10;i++)
if (array[i]==element)
break;
if (i<10)
return i;
else
return -1;
}

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.

Input: The number of students, followed by marks of each student.


Output: The average of class

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

Input: A text string


Output: Upper case string

//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

You might also like