Answered step by step
Verified Expert Solution
Question
1 Approved Answer
[c++ Programming Question] I want to find middle element in my array but int size = (sizeof(array) / sizeof(array[0]) )/2 - 1; is not a
[c++ Programming Question] I want to find middle element in my array but int size = (sizeof(array) / sizeof(array[0]) )/2 - 1; is not a work that is located in middleValue method. I got always size = 0.... #include#include // Input/Output with Files , ifstream(read from file) + ofstream(write on file) using namespace std; class Random { private: public: void allValues(int array[]) { cout << "Show all values" << endl; for(int i =0; i < 1000; i++) { cout << array[i] << endl; } } void sumValues(int array[]) { cout << "Output the sum of all values" << endl; int sum = 0; for(int i = 0; i < 1000; i++) { sum += array[i]; } cout << sum < highValue) { highValue = array[i]; } } cout << highValue < array[j+1]) { temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } cout << "Output all values thar sorted in Bubble sort" << endl; for(int k = 0; k < 1000; k++) { cout << array[k] << endl; } } void linearSearch(int array[]) { int searchValue, flag; cout << "Input what values do you want to find: " << endl; cin >> searchValue; flag = 0; for(int i = 0; i < 1000; i ++) { if(array[i] == searchValue) { cout << searchValue << "is located at" << i+1 << endl; flag = 1; //return; } } if(!flag) { cout << searchValue << "is not exsited in array" << endl; } } }; int main() { int ranNum; //random number const int arraySize = 1000; // Fixed array size int array[arraySize]; int choice; // Used in Menu Part ofstream outfile; //Write on File outfile.open("randomfile"); srand(time(0)); // producing a series of pseudo-random integers /**If we generate a sequence of random number with rand() function, it will create the same sequence again and again every time program runs */ cout << "Writing data to file..." << endl; for (int i = 0; i < 1000; i++) { ranNum = (rand() % 1000) + 1; //cout << ranNum << " "; outfile << ranNum << " "; } outfile.close(); ifstream file; file.open("randomfile"); file.seekg(0, ios::beg); cout << "Reading data from file..." << endl; for (int i = 0; i < 1000; i++) { file >> array[i]; // Push into my array } file.close(); cout << "===========================================================" << " "; Random rd; // random rd is not working wtf? /** * Here while passing the array as parameter, we just need to send * the name of the array without its size */ do { cout << "1.Show All Values" << endl; cout << "2.Show Sum of All Values" << endl; cout << "3.Show All Odd Values" << endl; cout << "4.Show All Even Values" << endl; cout << "5.Show Middle Value" << endl; cout << "6.Show First Element value" << endl; cout << "7.Show Last Element Value" << endl; cout << "8.Show Highest Value" << endl; cout << "9.Show Lowest Value" << endl; cout << "10.Sorting array by using bubble sort" << endl; cout << "11.Finding value by using Linear Search" << endl; cout << "12.Terminate Program" << endl; cout << "Input Your Choice: " << endl; cin >> choice; switch(choice) { case 1: rd.allValues(array); break; case 2: rd.sumValues(array); break; case 3: rd.oddValue(array); break; case 4: rd.evenValue(array); break; case 5: rd.middleValue(array); break; case 6: rd.firstValue(array); break; case 7: rd.lastValue(array); break; case 8: rd.highestValue(array); break; case 9: rd.lowestValue(array); break; case 10: rd.sortValue(array); break; case 11: rd.linearSearch(array); break; case 12: cout << "Terminate Program" << endl; } }while(choice != 12); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started