0% found this document useful (0 votes)
61 views

Arrays in C Programming Exercises

The document provides 10 programming exercises for working with C++ arrays. The exercises cover printing array elements, finding maximum/minimum/average values, inserting new elements, and sorting arrays. For each exercise, sample code is provided and sample input/output is described to test the programs. The exercises increase in complexity and cover a variety of common array operations and manipulations in C++.

Uploaded by

Isagani Alonzo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Arrays in C Programming Exercises

The document provides 10 programming exercises for working with C++ arrays. The exercises cover printing array elements, finding maximum/minimum/average values, inserting new elements, and sorting arrays. For each exercise, sample code is provided and sample input/output is described to test the programs. The exercises increase in complexity and cover a variety of common array operations and manipulations in C++.

Uploaded by

Isagani Alonzo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C++ ARRAYS PROGRAMMING EXERCISES

1. Create a C++ program that will print the element of an array. Filename: Value

Sample Test Data

Array elements size is 15


Enter index number: 4

Expected Output:
Array[4]= 65

2. Write a program in C++ to print all elements in an array. Filename: Element


Sample Test Data: 
Input the number of elements to be stored in the array :3 
Input 3 elements in the array : 
element - 0 : 1 
element - 1 : 5 
element - 2 : 1 

Expected Output : 
151

3. Create a C++ program that will find an element in an array and display the
location of the said element. Filename: Find

Sample Test Data

Element: 67

Expected Output:
Location: Array[9]

4. Filename: SumArray
#include <iostream>
using namespace std;
int main(){
int array[10]={1,2,3,4,5,6,7,8,9,0};
int sum, loop;
sum=0;
for(loop =9; loop >=0; loop--){
sum= sum + array[loop];
}
cout<<"Sum of array is " <<sum;
return0;
}

5. Write your own C++ program that calculates the average of a given array. Array
elements must be inputted by the user. Filename: AveArray

6. Filename: Largest

#include <iostream>
using namespace std;
int main(){
int array[10]={1,2,3,4,5,6,7,8,9,0};
int loop, largest;
largest= array[0];
for(loop =1; loop <10; loop++){
if( largest < array[loop])
largest= array[loop];
}
cout<<"Largest element of array is "<<largest;
return0;
}

7. Write your own program that will find the smallest element in a given array of size 10.
Array elements must be provided by the user. Filename: Smallest

8. Filename: SecLarge

#include <iostream>
using namespace std;

int main()
{
int array[10]={101,11,3,4,50,69,7,8,9,0};
int loop, largest, second;

if(array[0]> array[1]){
largest= array[0];
second = array[1];
}else{
largest= array[1];
second = array[0];
}

for(loop =2; loop <10; loop++){


if( largest < array[loop]){
second= largest;
largest= array[loop];
}else if( second < array[loop]){
second= array[loop];
}
}

cout<<"Largest element of array is "<<largest<<endl;


cout<<"Second largest element of array is "<<second;
return 0;

}
9. Write a program in C++ to find the maximum and minimum element in an array. 
Filename: MaxMin

Sample Test Data

Input the number of elements to be stored in the array


Input 3 elements in the array : 
element - 0 : 15 
element - 1 : 35 
element - 2 : 21 
Expected Output : 
Maximum element is : 35 
Minimum element is : 15 
10. Filename: DivArray

#include <iostream>
using namespace std;
#define DIMENSION 10
int main()
{
int i;
float max, array[DIMENSION];

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


{
cout<<"array[ " <<i;
cout<<" ] = ";
cin>>array[i];
if (i == 0)
{
max = array[i];
}
if (max < array[i])
{
max = array[i];
}
}
cout<<"Biggest element in array is " <<max<<endl;;
for (i = 0; i < DIMENSION; i++) {
array[i] /= max;
cout<<"array[ ";
cout<<i <<" ] = "<<array[i] <<endl;
}
return 0;
}
}
11. Write a program in C++ to print odd and even integers in a given array. Filename:
OddEven

Sample Test Data


 
Input the number of elements to be stored in the array :5 
Input 5 elements in the array : 
element - 0 : 15 
element - 1 : 5 
element - 2 : 22 
element - 3 : 2 
element - 4 : 4 
Expected Output : 
Even elements :  22 2 4 
Odd elements : 15 5
12. Filename: NewValue

#include <iostream>
using namespace std;
int main()
{
int arr1[100],i,n,p,x;

cout<<"\nInsert New value in the array : \n ";


cout<<"-----------------------------------------\n";
cout<<"Input the size of array : ";
cin>>n;
// Stored values into the array
cout<<"Input "<<n <<" elements in the array in ascending order:\n";
for(i=0;i<n;i++)
{
cout<<"element - "<<i<<" : ";
cin>>arr1[i];
}

cout<<"Input the value to be inserted : ";


cin>>x;
cout<<"Input the Position where the value will be inserted :";
cin>>p;

cout<<"The current list of the array :\n";


for(i=0;i<n;i++)
cout<<arr1[i];

// Move all data at right side of the array


for(i=n;i>=p;i--)
arr1[i]= arr1[i-1];
// insert value at given position
arr1[p-1]=x;

cout<<"\n\nAfter inserting the element, the new list is :\n";


for(i=0;i<=n;i++)
cout<<arr1[i] <<endl;

return 0;
}

You might also like