How to Use Pointer in C++

Vu Old Solved Programming Based Assignments Codes are Available in this Section.
Post Reply
User avatar
ALI RAZA
★★★★★★★★★★★★★
★★★★★★★★★★★★★
Posts: 177
Joined: Fri Dec 22, 2017 8:10 am
Location: Lahore
Has thanked: 6 times
Been thanked: 7 times
Contact:
Status: Offline

How to Use Pointer in C++

Post by ALI RAZA » Tue Jan 15, 2019 1:22 pm

How to use Pointers in C++ Language The Code is Given Below

Code: Select all

#include <iostream>
#include <stdlib.h>		/*rand and srand function*/
#include <ctime>		/*time function*/
using namespace std;

const int arraySize = 5;
int array[arraySize];
int minVal=0;
int minValInd=0;

//Random Number Generation Function
void genRandNum(){
	srand(time(NULL));
	for(int i=0; i<arraySize; i++){
		array[i] = (rand()%100)+1;
		cout << array[i]  <<" ";
	}
	cout<< endl;
}

//Finding min value in unsorted array
void findMinNum(int unFirstIndex){
	minVal=array[unFirstIndex];
	for(int i=unFirstIndex; i<arraySize; i++){
		if(minVal>array[i]){
			minVal=array[i];
			minValInd = i;
		}
	}
}

//Swaping min value with leftmost num in unsorted array
void swapValue(int unFirstIndex, int minValInd){
	int temp =array[unFirstIndex];
	array[unFirstIndex] = minVal;
	array[minValInd] = temp;
}

main(){
	
	//Generating random numbers and store in array
	cout<<"\n   Random Numbers are    : ";
	genRandNum();
	
	//Sorting the array using findmin and swaping values in array
	for(int i=0; i<arraySize; i++){
		findMinNum(i); 						
		if(array[i]!=minVal)
			swapValue(i,minValInd);	
	}
	
	//Printing sorted array	
	cout<<"Sorted Random Numbers are: ";
	for(int i=0; i<arraySize; i++)
		cout<<array[i]<<" ";

	cout<<"\n\n";
	system("pause");
}





Post Reply

Last 10 Members Who Visited This Topic

User avatar ALI RAZA (1)

Who is online

Users browsing this forum: No registered users and 1 guest