issue with pointers

So this program asks the user to create and initialize a dynamic array,display the min,max, and average of the array, and search for an element, but its not giving me the right info for the min,max,average are the same integer, and the third part gives me a "integer not found." can someone look over the code?

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int *arr;
int *createArray(int &size);
void findStats(int *arr, int size, int &min, int &max, double &average);
int *searchElement(int *arr, int size, int *element);



int main()
{
   int userOption;
   int ix = 0;
   int size;
   int  min = 0;
   int max = 0;
   int userElement;
   double average = 0;

   do
   {
      cout << "1. Create and initialize a dynamic array" << endl;
      cout << "2. Display statistics on the array" << endl;
      cout << "3. Search for an element" << endl;
      cout << "4. Quit" << endl << endl;
      cout << "Please select option (1-4)" << endl;

      cin >> userOption;

      if (userOption != 1 && userOption != 4 && ix == 0)
      {
         cout << endl << "Error; initial choice must be 1." << endl << endl;
      }

      ix++;

   
      if (userOption == 1)
      {
         cout << "Please enter an integer value for the size of the array." << endl;
         cin >> size;
         int arr = *createArray(size);

         for (int ia = 0; ia < size; ia++)

            cout << "Element " << ia << ":" << arr + ia << endl;
         cout << endl << endl;
      }

      else if (userOption == 2)
      {
         findStats(arr, size, min, max, average);

         cout << endl << "Statistics:" << endl;
         cout << "Min: " << min << endl;
         cout << "Max: " << max << endl;
         cout << "Average: " << average << endl << endl;

      }

    
      else if (userOption == 3)
      {
         
         cout << endl << " Enter an integer to search the array for." << endl;
         cin >> userElement;
         int *element = &userElement;

         int location = *searchElement(arr, size, element);

         if (location)

            cout << "Integer found in element: " << location << endl;

         else

            cout << "Integer not found." << endl;

         cout << "Select 4 to end." << endl;

      }

   } while (userOption != 4); 

   return 0;

} 




int *createArray(int &size)
{

 
   int* userArray = new int[size]();

   for (int ixx = 0; ixx < size; ixx++)
   {
      srand(time(NULL));

      userArray[ixx] = rand() % 100 + 1;
   }

  
   arr = *&userArray;
   return arr;

}


void findStats(int *arr, int size, int &min, int &max, double &average)
{
   int     ifs = 0,
      sum = 0;

   min = (arr[ifs]);

   for (ifs = 0; ifs < size; ifs++)
   {

      if (min >(arr[ifs]))
      {
         min = (arr[ifs]);
      }

      if (max < (arr[ifs]))
      {
         max = (arr[ifs]);
      }

      sum += (arr[ifs]);
   }

   average = sum / size;

}

int *searchElement(int *arr, int size, int *element)
{
   int             *location,
      index = 0,
      position = -1;
   bool    found = false;

   while (index < size && !found)
   {
      if (arr[index] == *element);
      {
         found = true;
         position = index;
      }

      index++;
   }

   return location = &position;

}
Last edited on
Line 47 int arr = *createArray(size); should be int *arr = createArray(size);

Line 51 cout << "Element " << ia << ":" << arr + ia << endl;should be cout << "Element " << ia << ":" << *(arr + ia) << endl;

This srand(time(NULL)); should execute only once. Move it to the top of main.
You have 2 issues:
1. Line 8 - don't use global variables, it gets confusing about which arr you are using
2. Line 47. You create arr as an int.
This srand(time(NULL)); should execute only once. Move it to the top of main.


thatll give me an error
thatll give me an error
It won't. If it is, post what you did, and exact error you get.

Also line 153 is meaningless as it is written now. Turn on warnings and see what is the problem here.
im confused inside the function main() or outside of it right above it?
At the top of the main, aka first line in main.
Also line 153 is meaningless as it is written now.

what do you mean?
Could you tell us what you're trying to do at line 153?
i can see how its wrong now, im trying to match the element with the integer the user enters
I was actually referring to other problem:
\Test\main.cpp|153|warning: suggest braces around empty body in an 'if' statement [-Wempty-body]|
It is actually exacuted as:
1
2
3
4
5
6
if (arr[index] == *element)
    ; //Empty statement

//Braces are not needed: execution is unconditional
found = true;
position = index;
can you elaborate on that^
Last edited on
1
2
3
4
5
6
if (arr[index] == *element)
    ; //Empty statement

//Braces are not needed: execution is unconditional
found = true;
position = index;
Do you know how if statements work? The program checks if the condition is true, then executes the code right after. If there are no brackets, then obviously it only associates a single line of code with the if statement.

When I say a line of code, I don't mean an actual line. I mean whatever code is there until the first semicolon (';'). In the case of your if statement, the semi-colon is right after your condition (before any brackets).

Therefore, if the condition is true, the program will execute the code between the ending ')' of your condition and before the end of the line ';' which in this case happens to be nothing. Basically, the code is saying if the condition is true, do nothing.
i get that, but after correcting it, it gives me "integer not found" only for the integer in element 0 but works fine for the others
The problem is with this:

1
2
3
4
5
6
7
         if (location)

            cout << "Integer found in element: " << location << endl;

         else

            cout << "Integer not found." << endl;

To be more specific, the if condition.
Topic archived. No new replies allowed.