Array Programming Exercise P-2

Array Exercise 

Programming Exercise

Array Programming Exercise P-1

Programming Exercise Looping Structure P-12

Program 10:
Write a program that initializes the values in one-dimensional array and computes the sum of even values assigned to the array. It also displays even values and their sum on the screen.

#include"iostream"
using namespace std;
main()
{
int arr[10]={14,16,34,43,77,34,21,12,33,30};
int i, sum;
i = sum = 0;

while(i<=9)
{
if(arr[i]%2==0)
{
cout<<arr[i]<<endl;
sum = sum + arr[i];
}
i++;
}
cout<<"\nSum of Even Values : "<<sum;

}

Program 11:
Write a program that initializes values 2, 6, 3, 5, 4 and 7 in one-dimensional array. It computes the factorial of value of each element of the array and displays the result on the screen.

#include"iostream" 
using namespace std; main() { long arr[] = {2,6,3,5,4,7},i = 0, f, n; while(i<=5) { f = 1; n = arr[i]; for(; n>=1; n--) f = f * n; cout<<"Factorial of "<<arr[i]<<": "<<f<<endl; i++; } }

Program 12:
Write a program in C++ that inputs data into an array. It inputs a value from the user and searches this value in the array using sequential search. If the entered value is found in the array, it displays the position of the value in the array; otherwise, it displays the message “Value not found”.

#include"iostream" 
using namespace std; main() { int arr[5], n, i, pos; i = 0; while(i<=4) { cout<<"Enter value in element "<<i<<"? ";
cin>>arr[i]; i++; } pos = 0; cout<<"Enter any value ? "; cin>>n; i = 0; while(i<=4) { if(n == arr[i]) { pos = i + 1; break; } i++; } if(pos == 0) cout<<"Value not found "<<endl; else cout<<"Value found at position = "<<pos<<endl; }

Program 13:
Write a program to enter data into one-dimensional array. Find out the maximum value and its location in the array and print the result on the screen.

#include"iostream" 
using namespace std; main() { float arr[10], max; int loc, i = 0; while(i<=9) { cout<<"Value for element "<<i<<" ? "; cin>>arr[i]; i++; } max = arr[0]; i = 1; while(i<=9) { if(max<arr[i]) { max = arr[i]; loc = i; } i++; } cout<<"\nMaximum value is :"<<max<<endl; cout<<"Location of value in array :"<<loc+1; }

Program 14:
Write a program that initializes data into one-dimensional array. The data must be in ascending order. It inputs a value from the user and searches this value in the array using binary search method.

#include"iostream" 
using namespace std; main() { int abc[10] = {1,3,9,15,78,87,95,103,124,352}; int loc, num, i, M, S = 0, E = 9; loc = 0; cout<<"Enter any value ? "; cin>>num; while(S<=E) { M = (S + E)/2; if(num == abc[M]) { loc = M+1; break; } else if(num < abc[M]) E = M - 1; else S = M + 1; } if(loc == 0) cout<<"Value not found "<<endl; else cout<<"Value found at position = "<<loc<<endl; }

Program 15:
Write a program that inserts a new value at a specified location into one-dimensional array of N elements.

#include"iostream" 
using namespace std; main() { int p, val, n, i; int arr[10] = {55,41,12,78,15,69,45,47,36}; cout<<"Values of array before insertion\n"; for(i=0; i<=8; i++) cout<<arr[i]<<" "; cout<<endl; while(1) { cout<<"\nEnter position to insert value ? "; cin>>p; n=9; if(p > n || p < 0) { cout<<"Invalid position\r"; getch(); clreol(); } else break; } while(n >= p-1) { arr[n+1] = arr[n]; n--; } cout<<"Enter value to insert ? "; cin>>val; arr[p-1] = val; cout<<"\nValues of array after insertion\n"; for(i = 0; i<=9; i++) cout<<arr[i]<<" "; }

Program 16:
Write a program that deletes a value from a specified location of one-dimensional array of N elements.

#include"iostream" 
using namespace std; main() { int p, n, i; int arr[10] = {55,41,12,78,15,69,45,47,36,78}; cout<<"Values of array before deletion\n"; for(i = 0; i<=9; i++) cout<<arr[i]<<" "; cout<<endl; while(1) { cout<<"\nEnter position to delete value?"; cin>>p; n = 9; if(p > n || p < 0) { cout<<"Invalid position\r"; getch(); clreol(); } else break; } while(p<=n) { arr[p-1] = arr[p]; p++; } arr[n] = NULL; cout<<"\nValues of array after deletion\n"; for(i = 0; i<=9; i++) cout<<arr[i]<<" "; cout<<endl; }

Program 17:
Write a program that sorts one-dimensional array’s values into descending order using bubble sort method.
// Array sorting using bubble sort method

#include"iostream" 
using namespace std; main() { int array[10] = {55,41,12,78,1,69,45,4,36,5}; int u, i, temp; cout<<"Values of array before sorting\n"; for(i = 0; i<=9; i++) cout<<array[i]<<" "; cout<<endl; // array sorting u = 9; while(u>=1) { i = 0; while(i<u) { if(array[i] < array[i+1]) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; } i++; } u--; } cout<<"\nValues of array after sorting\n"; for(i = 0; i<=9; i++) cout<<array[i]<<" "; }

Program 18:
Write a program that sorts one-dimensional array’s values into ascending order using selection sort method.
// Array sorting using selection sort method.

#include"iostream" 
using namespace std;
main() { int array[10] = {55,41,12,78,1,69,45,4,36,5}; int u, i, temp, min, pos; cout<<"Values of array before sorting\n"; for(i = 0; i<=9; i++) cout<<array[i]<<" "; cout<<endl; // array sorting u=0; while(u<9) { min = array[u]; pos = u; i = u; while(i<=9) { if(min > array[i]) { min = array[i]; pos = i; } i++; } // swap values temp = array[pos]; array[pos] = array[u]; array[u] = temp; u++; } cout<<"\nValues after sorting\n"; for(i = 0; i<=9; i++) cout<<array[i]<<" "; }

Program 19:
Write a program that sorts one-dimensional array of 5 elements in ascending order using insertion sort method.

#include"iostream" 
using namespace std;
void main(void) { int abc[4], val, u, i; cout<<"Enter 5 values "<<endl; for(i = 0; i<=4; i++) cin>>abc[i]; for(u = 0; u<=4; u++) { val = abc[u]; for(i = u; i >=0 && val<abc[i-1]; i--) abc[i] = abc[i-1]; abc[i] = val; } cout<<"Sorted array "<<endl; for(i = 0; i<=4; i++) cout<<abc[i]<<endl; }

Program 9.20:
Write a program that inputs data into a table and displays on the screen in tabular form.

#include"iostream" 
using namespace std;
main() { int table[3][4]; int r, c; r = 0; while(r<=2) { for(c = 0; c<=3; c++) { cout<<"Enter value for row "<<r<<" and column "<<c<<" ? "; cin>>table[r][c]; } r++; } cout<<"\nValues in tabular form" <<endl; for(r = 0; r<=2; r++) { for(c = 0; c<=3; c++) cout<<table[r][c]<<"\t"; cout<<endl; } }

 

Leave a Reply