hey guys,
I have an assignment that asks for an array of 8 random numbers from 60-100 then it asks for the user to input an option A=sum R=reverse and S=find a value in the array.. So far i have the random part but I am so lost on how to get everything else. Can someone please direct me in the right direction.
Why not post the code you have so far, show us where you're not getting the results you expect and your ideas on what's wrong. It's easier to point someone in a right direction, if we know what direction they're already headed.
int linearSearch( const int [], int, int );
char option;
int main()
{
do
{
srand(time(0)); // loop 10 times
int search, array;
for ( int x = 1; x < 10; x++ )
{
cout << setw( 10 ) << ( rand() % (100 - 60 +1) + 60);
}
cout << "\n R for reverse version of the array";
cout << "\n A for the sum of the elements of the array";
cout << "\n S to search for a number in the array (including multiple
occurrences) or if its not found";
cout << "\n E for end program \n";
cout << "Enter a selection code:";
cin >> option;
if(option == 'R'|| option =='r')
cout << "The reverse version of the array = " << endl;
//*************************************************************************************
else if (option == 'A' || option == 'a')
cout<< "The sum = " << endl;
/*************************************************************************************
case 'S':
cout << "Please enter a number to search for "<< endl;
cin >> search;
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int linearSearch( constint [], int, int );
char option; // Put in main. No need to make it global
int main()
{
do// this do and bracket,should be moved to just before your cout statements
{
srand(time(0)); // Only use srand once at the start. Not in a loop
int search, array;// variable array, is NOT an array. array[10] would make it an array of 10. 0 to 9
for ( int x = 1; x < 10; x++ ) // Only gets 9 numbers. 1 to 9. x should be 0
{
cout << setw( 10 ) << ( rand() % (100 - 60 +1) + 60);// Prints out 9 numbers. Nothing is put into an array
// No need, though, to print them out. You search for a number with the search function
// rand() % (100 - 60 +1) + 60) should be .. rand() % 40 + 61
// array[x] = rand() % 40 + 61; would put a number into array
}
cout << "\n R for reverse version of the array";
cout << "\n A for the sum of the elements of the array";
cout << "\n S to search for a number in the array (including multiple occurrences) or if it's not found";
cout << "\n E for end program \n";
cout << "Enter a selection code:";
cin >> option;
if(option == 'R'|| option =='r')
cout << "The reverse version of the array = " << endl;
//*************************************************************************************
elseif (option == 'A' || option == 'a')
cout<< "The sum = " << endl;
/*************************************************************************************
case 'S':
cout << "Please enter a number to search for "<< endl;
cin >> search;
//*///************************************************************************************
}
while (option !='E' || option == 'e'); // Should be (option !='E' && option != 'e')
cout << "End of program";
return 0;
} // end main
This is the assignment....this is what I'm trying to do
Write a program that generates an array filled up with random positive integer number ranging from 60 to 100, and display it on the screen.
After the creation and displaying of the array, the program displays the following:
[R]everse [A]dd [S]earch E[xit]
Please select an option:_
Then, if the user selects:
- R (lowercase or uppercase): the program displays the reversed version of the array.
- A (lowercase or uppercase): the program displays the sum of the elements of the array.
- S (lowercase or uppercase): the program asks the user to insert an integer number and look for it in the array, returning a message whether the number is found and its position (including multiple occurrences, see example below), or is not found.
- E (lowercase or uppercase): the program exits.
NOTE: Until the last option (āeā or āEā) is selected, the main program comes back at the beginning, asking the user to insert a new integer number.
Okay. Everything I pointed out in your code will help do your assignment, except where I mentioned you don't need to print out the array. Go ahead and print them, since your teacher wants it.
I didn't mention your case'S':, which needs to be changed to match the other elseif sections.
Changed
1 2 3
case'S':
cout << "Please enter a number to search for "<< endl;
cin >> search;
to
1 2 3 4 5
elseif (option == 'S' || option == 's')
{
cout << "Please enter a number to search for "<< endl;
cin >> search;
}
and you should put everything you want done when the user presses certain keys, in the curly brackets, otherwise the next line after elseif will execute.
How do I add search and reverse the array? Also if I add the search to another else if the loop doesn't repeat, why is that? I'm thinking of changing everything to switch case
Changing to a switch case, is a good idea. As for the search function, after getting the number o be searched for, just use a for loop, and compare the users choice, with what's in the array, using ==. ( IE: if(search == array[x]) // assuming for loop is x ) Then, if match, cout you found the number in location x.
For the reverse function, I would create another array, copy the first array into it in reverse order, then copy everything back into the original array.
Post your code afterwards, and we can tweak it, together.
Well to reverse the array use the .length(), function from the string header to get the length of the string, which you already know is 10. Create a second array of the same length, and try something like this.