question

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.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 #include<iostream>
#include<string>
#include<iomanip>
#include <cstdlib>
#include <ctime>
using namespace 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 = new int[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;
				}
			}

			else if (option == 'r' || option == 'R')
			{
				reverse(arrayx, arrayx + size);
				cout << "\nyour reversed version is: ";
				for (int i = 0; i < size; i++)
				{
					cout << arrayx[i] << ", ";
				}
			}

			else if (option == 'a' || option == 'A')
			{
				float sum = 0;
				for (int alexis = 0; alexis < size; alexis++)
				{
					sum += arrayx[alexis];
				}
				cout << "the average is " << sum / size << endl;

			}

			else if (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;
}
Last edited on
Hi shervin1373,

I am honestly not sure what you are asking. I did look at your code and I noticed your includes are not right.

1.

1
2
// not being used #include<string>
// not being used #include <ctime> 


and 2.

1
2
// you forgot this for reverse? Or not? 
#include <algorithm> 


does that help?
Can you rephrase your question?
Bdanielz wrote:
not being used #include <ctime>

<ctime> is needed for the time function.

shervin1373 wrote:
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.
Last edited on
@Peter

I can run srand/time on my machine and generate random numbers only using

1
2
3
#include<iostream>
#include <iomanip> //time
#include <cstdlib> 


It appears I am getting access to the "time" function from iomanip

Sorry for derailing this thread. I just want to clarify I was not just saying things for the sake of saying things.

EDIT. When I compile with "-ansi" then i get the ctime error. Thank you everyone!
Last edited on
When I compile with "-ansi" then i get the ctime error.


Why would you do that? -ansi means c++98 standard :+)

@the ideas man
I was trying recreate the error that Peter was referring to.

Topic archived. No new replies allowed.