i am supposed to write a program about how to find the position, reverse, average, search and finally after a loop exist by the user choose but every time i pick reverse first and the hit p the program gives me the reverse position unless i reverse it again t make it as the beginning. can someone help me to fix this problem. and my other question is do i really need make a function for this program or this is just like this is fine.
#include<iostream>
#include<string>
#include<iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int size; //in order user pick a number between 15 to 20
cout << "please select a number between 15 to 20: ";
cin >> size;
int *arrayx = newint[size]; // I used dynamic array
srand((unsigned)time(0)); // in order to have randome number everytime
for (int x = 0; x < size; x++) // for loop for for different numbers
{
arrayx[x] = 1 + (rand() % 20); // function for randome numbers
cout << arrayx[x] << ", ";
}
cout << "\n\nthe program displays the array elements position and value pairs for all member elements of the array" << endl;
cout << "the program displays the reversed version of the array" << endl;
cout << "the program calculates and displays the average of the array elements" << endl;
cout << "the program asks the user to insert an integer number and look for it in the array, returning the message wheher the number is found and its position(including multiple occurences), or is not found." << endl;
char option; //
do{
cout << "\n[P]osition, [R]everse, [A]verage, [S]earch, [Q]uit " << endl;
cout << "\n\nPlease select an option: ";
cin >> option;
if (option == 'p' || option == 'P')
{
for (int sh = 0; sh < size; sh++)
{
cout << setw(2) << "element [" << sh << "] is " << arrayx[sh] << endl;
}
}
elseif (option == 'r' || option == 'R')
{
reverse(arrayx, arrayx + size);
cout << "\nyour reversed version is: ";
for (int i = 0; i < size; i++)
{
cout << arrayx[i] << ", ";
}
}
elseif (option == 'a' || option == 'A')
{
float sum = 0;
for (int alexis = 0; alexis < size; alexis++)
{
sum += arrayx[alexis];
}
cout << "the average is " << sum / size << endl;
}
elseif (option == 's' || option == 'S')
{
bool found = false;
int search;
cout << "enter the number you are looking for: ";
cin >> search;
for (int b = 0; b < size; b++)
if (arrayx[b] == search)
{
found = true;
cout << "the number was found at " << b << endl;
}
if (!found)
{
cout << "the following number does not exist " << endl;
}
}
} while (option != 'Q' && option != 'q');
cout << "see you next time " << endl;
delete[] arrayx;
system("pause");
return 0;
}
every time i pick reverse first and the hit p the program gives me the reverse position unless i reverse it again t make it as the beginning.
Yeah, because reverse modifies the array and unless you reverse it again it will stay in reverse order. If you don't want the reverse to be permanent you can do one of the following:
* use reverse once before the loop and another time after the loop,
* create a copy of the array and then reverse and print the copy, or
* don't use reverse at all but instead change the for loop so that it iterates in reverse order.
shervin1373 wrote:
do i really need make a function for this program or this is just like this is fine.
You never strictly need more than the main function but I don't think it would hurt if you split it into multiple functions. If done right it will make the code easier to read and maintain.