I need help with options 6 and 4 in the menu.
This is the menu :
0. Create array: Asks user how many numbers to fill (the length of the partially filled array), with
random numbers 0-99. First fill all 100 elements with zeroes then insert as many random
numbers as specified by the user. Validate the user entered number (1-99).
1. Calculates and prints the highest value, lowest, sum, and average of the partially filled array.
2. Add a number to the end: asks user to enter a number and adds it to the end of the partially
filled array.
3. Find index of a number: Displays the index of a number if it exists otherwise a message
saying that the number is not found.
4. Insert number at index: Ask the user to enter a number and the index to insert it in. Display
error message if the specified index is outside the current partially filled array size.
5. Remove number: Ask the user to enter a number, remove it if it exists otherwise display a
message to indicate that it does not exist.
6 Remove number at index: Ask the user to enter the index. Remove the number at the
specified index or display error message if the specified index is outside the current partially
filled array size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
const int LENGTH =100;
int array[LENGTH];
srand(((unsigned)time(0)));
int cnt = 0;
cout << "Enter the length of the array: " ;
cin >> cnt ;
if( cnt > LENGTH )
cnt = LENGTH ;
for( int i = 0; i < cnt ; ++i )
array[i] = 1 + rand() % 100;
for( int i = 0; i < cnt ; ++i )
cout << array[i] << ' ' ;
cout << '\n' ;
int input;
cout << "Enter your menu choice" << endl;
cout << "1. Find high, low, sum average" << endl;
cout << "2. Add a number to the end" << endl;
cout << "3. Find the index of a number" << endl;
cout << "4. Insert number at index" << endl;
cout << "5. Remove number" << endl;
cout << "6. Remove number at index" << endl;
cout << "7. Quit" << endl;
cin >> input;
if (input == 1) {
int sum = 0;
int average = 0;
int high = array[0];
int low = array[0];
for (int dx = 0; dx < cnt; dx++)
{
sum += array[dx];
if(array[dx] > high) high = array[dx];
if(array[dx] < low) low = array[dx];
}
average = sum/ cnt;
cout << "The highest number is " <<high<<endl << "The lowest number is " << low<< endl<< "The average is " << average << endl;
}
else if (input == 2)
{
int number;
cout << "Enter a number to add" << endl;
cin >> number;
for( int i = 0; i < cnt ; ++i )
cout << array[i] << ' ' ;
cout << number;
}
else if ( input == 3){
int TheNum =0;
int i =0;
cout << "Number to search for: ";
cin >> TheNum;
for(i = 0; i < cnt; i++)
{
if(array[i] == TheNum){
cout << TheNum << " is at index " << i << endl;
for( int i = 0; i < cnt ; ++i )
cout << array[i] << ' ' ;
cout << '\n' ;
}
} if(TheNum!=cnt){
cout << TheNum << " does not exist" << endl;} }
else if (input == 4){
int number =0;
int index =0;
cout << "Enter a number to insert\n" ;
cin >> number;
cout << "Enter the index to insert\n";
cin >> index;
for( int i = 0; i < cnt ; ++i )
cout << array[i] << ' ' ;
cout << number;
}
else if (input == 5){
int del = 0;
int count = 0;
cout << "Enter a number to remove" << endl;
cin >> del;
for(int i=0; i<cnt; i++)
{
if(array[i]==del)
{
for(int j=i; j<(cnt-1); j++)
{
array[j]=array[j+1];
}
count++;
break;
}
}
if(count==0)
{
cout<<"Element not found..!!";
}
else
{
for(int i=0; i<(cnt-1); i++)
{
cout<<array[i]<<" ";
}
}
}
else if (input == 6){
int index=0;
int count=0;
cout << "Enter the index to remove:" << endl;
cin >>index;
for(int i=0; i<cnt; i++)
{
if(index==i)
{
for(int i=index+1; i<cnt; i++)
{
array[i-1] = array[i];
}}}
if(count==0)
{
cout<<"Element not found..!!";
}
else
{
for(int i=0; i<(cnt-1); i++)
{
cout<<array[i]<<" ";
}
}
}
else if (input == 7){
cout << " Good By " << endl;
return 0;
}
}
|