comment the below code
kindly comment the below code
#include<iostream>
#define SIZE 2500
using namespace std;
int counter = 0;
int array[SIZE];
void insertNumber(int number)
int isSorted()
void deleteNumber(int index);
void deleteFronRange( int from, int to);
void Replace(int index , int number);
void findAndReplace(int number , int newNumber);
void ascending();
void descending();
void printArray();
int main()
{
int option , insertOption ;
int index , number1 , number2;
cout << "Enter Number of elements in Array = ";
cin >> counter;
for (int i = 0; i < counter; i++)
{
cout << "\nEnter Number at " << i << " index : ";
cin >> array[i];
}
printArray();
while (1)
{
cout << "\nPress '1' to Insert ";
cout << "\nPress '2' to Delete " ;
cout << "\nPress '3' to Delete from Range " ;
cout << "\nPress '4' to Replace";
cout << "\nPress '5' to Find and Replace";
cout << "\nPress '6' to Display Ascending Numbers";
cout << "\nPress '7' to Display Descending Numbers ";
cout << "\nPress '8' to Display and Exit = ";
cin >> option;
switch (option)
{
case 1:
cout << "Enter the Number to Insert: \n";
cin >> number;
insertNumber(number);
break;
case 2:
cout << "Enter index to delete = \n";
cin >> index;
deleteNumber(index);
break;
case 3:
cout << "Enter number 1 from = \n";
cin >> number1;
cout << "Enter number 2 to = \n";
cin >> number2;
deleteFronRange(number1 , number2);
case 4:
cout << "Enter index to replace = \n";
cin >> index;
Replace(index , number);
break;
case 5:
cout << "Enter the Number to find: \n";
cin >> number;
findAndReplace(number , newNumber);
break;
case 6:
ascending();
break;
case 7:
descending();
break;
case 8:
printArray();
cout << "\nThe array size is " << counter;
return 0;
break ;
default :
cout << "Invalid Number !! Try Again";
}
}
}
void insertNumber(int number) { // this method shows intelligence
int i = 0, j = counter; // if array is unsorted it print numbefr at the end at runtime
if (isSorted() == 1) {
cout << "\n Array is sorted";
while (array[i] < number) {
i += 1;
}
while (j > i) {
array[j] = array[j - 1];
j--;
}
array[j] = number;
counter++;
} else {
cout << "\n Array is unsorted";
array[counter] = number;
counter++;
}
printArray();
}
int isSorted() {
for (int i = 0; i < counter; i++) {
if (array[counter - 1] < array[counter - 2]) //not sorted order
return 0;
}
//sorted
return 1;
}
void deleteNumber(int index)
{
if (index >= counter)
{
cout << "You Entered Wrong Index . It is not possible !! Try Again\n";
}
else
{
for (int i = index; i < counter; i++)
{
array[i] = array[i + 1];
}
counter--;
}
printArray();
}
void deleteFronRange( int from, int to) {
int i, j = 0;
for (i = 0; i < counter; i++) {
if (i <= from - 1 || i >= to + 1) {
array[j] = array[i];
j++;
}
}
for (int i = 0; i < j; i++)
cout << A[i] << " ";
printArray();
}
void Replace(int index , int number)
{
if (index >= counter && index < 0)
{
cout << "Invalid Index \n";
Replace(index , number);
}
else
{
cout << "Enter the number to replace at " << index << " Index = ";
cin >> number;
array[index] = number;
printArray();
}
}
void findAndReplace(int number , int newNumber)
{
int Count = 0;
for (int i = 0; i < counter; i++)
{
if (array[i] == number)
{
Count++;
if (Count == 1)
{
cout << number << " is at" << i <<"index.\n";
cout << "Enter New Number = ";
cin >> newNumber;
array[i] = newNumber;
printArray();
}
else
{
int choice3;
cout << number << " is also at"<< i <<" Index.\n";
cin >> choice3;
if (choice3 == 1)
{
cout << "Enter the new number = ";
cin >> newNumber;
array[i] = newNumber;
printArray();
}
else if (choice3 == 2)
{
printArray();
break;
}
else
{
cout << "Invalid value Entered";
}
}
}
}
}
void ascending()
{
for (int i = 0; i < counter; i++)
{
for (int j = i + 1; j < counter; j++)
{
if (array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printArray();
}
void descending()
{
for (int i = 0; i < counter; i++)
{
for (int j = i + 1; j < counter; j++)
{
if (array[i] < array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printArray();
}
void printArray()
{
cout << "\nPrinted Array is: ";
for (int j = 0; j < counter; j++)
{
cout << array[j] << " ";
if (j == counter - 1)
{
cout << "\n";
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images