Posted by : Waqas Javed Wednesday, 4 March 2015

Create dynamic array you can change the prototype of function where you feel necessary. N is the size of the array

1. void populate_array(int *array, int N);
// this initializes an array of size N with random values.
2. void print_array(int *array, int N);
// this function prints the entire array.
4. void sort (int *array, int N);
//sort the elements of array in ascending order
5. void search (int *array, int N);

//search the element in the array entered by the user.


#include<iostream>
#include<conio.h>
using namespace std;
void populate_array(int *array, int N);
void print_array(int *array, int N);
void search(int *array, int N);
void main(){

                int x;
                cout <<"Enter a number";
                cin >> x;

                int *arr1 = new int[x];

                populate_array(arr1, x);
                print_array(arr1, x);
                search(arr1, x);
                _getch();
}
void populate_array(int *array1, int N){
                for (int i = 0; i < N; i++){
                                *array1 = rand()%100;
                                array1++;
                }
}
void print_array(int *array1, int N){
                for (int i = 0; i < N; i++)
                {
                                cout << *array1 << " ";
                                array1++;
                }
}

void search(int *array1, int N){
                int x;
                bool y = 0;
                cout << "Enter number to search: ";
                cin >> x;

                for (int i = 0; i < N; i++)
                {
                                if (*array1 == x){
                                                cout << "THe elemnt is found at position: " << i+1;
                                                y = 1;
                                               
                                                break;
                                }
                                array1++;
                }

                if (y == 0){
                                cout << "Element is not present in array";
                }
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Powered by Blogger.

- Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -