I add some photo of the instruction.
But my prof wanted to change integer into character. how will I do it? this is my code. I use my full potential in programming but this program makes me down. I use all resources i may find but I didn't get the right code
#include <stdlib.h>
#include <iostream>
using namespace std;
int array[10];
void DisplayArray() {
for (int i = 0; i < 10; i )
cout << "Array [ " << i << " ] = " << array[i] << endl;
}
void SetDefaultValues() {
cout << "Defalut Values :" << endl;
for (int i = 0; i < 10; i ) {
array[i] = -1;
cout << "array [" << i << "]"
<< "= " << array[i] << endl;
}
}
void InsertValues() {
cout << "Enter 10 Values " << endl;
for (int i = 0; i < 10; i ) {
cin >> array[i];
}
cout << "\n\t\t\tArray Values Inserted... Successfully " << endl;
}
void DeleteValues() {
cout << "Enter the Index Number To Delete Value :";
int index;
cin >> index;
if (index > 9 || index < 0) {
cout << "Invalid Index Entered-> Valid Range(0-9)" << endl;
DeleteValues(); // Recall The Function it self
} else {
array[index] = -1;
}
cout << "\n\t\t\tArray Value Deleted... Successfully " << endl;
}
void UpdateValues() {
cout << "Enter Index Number to Update Value :";
int index;
cin >> index;
if (index > 9 || index < 0) {
cout << "Invalid Index Entered-> Valid Range(0-9)" << endl;
UpdateValues(); // Recall The Function it self
} else {
cout << "Enter the New Value For Index array[ " << index << " ] = ";
cin >> array[index];
cout << "\n\t\t\tArray Updated... Successfully " << endl;
}
}
int main() {
char option;
SetDefaultValues();
do {
cout << "\t\t\tEnter 1 to Enter Values\n\t\t\tEnter 2 to Update "
"Values\n\t\t\tEnter 3 to Delete Values\n\n\t\t\t or Enter E to "
"EXIT\n\n\t\t\t Enter Option: -> ";
cin >> option;
if (option == '1') {
cout << "Insert Function Called" << endl;
InsertValues();
cout << "Inserted Values :" << endl;
DisplayArray();
} else if (option == '2') {
UpdateValues();
cout << "Updated Array :" << endl;
DisplayArray();
} else if (option == '3') {
DeleteValues();
cout << "Array After Deleting Values :" << endl;
DisplayArray();
} else if (option != 'e' && option != 'E') {
cout << "\n\n\t\t\tSelect A Valid Option From Below\n\n";
}
} while (option != 'e' && option != 'E');
system("cls"); // To Clear The Screen
cout << "\n\n\n\n\n\n\n\n\n\n\t\tProgram Ended Press Any Key To Exit "
"Screen.....\n\n\n\n\n\n\n\n\n\n\n\n"
<< endl;
return 0;
}
This is the output of my current program
Defalut Values :
array [0]= -1
array [1]= -1
array [2]= -1
array [3]= -1
array [4]= -1
array [5]= -1
array [6]= -1
array [7]= -1
array [8]= -1
array [9]= -1
Enter 1 to Enter Values
Enter 2 to Update Values
Enter 3 to Delete Values
or Enter E to EXIT
Enter Option: ->
CodePudding user response:
the first really necessary change is to move int array[10]; to char array[10];
in that way we can store characters instead of store integers.
since your program is using std::cout and std::cin (and both of them got different overloads getting a char or an integer) you don't really need to change nothing else except the default value (-1) which is an invalid character.
You may use basically a dot ('.') as default value or anything else that looks good to you.
About the search option you can basically loop trought the array and print what positions match.
void SearchCharacter(char a){
bool printed_out = false;
for(unsigned int i=0; i < sizeof(array)/sizeof(array[0]); i){
if(array[i] == a){
if(!printed_out){
std::cout << "The character " << a << " is found in position: ";
printed_out = true;
}
std::cout << i << " ";
}
}
if(printed_out)
std::cout << std::endl;
else
std::cout << "No matches found for character: " << a << std::endl;
}
