Random number array (sum, reverse, search)

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.

any help would be greatly appreciated

-E
@eediott

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.
don't judge my code, I'm new and my teacher isn't much help. Thanks @whitenite1

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

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;

//*///************************************************************************************

}
while (option !='E' || option == 'e');
cout << "End of program";
return 0;

} // end main
@eediott

Here's my ideas on your code. I made notes in it, but did not change your program. Ask more questions when you get this settled out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int linearSearch( const int [], 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;

//*************************************************************************************
else if (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 
Last edited on
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.
@eediott

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 else if sections.
Changed
1
2
3
case 'S':
cout << "Please enter a number to search for "<< endl;
cin >> search;
to
1
2
3
4
5
else if (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 else if 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
@eediott

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.
@eediott

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.

1
2
3
4
5
6
7
     unsigned int length=normalArray.length();
     int reverseArray[length];
     for(int count=0; count<10; count++)
    { 
        reverseArray[count]=normalArray[length-1];
        length--;
    }


This would repeat until the all the elements in the array have been transferred.

Searching the array is even simpler, and I even did it in the above example.
Simply ask for which one of the elements they want.

1
2
3
cout<<"What element would you like to retrieve from the array?"<<endl;
cin>>index;
cout>>"The value of that element is :">>normalArray[index];


However if you intend to search by the value of the element. That is a bit different. Let me know if that helped.
Topic archived. No new replies allowed.