Help with Code PLEASE!!

I need to create a program that does this, but so far I have been getting nothing but errors. I've been having a friend look my code over but she is stumped too.
the instructions from my professor:
In the main function display a menu to get user’s option for:

1. Create and initialize a dynamic array
2. Display statistics on the array
3. Search for an element
4. Quit

The user should choose 1 first to proceed with the rest of the options.

When option 1 is chosen, ask the user for an integer value for size from inside the main function. This value will be the size of the array that you will create dynamically. Then call a function with the following prototype and description:

int *createArray(int &size) : Dynamically allocate and initialize with random numbers an array of size size. The parameter to this function is a reference variable which will store the size of the array. The function will return a pointer to the dynamically allocated array. In the main function, display contents of the array.

When option 2 is chosen, call a function with the following prototype and description:
void findStats(int *arr, int size, int &min, int &max, double &average) : This function finds the statistics about the array by searching the minimum and maximum elements and by calculating the average of elements (The average is calculated by adding each element value in the array then dividing this sum by the size of the array).
The first parameter to the function is a pointer to the dynamically created array, the second parameter is the size of the array that is passed by value, the third parameter is the minimum value that is passed by reference, the fourth parameter is the maximum value that is passed by reference, and the last parameter is the average value that is passed by reference. The findStats function returns a void, i.e. nothing. Still, since min, max, and average are passed by reference, you should be able to display these variables’ calculated values from inside the caller (main) function. Display the value of each statistic properly in the caller function.

When option 3 is chosen, get an integer value for an element from inside the main function. Then call a function with the following prototype and description:
int *searchElement(int *arr, int size, int *element): Search for the element stored in the variable element inside the array. The first parameter to the function is a pointer to the array. The second parameter is the size of the array. The third parameter is a pointer to the element variable for which you’re going to search. You may either use linear or binary search here, whichever is most convenient for you, but if you use a binary search, you must sort the array. The function returns a pointer to the element found in the array, or a 0 if the element is not found to the main function. Please display properly where the element is found (if found), or display not found (if the element is not found) in the main function.

When option 4 is chosen, quit the program.


My code:

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

using namespace std;
//This program is used to the ability to use pointers
//to handle arrays and variable locations,
//and accomplish dynamic memory allocation


//function prototypes
int *createArray(int) ;
void findStats(int, int, int, int, double);
int *searchElement(int, int, int);



int main(){

int choice, size, element, min, max;
int *arr;
double average;

cout << " choose one of the options" << endl;
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;
cin >> choice;

if (choice = 1){
cout <<"what is the size of the array?" << endl;
cin >> size;

cout << " here are the contents of the array" << endl;
cout << *createArray(size);
}
else if (choice = 2){
findStats(arr, size, min, max, average); //call
cout << "Min = " << *min << endl;
cout << "Max = " << *max <<endl;
cout << "Average = " << *average << endl;
}
else if (choice = 3){
cout << "Enter a number to search for in the array: " << endl;
cin >> element;

searchElement(arr, size, element);// call the option

int arr[size],found;

found = searchElement(arr, size, element);

if(found==1)
cout<<"\nItem found";
else
cout<<"\nItem not found";
}
else if (choice = 4){
cout << "Quit"<< endl;
}

//free memory
delete [] arr;
arr[] = 0;

return 0;
}





int *createArray(int &size)
{

int *arr;

int num = 0;

cout << "Enter in a random number: ";
cin >> num;
int *createArray; // array to hold the numbers
// return null if number is 0 or negative
if (size <= 0)
return NULL;

// Dynamically allocate the array
arr = new int[size];
//seed random number
srand( time(0) );
// populate array with numbers
for (int size = 0; size < num; size++)
arr[size] = rand();
//return a pointer to the array

return arr;
}

void findStats(int *arr, int size, int &min, int &max, double &average) {
for (int i = 1; i < size; ++i) {
if (*arr[max] > *arr[i]) {
max = i;
}
}

min = 0;
for (int i = 1; i < size; ++i) {
if (*arr[min] < *arr[i]) {
min = i;
}
}

average = 0;
for (int i = 1; i < size; ++i) {
average += *arr[i];
}

average == average/size;
}



int *searchElement(int *arr, int size, int *element)
{
for(int I = 0; I < size; I++)
{
if(*arr[I] == *element)
return 1;
}
return 0;
}

what am I doing wrong and how do I fix it please?

Here u go, ive removed the errors, dont know anything about ur Logic
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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;
//This program is used to the ability to use pointers 
//to handle arrays and variable locations,
//and accomplish dynamic memory allocation



//function definition
int *createArray(int &size)
{

int *arr; 

int num = 0;

cout << "Enter in a random number: ";
cin >> num;
int *createArray; // array to hold the numbers
// return null if number is 0 or negative
if (size <= 0)
return NULL;

// Dynamically allocate the array
arr = new int[size];
//seed random number
srand( time(0) );
// populate array with numbers
for (int size = 0; size < num; size++)
arr[size] = rand();
//return a pointer to the array

return arr;
}

void findStats(int* arr, int size, int &min, int &max, double &average) {
for (int i = 1; i < size; ++i) {
if (arr[max] > arr[i]) {
max = i;
}
}

min = 0;
for (int i = 1; i < size; ++i) {
if (arr[min] < arr[i]) {
min = i;
}
}

average = 0;
for (int i = 1; i < size; ++i) {
average += arr[i];
}

average = average/size;
}



int searchElement(int* arr, int size, int element)
{
for(int I = 0; I < size; I++)
{
if(arr[I] == element)
return 1; 
}
return 0;
}


int main(){

int choice, size=0, element, min, max;
int *arr;
double average;

cout << " choose one of the options" << endl;
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;
cin >> choice;

if (choice = 1){
cout <<"what is the size of the array?" << endl;
cin >> size;

cout << " here are the contents of the array" << endl;
cout << *createArray(size);
}
else if (choice = 2){
findStats(arr, size, min, max, average); //call
cout << "Min = " << min << endl;
cout << "Max = " << max <<endl;
cout << "Average = " << average << endl;
}
else if (choice = 3){
cout << "Enter a number to search for in the array: " << endl;
cin >> element;

searchElement(arr, size, element);// call the option

int* arr= new int[10],found;

found = searchElement(arr, size, element);

if(found==1)
cout<<"\nItem found";
else
cout<<"\nItem not found";
}
else if (choice = 4){
cout << "Quit"<< endl;
} 

//free memory
delete [] arr;
arr = NULL;

return 0;
}


your concept of pointers is badly messed up, you are unnecessarily using pointer variables for everything, this may help u:
http://www.programminggeeksinchrysalis.blogspot.de/2012/04/introduction-to-pointers.html

http://www.cplusplus.com/articles/DGE1wA7f/
Thank so much for your help! I understand my knowledge of pointers isn't very good, I'll look over the tutorial sites you have provided. Is there anything else that can help me get better at this?
The only way for you to get better is practise practise and more practise. :P Really just think of little programs that you can make for yourself and try to use pointers in it has much as you can. While doing this keeping the Tutorial on C++ from this website or the textbook you have from school close by so when things don't work you can look at the book to see where you went wrong. :)
Pointer Help

Imagine..

1
2
3
int array[4] = {10,12,13,14};
//array[0] == 10, array is a pointer to the address of the 0'th element, so
int *pointer = array; // will point to the 0'th element's ADDRESS! 


the (*) means it points the the address so if you output the pointer now it
will display something like 0x28fedc, this is an address it could be any number like 100 (don't change it to 100, this is figuratively speaking)..
1
2
//i.e
std::cout<<pointer;


imagine the addresses were from say 100, an arrays elements increase 4 bytes for ints, 1 byte for chars e.t.c..

A visual representation of an array..
=======
= 10 =address 100 <--- pointer
=======
= 12 =address 104
=======
= 13 =address 108
=======
= 14 =address 112
=======

in the case of above doing..
std::cout<<pointer;
will output 100..

if you output the pointer again with the (*) it will deference it and display the actual value 10 rather than the address.
std::cout<<*pointer;

if you increment the address, if it's an integer it will increment 4 bytes automatically..
1
2
//ie
pointer++;


=======
= 10 =address 100 <--- (where we were)
=======
= 12 =address 104 <--- pointer ... (after pointer++;)
=======
= 13 =address 108
=======
= 14 =address 112
=======

running this program should help clear it up..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main(){
	int array[4] = {10,12,13,14};
	int *pointer = array;
	std::cout<<"pointer <address>("<<pointer<<"), *pointer <value>("<<*pointer<<")\n";
	pointer++;
	std::cout<<"pointer <address>("<<pointer<<"), *pointer <value>("<<*pointer<<")\n";
	pointer++;
	std::cout<<"pointer <address>("<<pointer<<"), *pointer <value>("<<*pointer<<")\n";

	
	int int_address = (int) pointer;
	std::cout<<"address as decimal ("<<int_address<<") as hexidecimal ("
			 <<std::hex<<int_address<<")\n";
	return 0;
}


Hope this helps you with pointers they can be a pain when you first start with them.
Last edited on
Topic archived. No new replies allowed.