Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HI, I have created a class menu, to retrieve and show array, but for some reason I do not get the correct element value. Any

HI, I have created a class menu, to retrieve and show array, but for some reason I do not get the correct element value. Any help?

#include  #include  using namespace std; class myClass { private: int length; float *myPointer; public: myClass(int a) { myPointer = new float[a]; // create array length = a; } void setValue(int b, float n) { *(myPointer + b) = n; } float retValue(int c) { return *(myPointer + c); } void setElem(float num, int el){ if(el < length) { *(myPointer + el) = num; }else{ cout<<"\t*** Array is out of bound. *** "; } } // float showElem(int el){ // // // float arr = *myPointer; // // return *(myPointer + el); // // } float lowest() { float low; low = *myPointer; for (int l = 1; l < length; l++) { if (*(myPointer + l) < low) { low = *(myPointer + l); } } return low; } float highest() { float large; large = *myPointer; for (int h = 1; h < length; h++) { if (*(myPointer + h) > large) { large = *(myPointer + h); } } return large; } float average() { float total = 0; for (int av = 0; av < length; av++) { total += *(myPointer + av); } return total / length; } ~myClass() { delete myPointer; } }; void showArray(myClass, int); int main() { float NumberOfElements, num; char ch, choice; int size, n1, n2; do { cout << "How many numbers do you have? "; cin >> size; myClass numbers(size); cout << "Enter your numbers: "; for (int j = 0; j < size; j++) { cout << "Number " << j + 1 << ": "; cin >> NumberOfElements; numbers.setValue(j, NumberOfElements); } showArray(numbers, size); while(ch != 'n') { cout << " 1. Store a number in any element of the array. " << "2. Retrieve a number from any element of the array. " << "3. Largest value. " << "4. Lowest value. " << "5. Average. " << "6. Show Array. " << "7. Exit. "; cout << "\tOption: "; cin >> choice; switch (choice) { cout << setprecision(2); case '1': cout<<"Number: "; cin>>num; cout<<"Array Location: "; cin>>n1; numbers.setElem(num, n1); showArray(numbers, size); break; case '2': cout << "Which element you want to retrieve: "; cin >> n2; cout << "\tThe value of element " << n2 << " in your array is " << numbers.retValue(n2) << " "; break; case '3': cout << " The highest value in the array is: " << numbers.highest() << " "; break; case '4': cout << " The lowest value in the array is: " << numbers.lowest() << " "; break; case '5': cout << " The average of all the numbers in the array is: " << numbers.average() << " "; break; case '6': showArray(numbers, size); break; case '7': cout << "***Exiting***"; ch = 'n'; break; default: cout << "\t *** Wrong Option ***"; } } cout << " Try Again(y/n): "; cin >> ch; } while (toupper(ch) == 'Y'); return 0; } void showArray(myClass numbers, int length) { cout << " \tYour Array: "; for (int i = 0; i < length; i++) { cout << numbers.retValue(i); if (i != length - 1) { cout << ","; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago